简体   繁体   English

使用mfc对话框显示.bmp图像时出错

[英]Error with displaying an .bmp image using mfc dialog

I am trying to display a bitmap image using MFC application. 我试图使用MFC应用程序显示位图图像。 I am using a browse button to select file which is working properly. 我正在使用浏览按钮来选择正常工作的文件。 But when I try to load an image by double clicking on the file, the application is launched, but the image is not displayed. 但是当我尝试通过双击文件来加载图像时,应用程序将启动,但不会显示图像。

Here is my code for browse button and function to open a double clicked image. 这是我的浏览按钮的代码和打开双击图像的功能。

void COpenImageDlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here
    CString path;
    CFileDialog dlg(TRUE);
    int result=dlg.DoModal();
    if(result==IDOK)
    {
    path=dlg.GetPathName();
    UpdateData(FALSE);
    }

    HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
    CBitmap bmp;

    bmp.Attach(hBmp); 

    CClientDC dc(this); 
    CDC bmDC; 
    bmDC.CreateCompatibleDC(&dc); 
    CBitmap *pOldbmp = bmDC.SelectObject(&bmp); 

    BITMAP  bi; 
    bmp.GetBitmap(&bi); 

    dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY); 

    bmDC.SelectObject(pOldbmp);
}

void COpenImageDlg::OpenImage1(CString path)
{

    //CString path;
    CFileDialog dlg(TRUE);
    int result=dlg.DoModal();
    if(result==IDOK)
    {
    path=dlg.GetPathName();
    UpdateData(FALSE);
    }

    HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
    CBitmap bmp;

    bmp.Attach(hBmp); 

    CClientDC dc(this); 
    CDC bmDC; 
    bmDC.CreateCompatibleDC(&dc); 
    CBitmap *pOldbmp = bmDC.SelectObject(&bmp); 

    BITMAP  bi; 
    bmp.GetBitmap(&bi); 

    dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY); 
}

Init class : 初始类:

`BOOL COpenImageApp::InitInstance() { // InitCommonControlsEx() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. 如果应用程序// manifest指定使用ComCtl32.dll版本6或更高版本来启用//视觉样式,则在Windows XP上需要`BOOL COpenImageApp :: InitInstance(){// InitCommonControlsEx()。 Otherwise, any window creation will fail. 否则,任何窗口创建都将失败。

INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

CWinApp::InitInstance();

AfxEnableControlContainer();

// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;


// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));

COpenImageDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();

char* buff;
char* command_line = GetCommandLine();

buff = strchr(command_line, ' ');
buff++;
buff = strchr(buff, ' ');
buff++;
buff = strchr(buff, ' ');
buff++;

if (buff != NULL)
{

HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, "C:\Users\Raguvaran\Desktop\tiger.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap bmp;
bmp.Attach(hBmp); 

dlg.RedrawWindow();
CClientDC dc(m_pMainWnd); 
CDC bmDC; 
bmDC.CreateCompatibleDC(&dc); 
CBitmap *pOldbmp = bmDC.SelectObject(&bmp); 

BITMAP  bi; 
bmp.GetBitmap(&bi); 

dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY); 

}

//RedrawWindow(dlg, NULL, NULL, RDW_INVALIDATE);
//UpdateWindow(dlg);

if (nResponse == IDOK)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
}

// Delete the shell manager created above.
if (pShellManager != NULL)
{
    delete pShellManager;
}

// Since the dialog has been closed, return FALSE so that we exit the
//  application, rather than start the application's message pump.
return FALSE;

}` }`

I used the same code for browse button and it displays the image. 我使用相同的代码浏览按钮,它显示图像。 But when I double click the file, the image is not displayed. 但是当我双击文件时,图像不会显示。 Please tell me what I am doing wrong. 请告诉我我做错了什么。

If you have associated your application with a particular file extension, it will be launched automatically when you double-click such a file (as you have said). 如果您已将应用程序与特定文件扩展名相关联,则双击此类文件时会自动启动(如您所说)。

When this happens, your application is launched with the file name (actually the full path) supplied as a command line argument to your application. 发生这种情况时,将启动应用程序,并将文件名(实际上是完整路径)作为命令行参数提供给应用程序。

In SDI MFC applications, this is handled automatically by the framework as long as you haven't overridden the default File/Open handling mechanism, but if you have a dialog-based application you will need to add code for this yourself. 在SDI MFC应用程序中,只要您没有覆盖默认的文件/打开处理机制,框架就会自动处理,但如果您有基于对话框的应用程序,则需要自己添加代码。

Your dialog COpenImageDlg is created and displayed inside the call to DoModal before the command line has a chance to be processed. 在命令行有机会被处理之前,会在对DoModal的调用中创建并显示对话框COpenImageDlg When the DoModal returns, the dialog is already destroyed, so there is no dialog for the code to draw upon. DoModal返回时,对话框已经被销毁,因此没有用于绘制代码的对话框。

I understand that when you double click the file to choose a image on file dialog, the image doesn't show. 据我所知,当您双击文件以选择文件对话框时,图像不会显示。 I just tried your code of function OnBnClickedButton1 and OpenImage1. 我刚试过你的函数OnBnClickedButton1和OpenImage1的代码。 And it turns out that the image is displayed when double click to choose a image. 事实证明,双击选择图像时会显示图像。 I use VS2010 on win7. 我在win7上使用VS2010。 I hope this will help you though i donot find the error of your code. 我希望这会帮助你,虽然我不会发现你的代码错误。

I found the answer to my question. 我找到了问题的答案。 It was actually a very stupid mistake. 这实际上是一个非常愚蠢的错误。 When I read the file address using Commandline, the address has single slash, whereas I need to pass the address using double slash. 当我使用Commandline读取文件地址时,地址有单斜杠,而我需要使用双斜杠传递地址。 Such a silly bug. 这么傻的虫子。 Sorry to waste your time. 抱歉浪费你的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM