我正在尝试使用此代码在屏幕上显示bmp :(此代码来自一本书,但我无法在编译时显示窗口,只显示白色窗口)
#include "resource2.h"
#include <vector>
#include <windows.h>
using namespace std;
HWND window1;
HWND window2;
HINSTANCE happ;
HDC handledevice;
PAINTSTRUCT ps;
HBITMAP hbitmap;
BITMAP bitmap;
HDC bmhdc;
HBITMAP oldbm;
LRESULT CALLBACK WindTyp1(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
switch (msg)
{
case WM_KEYDOWN:
if (wparam==VK_ESCAPE)
{
DestroyWindow(window1);
}
return 0;
case WM_PAINT:
handledevice=BeginPaint(hwnd,&ps);
BitBlt(handledevice,0,0,bitmap.bmWidth,bitmap.bmHeight,bmhdc,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
return 0;
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
LRESULT CALLBACK WindTyp2(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
switch (msg)
{
case WM_KEYDOWN:
if (wparam==VK_F1)
{
DestroyWindow(window1);
}
if (wparam==VK_F2)
{
DestroyWindow(window2);
}
if (wparam==VK_ESCAPE)
{
DestroyWindow(window1);
DestroyWindow(window2);
}
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
int WINAPI WinMain(HINSTANCE histance,HINSTANCE hprevinstance,PSTR cmdline,int showcmd)
{
happ=histance;
MSG msg;
hbitmap=LoadBitmap(happ,MAKEINTRESOURCE(happ,IDB_BITMAP1));
GetObject(hbitmap,sizeof(BITMAP),&bitmap);
bmhdc=CreateCompatibleDC(handledevice);
SelectObject(bmhdc,&bitmap);
//clase 1
WNDCLASS windowstyle1,windowstyle2;
windowstyle1.cbClsExtra=0;
windowstyle1.cbWndExtra=0;
windowstyle1.hbrBackground=(HBRUSH) ::GetStockObject(WHITE_BRUSH);
windowstyle1.hCursor=::LoadCursor(0,IDC_ARROW);
windowstyle1.hIcon=::LoadIcon(0,IDI_APPLICATION);
windowstyle1.hInstance=histance;
windowstyle1.lpfnWndProc=WindTyp1;
windowstyle1.lpszClassName="Class 1";
windowstyle1.lpszMenuName=0;
windowstyle1.style= CS_HREDRAW | CS_VREDRAW;
//clase 2
windowstyle2.cbClsExtra=0;
windowstyle2.cbWndExtra=0;
windowstyle2.hbrBackground=(HBRUSH) ::GetStockObject(BLACK_BRUSH);
windowstyle2.hCursor=::LoadCursor(0,IDC_ARROW);
windowstyle2.hIcon=::LoadIcon(0,IDI_APPLICATION);
windowstyle2.hInstance=histance;
windowstyle2.lpfnWndProc=WindTyp2;
windowstyle2.lpszClassName="Class 2";
windowstyle2.lpszMenuName=0;
windowstyle2.style= CS_HREDRAW | CS_VREDRAW;
//registrar ambas clases
RegisterClass(&windowstyle1);
RegisterClass(&windowstyle2);
//crear ventanas
window1=::CreateWindow("Class 1","Ventana Blanca",WS_OVERLAPPEDWINDOW,0,0,1400,1000,0,0,happ,0);
if (window1==0)
::MessageBox(0,"error failed to create window","error",0);
//Show & Update
ShowWindow(window1,true);
UpdateWindow(window1);
//Message Loop
ZeroMemory(&msg,sizeof(MSG));
while (GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
答案 0 :(得分:2)
在这里看一下这个例子 http://www.functionx.com/win32/Lesson13.htm
bmhdc=CreateCompatibleDC(handledevice);
在WinMain中你使用的是handledevice - 尚未初始化,所以选择位图会导致问题,但它可能是0,所以它会使用桌面DC。通常我不会在WinMain中执行此操作,而是使用url中的示例中的paint。
HTH
答案 1 :(得分:0)