我写了个程序,位图没有加载上去。
代码:
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
HDC hdc;HDC hdcmem; //设备环境句柄和内存设备环境句柄
HBITMAP hBm; //位图句柄
BITMAP bm; //BITMAP结构int WINAPI WinMain //主函数
( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
wndclass.lpfnWndProc=WndProc;
wndclass.hInstance=hInstance;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =HBRUSH(GetStockObject(WHITE_BRUSH));
wndclass.lpszClassName ="TheMainClass"/* lpszClassName*/;
if( !RegisterClass( &wndclass))
{ MessageBeep(0) ; return FALSE ;}
hwnd=CreateWindow("TheMainClass", "位图演示",
WS_OVERLAPPEDWINDOW,0,0,400,500,0,0,hInstance,0);
hBm=LoadBitmap(NULL,MAKEINTRESOURCE(FOREST)); //加载位图
GetObject(hBm,sizeof(BITMAP),(LPVOID)&bm);//获取位图尺寸
ShowWindow(hwnd, SW_SHOWMAXIMIZED) ;
UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0))
{TranslateMessage( &Msg) ; DispatchMessage( &Msg) ;}
return Msg.wParam;
}
long WINAPI WndProc(HWND hwnd,UINT message,
UINT wParam,LONG lParam) {
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
hdc=GetDC(hwnd); //获取设备环境
hdcmem=CreateCompatibleDC(hdc); //获取内存设备环境
ReleaseDC(hwnd,hdc); //释放设备环境
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SelectObject(hdcmem,hBm); //将位图选入内存设备环境
//调用BitBlt将内存设备环境中的位图拷贝到设备环境中
BitBlt(hdc,180,80,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
EndPaint(hwnd,&ps); //释放设备环境句柄
break;
case WM_DESTROY:
DeleteObject(hBm); //释放位图
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
不知道是什么原因,请帮我解决下
代码:
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
HDC hdc;HDC hdcmem; //设备环境句柄和内存设备环境句柄
HBITMAP hBm; //位图句柄
BITMAP bm; //BITMAP结构int WINAPI WinMain //主函数
( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
wndclass.lpfnWndProc=WndProc;
wndclass.hInstance=hInstance;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =HBRUSH(GetStockObject(WHITE_BRUSH));
wndclass.lpszClassName ="TheMainClass"/* lpszClassName*/;
if( !RegisterClass( &wndclass))
{ MessageBeep(0) ; return FALSE ;}
hwnd=CreateWindow("TheMainClass", "位图演示",
WS_OVERLAPPEDWINDOW,0,0,400,500,0,0,hInstance,0);
hBm=LoadBitmap(NULL,MAKEINTRESOURCE(FOREST)); //加载位图
GetObject(hBm,sizeof(BITMAP),(LPVOID)&bm);//获取位图尺寸
ShowWindow(hwnd, SW_SHOWMAXIMIZED) ;
UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0))
{TranslateMessage( &Msg) ; DispatchMessage( &Msg) ;}
return Msg.wParam;
}
long WINAPI WndProc(HWND hwnd,UINT message,
UINT wParam,LONG lParam) {
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
hdc=GetDC(hwnd); //获取设备环境
hdcmem=CreateCompatibleDC(hdc); //获取内存设备环境
ReleaseDC(hwnd,hdc); //释放设备环境
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SelectObject(hdcmem,hBm); //将位图选入内存设备环境
//调用BitBlt将内存设备环境中的位图拷贝到设备环境中
BitBlt(hdc,180,80,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
EndPaint(hwnd,&ps); //释放设备环境句柄
break;
case WM_DESTROY:
DeleteObject(hBm); //释放位图
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
不知道是什么原因,请帮我解决下
解决方案 »
- 如何在dll中写接口的注释
- VS自定义模板
- 求DDE 服务器端MFC对话框应用程序
- 服务参数为数组时该怎么调用服务?
- Window资源管理器中文件后缀不能是xml:我点或拖xml文件,资源管理器(桌面)崩溃!碰到这个问题N久了,一直不知道原因。谢谢
- CDatabase db;db.OpenEx(_T("DSN=***;UID=××;PWD=;"));中的UID是否必须加?
- 如何实现网络传来的压缩过的视频流的存储
- 一个MFC的对于大神来说超级简单的问题
- 请教,SQLite中的BLOB能分次写入吗,一次写入的话貌似太夸张了一点
- 如何在ActiveX控件中绘制按钮、下拉菜单等控件????
- 新手紧急求助:点击树控件某一节点后,在列表控件里显示一些参数?
- 我是新手 请问为什么我循环调用消息处理会占用99%CPU?
上面就有一个位图的例子~~
long WINAPI WndProc(HWND hwnd,UINT message,
UINT wParam,LONG lParam) {
PAINTSTRUCT ps;
static HINSTANCE hInstance ;
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
hBm=LoadBitmap(hInstance,MAKEINTRESOURCE(FOREST)); //加载位图
//...
这样你在试一下看看~~~
hBm=LoadBitmap(NULL,MAKEINTRESOURCE(FOREST)); //加载位图
放到case WM_CREATE:里面处理~~
同时定义一个static的实例hInstance~~~
然后通过
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
传递参数给LoadBitmap~~
这样应该可以的~~
UINT wParam,LONG lParam) {
PAINTSTRUCT ps;
static HINSTANCE hInstance ;
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
hBm=LoadBitmap(hInstance,MAKEINTRESOURCE(FOREST));//
这个我试了的,还是不行
UINT wParam,LONG lParam) {
PAINTSTRUCT ps;
static HINSTANCE hInstance ;
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
hBm=LoadBitmap(hInstance,MAKEINTRESOURCE(FOREST));//
这个我试了的,还是不行
怎么可能~~
我的怎么可以阿 ??
奇怪了~~~
谢谢你了