WIN32图形编程不能显示位图(附代码)

#include<Windows.h>#include"resource.h"#include<atlimage.h>//CImage的头文件#include<strin... #include <Windows.h>
#include "resource.h"
#include <atlimage.h>//CImage的头文件
#include <string.h>
#include <stdio.h>

#define MAX_ARRAY 50

/////全局变量/////
TCHAR szTitleName[MAX_ARRAY] = TEXT("游戏"); /////标题
TCHAR szClassName[MAX_ARRAY] = TEXT("szClassName"); /////类名
TCHAR szReClaError[MAX_ARRAY] = TEXT("窗口注册失败!");

HDC BGbmpDC = NULL;
HBITMAP BGbmp1 = NULL;
HBITMAP BGbmp2 = NULL;

LRESULT CALLBACK MyWndPoc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd )
{
HWND hWnd;
MSG messag;

/////定义窗口类/////
WNDCLASS wndclass;

wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor = LoadCursor(hInstance,IDC_ARROW);
wndclass.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = MyWndPoc;
wndclass.lpszClassName = szClassName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;

/////注册窗口类/////
if (!RegisterClass(&wndclass))
{
MessageBox(NULL,szReClaError,szClassName,MB_ICONERROR);
}

/////创建窗口类/////
hWnd = CreateWindow(szClassName,
szTitleName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
414,
460,
NULL,
NULL,
hInstance,
NULL);

/////显示更新窗口/////
ShowWindow(hWnd,nShowCmd);
UpdateWindow(hWnd);

/////消息循环/////
while (1)
{
if (!PeekMessage(&messag,NULL,0,0,PM_REMOVE))
{
if (WM_QUIT==messag.message)
{
PostQuitMessage(0);
break;
}
TranslateMessage(&messag);
DispatchMessage(&messag);
}
}

HDC windowDC = GetDC(hWnd);

BGbmpDC = CreateCompatibleDC(windowDC);
BGbmp1 = CreateCompatibleBitmap(windowDC,414,460);
BGbmp2 = (HBITMAP)LoadImage(NULL, MAKEINTRESOURCE(IDB_BG), IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
SelectObject(windowDC,BGbmp2);
BitBlt(BGbmpDC,0,0,414,460,windowDC,0,0,SRCCOPY);
return (int)messag.wParam;
}

/////窗口过程函数/////
LRESULT CALLBACK MyWndPoc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_CREATE:
break;
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
SelectObject(BGbmpDC,BGbmp1);
BitBlt(hdc,0,0,414,460,BGbmpDC,0,0,SRCCOPY);

EndPaint(hWnd,&ps);
ReleaseDC(hWnd,hdc);
return 0;
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_NUMPAD1:
MessageBox(hWnd,szTitleName,szTitleName,MB_OK);
break;
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
break;
}
}
请帮我改正确一下,好希望调试过后,看到的是客户区上一张美丽的位图,而不是黑色背景,附上改正确后的代码最好,追加高分,希望您在百忙之中帮小弟一把,谢谢
展开
 我来答
dark_chris
2010-07-11 · TA获得超过247个赞
知道答主
回答量:74
采纳率:0%
帮助的人:61.4万
展开全部
#include <Windows.h>
#include "resource.h"
#include <atlimage.h>//CImage的头文件
#include <string.h>
#include <stdio.h>

#define MAX_ARRAY 50

/////全局变量/////
TCHAR szTitleName[MAX_ARRAY] = TEXT("游戏"); /////标题
TCHAR szClassName[MAX_ARRAY] = TEXT("szClassName"); /////类名
TCHAR szReClaError[MAX_ARRAY] = TEXT("窗口注册失败!");

HDC BGbmpDC = NULL;
HBITMAP BGbmp1 = NULL;
HBITMAP BGbmp2 = NULL;

LRESULT CALLBACK MyWndPoc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd )
{
HWND hWnd;
MSG messag;

/////定义窗口类/////
WNDCLASS wndclass;

wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor = LoadCursor(hInstance,IDC_ARROW);
wndclass.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = MyWndPoc;
wndclass.lpszClassName = szClassName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;

/////注册窗口类/////
if (!RegisterClass(&wndclass))
{
MessageBox(NULL,szReClaError,szClassName,MB_ICONERROR);
}

/////创建窗口类/////
hWnd = CreateWindow(szClassName,
szTitleName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
414,
460,
NULL,
NULL,
hInstance,
NULL);

HDC windowDC = GetDC(hWnd);

BGbmpDC = CreateCompatibleDC(windowDC);
//BGbmp1 = CreateCompatibleBitmap(windowDC,414,460);
BGbmp2 = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDB_BG), IMAGE_BITMAP, 0, 0,
/*LR_LOADFROMFILE|*/LR_CREATEDIBSECTION);
//SelectObject(BGbmpDC,BGbmp2);
// BitBlt(BGbmpDC,0,0,414,460,windowDC,0,0,SRCCOPY);
/////显示更新窗口/////
ShowWindow(hWnd,nShowCmd);
UpdateWindow(hWnd);

/////消息循环/////
while (1)
{
if (PeekMessage(&messag,NULL,0,0,PM_REMOVE))
{
if (WM_QUIT==messag.message)
{
PostQuitMessage(0);
break;
}
TranslateMessage(&messag);
DispatchMessage(&messag);
}
}

return (int)messag.wParam;
}

/////窗口过程函数/////
LRESULT CALLBACK MyWndPoc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_CREATE:
break;
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
SelectObject(BGbmpDC,BGbmp2);
BitBlt(hdc,0,0,414,460,BGbmpDC,0,0,SRCCOPY);
EndPaint(hWnd,&ps);
ReleaseDC(hWnd,hdc);
return 0;
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_NUMPAD1:
MessageBox(hWnd,szTitleName,szTitleName,MB_OK);
break;
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
break;
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式