2013-12-20 49 views
0

我想使用MFC應用程序顯示位圖圖像。 我正在使用瀏覽按鈕來選擇正常工作的文件。但是,當我嘗試通過雙擊文件加載圖像時,應用程序啓動,但圖像不顯示。使用mfc對話框顯示.bmp圖像時出錯

這裏是我的代碼瀏覽按鈕和功能打開雙擊圖像。

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:\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; 

}`

我用於瀏覽按鈕相同的代碼,並顯示圖像。但是,當我雙擊文件時,圖像不顯示。請告訴我我做錯了什麼。

回答

0

我找到了我的問題的答案。這其實是一個非常愚蠢的錯誤。 當我使用命令行讀取文件地址時,地址有單斜槓,而我需要使用雙斜槓傳遞地址。 這樣一個愚蠢的錯誤。對不起,浪費你的時間。

1

如果您已將您的應用程序與特定的文件擴展名關聯,則當您雙擊此文件時(如您所述),它將自動啓動。

發生這種情況時,應用程序將以作爲命令行參數提供給您的應用程序的文件名(實際上是完整路徑)啓動。

在SDI MFC應用程序中,只要您沒有重寫默認的文件/打開處理機制,就可以由框架自動處理,但是如果您有基於對話框的應用程序,則需要爲此自行添加代碼。

+0

我明白你的觀點。 我添加了這段代碼來獲取路徑。 \t'char * buff; \t char * command_line = GetCommandLine(); \t \t \t buff = strchr(command_line,''); \t buff ++; \t \t if(buff!= NULL) \t { \t \t dlg.OpenImage1(buff); \t \t \t}' – Khushboo

+0

我得到正確的路徑。但靜止圖像不顯示。 – Khushboo

+0

@Khushboo所以,當你調試到這個功能,你有沒有得到完全相同的文件名,當你瀏覽?請記住,命令行中的第一個參數是應用程序名稱本身。 –

0

據我所知,當你雙擊文件選擇文件對話框中的圖像時,圖像不顯示。我只是嘗試了你的函數OnBnClickedButton1和OpenImage1的代碼。事實證明,雙擊圖像時顯示圖像。我在win7上使用VS2010。我希望這會幫助你,雖然我不會找到你的代碼的錯誤。

+0

我不確定你目前是否瞭解我。我的意思是,當我選擇打開圖像並選擇此應用程序時,會彈出對話框,但沒有圖像。然後我再次瀏覽打開它。 – Khushboo

1

您的對話框COpenImageDlg在命令行有機會處理之前被創建並顯示在DoModal的調用中。當DoModal返回時,對話框已經被銷燬,所以沒有對話框供代碼使用。

+0

是的,就是這個問題。你能告訴我該如何解決它?我應該在調用OpenImage1函數後給出'DoModal'調用嗎?由於'CClientDC',如果我刪除此調用,則會出現錯誤。 – Khushboo

+0

@Khushboo,解決這個問題的簡單方法是在'COpenImageDlg'內部完成所有的繪圖。直接打開文件的路徑在對話框打開之前傳遞到對話框中(Domodal)。這可以通過聲明一個公共成員變量'COpenImageDlg'來完成,並在實例化對話框之後從外部設置值。 – nobody99

+0

@Khushboo,在對話框的OnInitDialog函數中,檢查這個變量,如果它不是空的,直接執行位圖操作。但是,繪製位圖的邏輯序列中存在問題,因爲它可能無法正確重新繪製。處理圖形的正確方法是緩存位圖並將其繪製在「OnPaint」或「OnEraseBkgnd」中。 – nobody99