我是新來的,我更適應於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)。
那麼,真正的問題是什麼?我的帖子中沒有看到問號。也許你應該確切地指出你的問題在哪裏指定。 – Xeo 2012-01-11 13:59:45
這裏只是一個猜測(你的代碼應該儘可能短,仍然顯示問題),但你有'hf = CreateFile(...)'從用戶獲得文件名後。如果共享模式不兼容,則無需執行此操作,並且可能會導致位圖加載失敗。 – tinman 2012-01-11 14:07:02
謝謝大家的評論,讚賞。 問題是:目前我想用OpenFileName的lpStrFile替換硬編碼的「F-35C.bmp」。 但它不起作用,抱歉,再次感謝。 – Newbie 2012-01-11 14:11:41