我写了个程序,位图没有加载上去。
代码:
#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;
}
不知道是什么原因,请帮我解决下

解决方案 »

  1.   

    把hdcmem=CreateCompatibleDC(hdc);放到WM_PAINT中去
      

  2.   

    楼主参考一下WINDOWS程序设计~
    上面就有一个位图的例子~~
      

  3.   

    这样改:
    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)); //加载位图
    //...
    这样你在试一下看看~~~
      

  4.   

    就是将主函数里面的
    hBm=LoadBitmap(NULL,MAKEINTRESOURCE(FOREST)); //加载位图
    放到case WM_CREATE:里面处理~~
    同时定义一个static的实例hInstance~~~
    然后通过
    hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
    传递参数给LoadBitmap~~
    这样应该可以的~~
      

  5.   

    加载的是自己的位图,LoadBitmap第一个参数不能为空!应该为应用程序实例句柄hInstance。
      

  6.   

    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));//
    这个我试了的,还是不行
      

  7.   

    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));//
    这个我试了的,还是不行
    怎么可能~~
    我的怎么可以阿 ??
    奇怪了~~~
      

  8.   

    [email protected]
    谢谢你了