2012-01-11 33 views
0

我是新來的,我更適應於C#,然後在C++中。Win32 C++:使用openfilename並顯示一個位圖文件

因此,我需要C++專家的幫助來解決我目前遇到的這個困境。

下面列出的僅僅是我認爲有必要完成的代碼片段,不過,我相信還有更多要實現的內容。

#include "stdafx.h" 
#include "winmain.h" 
#include "Resource.h" 
#include <stdio.h> 
#include <CommDlg.h> 
#include <windows.h> 

OPENFILENAME ofn; 
TCHAR szFile[260]; 

switch (message) 
    { 
    case WM_COMMAND: 
     wmId = LOWORD(wParam); 
     wmEvent = HIWORD(wParam); 
     // Parse the menu selections: 
     switch (wmId) 
     { 
           case ID_FILE_LOADBITMAP: 
      bBitmap = !bBitmap; 
      InvalidateRect(hWnd,0,TRUE); 
      break; 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 

case WM_PAINT: 
     hdc = BeginPaint(hWnd, &ps); 
     // TODO: Add any drawing code here... 
if(bBitmap) 
     { 
     ZeroMemory(&ofn, sizeof(ofn)); 
     ofn.lStructSize = sizeof(ofn); 
     ofn.hwndOwner = hWnd; 
     ofn.lpstrFile = szFile; 
     // 
     // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
     // use the contents of szFile to initialize itself. 
     // 
     ofn.lpstrFile[0] = '\0'; 
     ofn.nMaxFile = sizeof(szFile); 
     ofn.nFilterIndex = 1; 
     ofn.lpstrFileTitle = NULL; 
     ofn.nMaxFileTitle = 0; 
     ofn.lpstrInitialDir = NULL; 
     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 

     // Display the Open dialog box. 

     if (GetOpenFileName(&ofn)==TRUE) 
      hf = CreateFile(ofn.lpstrFile, GENERIC_READ, 
       0, (LPSECURITY_ATTRIBUTES) NULL, 
       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 
       (HANDLE) NULL); 

    LoadBitmap(__T("F-35C.bmp"), hdc); 

     EndPaint(hWnd, &ps); 
     break; 
     } 

正如你可以看到,當我點擊我的情況下菜單:LoadBitmap

它只是加載打開文件對話框,選擇,我想沒有在Windows顯示文件,僅此而已。我實際上想要做的是加載文件路徑到LoadBitmap函數,而不是硬編碼它在函數(「F-35C.bmp)。

我也知道nn.lpStrFile有文件路徑,但我我無法載入位圖文件,儘管與ofn.lpStrFile更換__T(「F-35C.bmp」)。如下

顯示LoadBitMap功能的功能。

bool LoadBitmap(LPCWSTR szFileName, HDC hWinDC) 
{ 
    // Load the bitmap image file 
    HBITMAP hBitmap; 
    hBitmap = (HBITMAP)::LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, 
     LR_LOADFROMFILE); 
    // Verify that the image was loaded 
    if (hBitmap == NULL) { 
     ::MessageBox(NULL, __T("LoadImage Failed"), __T("Error"), MB_OK); 
     return false; 
    } 

    // Create a device context that is compatible with the window 
    HDC hLocalDC; 
    hLocalDC = ::CreateCompatibleDC(hWinDC); 
    // Verify that the device context was created 
    if (hLocalDC == NULL) { 
     ::MessageBox(NULL, __T("CreateCompatibleDC Failed"), __T("Error"), MB_OK); 
     return false; 
    } 

    // Get the bitmap's parameters and verify the get 
    BITMAP qBitmap; 
    int iReturn = GetObject(reinterpret_cast<HGDIOBJ>(hBitmap), sizeof(BITMAP), 
     reinterpret_cast<LPVOID>(&qBitmap)); 
    if (!iReturn) { 
     ::MessageBox(NULL, __T("GetObject Failed"), __T("Error"), MB_OK); 
     return false; 
    } 

    // Select the loaded bitmap into the device context 
    HBITMAP hOldBmp = (HBITMAP)::SelectObject(hLocalDC, hBitmap); 
    if (hOldBmp == NULL) { 
     ::MessageBox(NULL, __T("SelectObject Failed"), __T("Error"), MB_OK); 
     return false; 
    } 

    // Blit the dc which holds the bitmap onto the window's dc 
    BOOL qRetBlit = ::BitBlt(hWinDC, 0, 0, qBitmap.bmWidth, qBitmap.bmHeight, 
     hLocalDC, 0, 0, SRCCOPY); 
    if (!qRetBlit) { 
     ::MessageBox(NULL, __T("Blit Failed"), __T("Error"), MB_OK); 
     return false; 
    } 

    // Unitialize and deallocate resources 
    ::SelectObject(hLocalDC, hOldBmp); 
    ::DeleteDC(hLocalDC); 
    ::DeleteObject(hBitmap); 
    return true; 
} 

要添加上,我我正在使用Microsoft Visual Studio 2010開發版與Win32應用程序(Not Console)。

+0

那麼,真正的問題是什麼?我的帖子中沒有看到問號。也許你應該確切地指出你的問題在哪裏指定。 – Xeo 2012-01-11 13:59:45

+1

這裏只是一個猜測(你的代碼應該儘可能短,仍然顯示問題),但你有'hf = CreateFile(...)'從用戶獲得文件名後。如果共享模式不兼容,則無需執行此操作,並且可能會導致位圖加載失敗。 – tinman 2012-01-11 14:07:02

+0

謝謝大家的評論,讚賞。 問題是:目前我想用OpenFileName的lpStrFile替換硬編碼的「F-35C.bmp」。 但它不起作用,抱歉,再次感謝。 – Newbie 2012-01-11 14:11:41

回答

1

你有你的LoadBitmap()參數倒退。這是MSDN

HBITMAP LoadBitmap(
    __in HINSTANCE hInstance, 
    __in LPCTSTR lpBitmapName 
); 

我也相當肯定,你不需要來包裝你的文件名中__T()宏函數調用。

+0

他調用的LoadBitmap()是他自己的代碼,而不是Win32 API。 – tinman 2012-01-11 14:35:05

+0

嗨!非常感謝你,我也意識到了這一點! – Newbie 2012-01-11 14:36:20

+0

- tinman,LoadBitmap函數不是我的,我從其他地方取得函數,我忘了它在哪裏,無論如何,真的感謝你們所有人的幫助!謝謝! – Newbie 2012-01-11 14:37:42

1

我發現在GetOpenFileName調用後立即打開文件的問題,CreateFile中的dwShareMode參數設置爲0(不允許共享)並且不關閉或使用獲取的句柄(hf )。這將導致LoadImage調用失敗,因爲該文件在調用時仍然處於打開狀態而不共享。 解決方案:要麼刪除CreateFile調用,因爲它在代碼中沒用,或者在調用LoadBitmap之前關閉句柄。

1

不要在WM_PAINT的內部完成所有這些,在程序執行過程中這將被調用很多次。

加載一次位圖,並保持HBITMAP。繪畫代碼應該從HBITMAP開始。

相關問題