为什么LoadImage了还是没有背景图呢?求大佬帮忙看看

wang3780336 2020-03-29 11:06:14
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
static TCHAR szWindowClass[] = _T("win32app");
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;
HDC hDC;
HDC image_mDC;



LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
//创建 WNDCLASSEX 类型的窗口类结构。 此结构包含关于窗口的信息
//例如应用程序图标、窗口背景色、标题栏中显示的名称、窗口过程函数的名称等。
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style =CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "GameFrame";
wcex.hIconSm = LoadIcon(NULL,MAKEINTRESOURCE(IDI_APPLICATION)); //应用程序小图标

//对已创建的窗口类进行注册。 使用 RegisterClassEx 函数,并将窗口类结构作为参数传递。
if (!RegisterClassEx(&wcex)) {
MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}

// Store instance handle in our global variable
// 将句柄实例存储于全局变量中
hInst = hInstance;

// CreateWindow 函数的参数解释:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
// 返回的HWND是一个窗口的句柄
HWND hWnd = CreateWindow( "GameFrame", "游戏框架", WS_OVERLAPPEDWINDOW^WS_THICKFRAME^WS_MAXIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL );
if (!hWnd) {
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
HBITMAP hBmp=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640, 480, LR_LOADFROMFILE);
hDC = GetDC(hWnd);
image_mDC= CreateCompatibleDC(hDC);
SelectObject(image_mDC,hBmp);
BitBlt(hDC, 0, 0, 640, 480, image_mDC, 0, 0, SRCCOPY);

}


// ShowWindow 函数的参数解释:
// hWnd: CreateWindow函数返回的窗口句柄
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd, nCmdShow);
// UpdateWindow函数用于更新窗口指定的区域
// 如果窗口更新的区域不为空,UpdateWindow函数就发送一个WM_PAINT消息来更新指定窗口的客户区。
// 函数绕过应用程序的消息队列,直接发送WM_PAINT消息给指定窗口的窗口过程
// 如果更新区域为空,则不发送消息。
UpdateWindow(hWnd);

// Main message loop:
// 添加用于侦听操作系统所发送消息的消息循环。
// 当应用程序收到一条消息时,此循环将该消息调度到 WndProc 函数以进行处理。
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg); //翻译消息
DispatchMessage(&msg); //派遣消息
}
return (int) msg.wParam;
}

// // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
// // 目标: 处理主窗体产生的信息
// // WM_PAINT消息代表绘制主窗体
// // WM_DESTROY消息代表投递一个退出消息并返回
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
switch (msg) {
case WM_CREATE:
//执行初始化代码
return(0);
break;
case WM_PAINT:
//要处理 WM_PAINT 消息,首先应调用 BeginPaint
//然后处理所有的逻辑以及在窗口中布局文本、按钮和其他控件等
//然后调用 EndPaint。
hdc = BeginPaint(hWnd, &ps);

// -----------------在这段代码对应用程序进行布局------------------------
// -----------------------布局模块结束----------------------------------
BitBlt(hdc,0,0,640,480,image_mDC,0,0,SRCCOPY);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
//DefWindowProc缺省窗口处理函数
//这个函数是默认的窗口处理函数,我们可以把不关心的消息都丢给它来处理
return DefWindowProc(hWnd, msg, wParam, lParam);
break;
}
return 0;
}
...全文
172 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xsc2001 2020-03-30
  • 打赏
  • 举报
回复
你的这段代码逻辑有问题,加载图片的代码没有执行就return了。把创建image_mDC和加载图片的代码移到WM_CREATE消息处理中。
if (!hWnd) {
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
HBITMAP hBmp=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640, 480, LR_LOADFROMFILE);
hDC = GetDC(hWnd);
image_mDC= CreateCompatibleDC(hDC);
SelectObject(image_mDC,hBmp);
BitBlt(hDC, 0, 0, 640, 480, image_mDC, 0, 0, SRCCOPY);

}
zgl7903 2020-03-30
  • 打赏
  • 举报
回复
引用 楼主 wang3780336 的回复:
if (!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
HBITMAP hBmp=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640, 480, LR_LOADFROMFILE);
hDC = GetDC(hWnd);
image_mDC= CreateCompatibleDC(hDC);
SelectObject(image_mDC,hBmp);
BitBlt(hDC, 0, 0, 640, 480, image_mDC, 0, 0, SRCCOPY);

}

上面的逻辑有问题, 应该是 hWnd 有效时加载位图

if (hWnd)
{
HBITMAP hBmp=(HBITMAP)LoadImage(NULL, TEXT("bg.bmp"),IMAGE_BITMAP,640, 480, LR_LOADFROMFILE);
hDC = GetDC(hWnd);
image_mDC= CreateCompatibleDC(hDC);
SelectObject(image_mDC,hBmp);
BitBlt(hDC, 0, 0, 640, 480, image_mDC, 0, 0, SRCCOPY);
}
else
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}


schlafenhamster 2020-03-29
  • 打赏
  • 举报
回复
image_mDC 初始化 放 case WM_CREATE: 中 试试

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧