windows窗体应用背景图片设置和窗口一样大,提示错误?
展开全部
#include <windows.h>
HBITMAP hbg;
BITMAP bm;
int w,h;
HDC memdc;
char *filename="pic.bmp";//位图文件名
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
case WM_CREATE:
{
hbg=(HBITMAP)LoadImage(GetModuleHandle(0),filename,IMAGE_BITMAP,0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
GetObject(hbg,sizeof(BITMAP),(LPVOID)&bm);//获取位图尺寸
HDC hdc=GetDC(hwnd); //获取设备环境
memdc=CreateCompatibleDC(hdc); //获取内存设备环境
ReleaseDC(hwnd,hdc); //释放设备环境
}
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd,&ps);
SelectObject(memdc,hbg);
BitBlt(ps.hdc,0,0,bm.bmWidth,bm.bmHeight,memdc,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
HBITMAP hbg;
BITMAP bm;
int w,h;
HDC memdc;
char *filename="pic.bmp";//位图文件名
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
case WM_CREATE:
{
hbg=(HBITMAP)LoadImage(GetModuleHandle(0),filename,IMAGE_BITMAP,0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
GetObject(hbg,sizeof(BITMAP),(LPVOID)&bm);//获取位图尺寸
HDC hdc=GetDC(hwnd); //获取设备环境
memdc=CreateCompatibleDC(hdc); //获取内存设备环境
ReleaseDC(hwnd,hdc); //释放设备环境
}
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd,&ps);
SelectObject(memdc,hbg);
BitBlt(ps.hdc,0,0,bm.bmWidth,bm.bmHeight,memdc,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询