Win32加载图片到程序中

来源:互联网 发布:菜鸟侦探挑战数据分析 编辑:程序博客网 时间:2024/04/29 06:37
 
//.rc
pic BITMAP "pic.bmp"

 

//.cpp
#include<windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

HBITMAP hBm;
BITMAP bm;
int iWindowWidth;
int iWindowHeight;
HDC hDC;
HDC hdcmem;
PAINTSTRUCT ps;

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInst,
                   LPSTR     lpszCmdLine,
                   
int       nCmdShow)
{
    HWND hwnd;
    MSG Msg;

    
//用HBITMAP指向资源内的图片
    hBm = LoadBitmap(hInstance,"pic");
    
//读取该图片到BITMAP结构体中
    GetObject(hBm,sizeof(BITMAP),&bm);
    
//根据图片大小获取窗口大小
    iWindowWidth = bm.bmWidth;
    iWindowHeight 
= bm.bmHeight;

    WNDCLASS wndclass;
    
char lpszClassName[]="窗口";
    
char lpszTitle[]="窗口示例程序";
    wndclass.style
=0;
    wndclass.lpfnWndProc
=WndProc;
    wndclass.cbClsExtra
=0;
    wndclass.cbWndExtra
=0;
    wndclass.hInstance
=hInstance;
    wndclass.hIcon
=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor
=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground
=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName
=NULL;
    wndclass.lpszClassName
=lpszClassName;
    
if(! RegisterClass(&wndclass))
    {
        MessageBeep(
0);
        
return FALSE;
    }
    hwnd
=CreateWindow(lpszClassName,
        lpszTitle,
        WS_CAPTION  
|WS_SYSMENU ,
        CW_USEDEFAULT,CW_USEDEFAULT,
        iWindowWidth,iWindowHeight,
        NULL,
        NULL,
        hInstance,
        NULL);
    ShowWindow(hwnd,nCmdShow);
    UpdateWindow(hwnd);
    
while(GetMessage(&Msg,NULL,0,0))
    {
        TranslateMessage(
&Msg);
        DispatchMessage(
&Msg);
    }
    
return (int)Msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM
                         IParam)
{
    
switch(message)
    {
    
case WM_CREATE:
        
//获取设备描述表
        hDC = GetDC(hwnd);
        
//在内存中创建与hDC相同的设备描述表
        hdcmem = CreateCompatibleDC(hDC);
        
//释放设备描述表
        ReleaseDC(hwnd,hDC);
        
break;
    
case WM_PAINT:
        hDC 
= BeginPaint(hwnd,&ps);
        
//选择用图片进行绘制
        SelectObject(hdcmem,hBm);
        
//显示图片从hdcmem到hDC
        BitBlt(hDC,0,0,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
        EndPaint(hwnd,
&ps);
        
break;
    
case WM_DESTROY:
        PostQuitMessage(
0);
    
default:
        
return DefWindowProc(hwnd,message,wParam,IParam);
    }
    
return(0);
}
原创粉丝点击