C++ 加载和绘制bmp win32api c++;
我试图使用以下代码在屏幕上显示bmp:(此代码来自一本书,但我无法在编译时显示窗口,仅显示白色窗口)C++ 加载和绘制bmp win32api c++;,c++,winapi,C++,Winapi,我试图使用以下代码在屏幕上显示bmp:(此代码来自一本书,但我无法在编译时显示窗口,仅显示白色窗口) #包括“resource2.h” #包括 #包括 使用名称空间std; HWND窗口1; HWND窗口2; 发生了什么事; HDC手持设备; PAINTSTRUCT-ps; HBITMAP HBITMAP; 位图; HDC-bmhdc; HBITMAP-oldbm; LRESULT回调WindTyp1(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
#包括“resource2.h”
#包括
#包括
使用名称空间std;
HWND窗口1;
HWND窗口2;
发生了什么事;
HDC手持设备;
PAINTSTRUCT-ps;
HBITMAP HBITMAP;
位图;
HDC-bmhdc;
HBITMAP-oldbm;
LRESULT回调WindTyp1(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
{
开关(msg)
{
案例WM_键控:
if(wparam==VK_转义)
{
破坏窗口(窗口1);
}
返回0;
案例WM_油漆:
handledevice=开始喷漆(hwnd和ps);
BitBlt(handledevice,0,0,bitmap.bmWidth,bitmap.bmHeight,bmhdc,0,0,SRCCOPY);
端漆(hwnd和ps);
返回0;
}
返回DefWindowProc(hwnd、msg、wparam、lparam);
}
LRESULT回调WindTyp2(HWND HWND、UINT msg、WPARAM WPARAM、LPARAM LPARAM)
{
开关(msg)
{
案例WM_键控:
如果(wparam==VK_F1)
{
破坏窗口(窗口1);
}
if(wparam==VK_F2)
{
破坏窗口(窗口2);
}
if(wparam==VK_转义)
{
破坏窗口(窗口1);
破坏窗口(窗口2);
}
}
返回DefWindowProc(hwnd、msg、wparam、lparam);
}
int-WINAPI-WinMain(HINSTANCE-histance、HINSTANCE-hprevinstance、PSTR-cmdline、int-showcmd)
{
happ=历史;
味精;
hbitmap=LoadBitmap(happ,MAKEINTRESOURCE(happ,IDB_bitmap 1));
GetObject(hbitmap、sizeof(位图)和位图);
bmhdc=CreateCompatibleDC(handledevice);
选择对象(bmhdc和位图);
//第1类
WNDCLASS windowstyle1、windowstyle2;
windowstyle1.cbClsExtra=0;
windowstyle1.cbWndExtra=0;
windowstyle1.hbrBackground=(HBRUSH)::GetStockObject(白色画笔);
windowstyle1.hCursor=::加载光标(0,IDC_箭头);
windowstyle1.hIcon=::加载图标(0,IDI_应用程序);
windowstyle1.hInstance=histance;
windowstyle1.lpfnWndProc=WindTyp1;
windowstyle1.lpszClassName=“类1”;
windowstyle1.lpszMenuName=0;
windowstyle1.style=CS_HREDRAW | CS_VREDRAW;
//第2类
windowstyle2.cbClsExtra=0;
windowstyle2.cbWndExtra=0;
windowstyle2.hbrBackground=(HBRUSH)::GetStockObject(黑色画笔);
windowstyle2.hCursor=::加载光标(0,IDC_箭头);
windowstyle2.hIcon=::加载图标(0,IDI_应用程序);
windowstyle2.hInstance=histance;
windowstyle2.lpfnWndProc=WindTyp2;
windowstyle2.lpszClassName=“Class 2”;
windowstyle2.lpszMenuName=0;
windowstyle2.style=CS_HREDRAW | CS_VREDRAW;
//安巴斯船级社
注册表类(&windowstyle1);
注册表类(&windowstyle2);
//克雷尔·文塔纳斯
window1=::CreateWindow(“类别1”,“文塔纳布兰卡”,WS_OVERLAPPEDWINDOW,0,014001000,0,0,happ,0);
如果(window1==0)
::MessageBox(0,“创建窗口失败错误”,“错误”,0);
//显示和更新
ShowWindow(window1,true);
更新窗口(窗口1);
//消息循环
零内存(&msg,sizeof(msg));
while(GetMessage(&msg,0,0,0))
{
翻译信息(&msg);
发送消息(&msg);
}
}
你让自己的生活变得更加艰难。您需要使用具有该样式的窗口来完成此操作
我意识到这不是对你问题的间接回答,但我提供了它,因为它将使你的任务更简单-让系统为你做繁重的工作 看看这个例子 在WinMain中,您使用的是handledevice,它尚未初始化,因此选择位图会导致问题,但它可能为0,因此将使用桌面DC。通常我不会在WinMain中这样做,而是在paint中这样做,就像url中的示例一样
hth不错,但我这样做是因为我在自学,所以如果我加载bmp,在窗口中显示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);
}
}
bmhdc=CreateCompatibleDC(handledevice);