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



我正在尝试使用 MFC 应用程序显示位图图像。我正在使用浏览按钮来选择正常工作的文件。但是当我尝试通过双击文件加载图像时,应用程序已启动,但不显示图像。

这是我的浏览按钮和打开双击图像的功能的代码。

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); 
}

初始化类:

'BOOL COpenImageApp::InitInstance(){ InitCommonControlsEx() 在 Windows XP 上是必需的,如果应用程序 清单指定使用 ComCtl32.dll版本 6 或更高版本启用 视觉样式。 否则,任何窗口创建都将失败。

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:UsersRaguvaranDesktoptiger.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;

}'

我对浏览按钮使用相同的代码,它显示图像。但是当我双击该文件时,图像没有显示。请告诉我我做错了什么。

如果您已将应用程序与特定的文件扩展名相关联,则当您双击此类文件时,它将自动启动(如您所说)。

发生这种情况时,将使用作为命令行参数提供给应用程序的文件名(实际上是完整路径)启动应用程序。

在 SDI MFC 应用程序中,只要您没有覆盖默认的 File/Open 处理机制,框架就会自动处理此问题,但如果你有基于对话框的应用程序,则需要自己添加代码。

在命令行有机会处理之前,将创建对话框COpenImageDlg并显示在对DoModal的调用中。当DoModal返回时,对话框已被销毁,因此没有可供代码绘制的对话框。

我知道当您双击文件以在文件对话框中选择图像时,图像不会显示。我刚刚尝试了您的函数 OnBnClickedButton1 和 OpenImage1 的代码。事实证明,双击选择图像时会显示图像。我在win7上使用VS2010。我希望这会对您有所帮助,尽管我没有找到您的代码错误。

我找到了问题的答案。这实际上是一个非常愚蠢的错误。当我使用命令行读取文件地址时,地址具有单斜杠,而我需要使用双斜杠传递地址。真是个愚蠢的虫子。很抱歉浪费您的时间。

最新更新