gpt4 book ai didi

c++ - 加载和绘制 bmp win32api c++

转载 作者:行者123 更新时间:2023-11-30 04:34:36 28 4
gpt4 key购买 nike

我正在尝试使用这段代码在屏幕上显示一个 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);
}
}

最佳答案

看看这个例子 http://www.functionx.com/win32/Lesson13.htm

bmhdc=CreateCompatibleDC(handledevice);

在 WinMain 中,您正在使用尚未初始化的 handledevice,因此选择位图会导致问题,但它可能为 0,因此它会使用桌面 DC。通常我不会在 WinMain 中这样做,而是像 url 中的示例那样在绘画中这样做。

第一个

关于c++ - 加载和绘制 bmp win32api c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882787/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com