MOOC清华《VC++面向对象与可视化程序设计》第5章:位图资源例程

来源:互联网 发布:java 获取文件路径was 编辑:程序博客网 时间:2024/05/24 07:07
#include <windows.h>#include <string.h>#include <stdlib.h>#include <stdio.h>LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);//定义变量HDC hdc;//设备环境句柄HDC hdcmem;//内存设备环境句柄HBITMAP hBm;//位图句柄BITMAP bm;//BITMAP结构int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow) {   HWND hwnd ;   MSG Msg ;   WNDCLASS wndclass ;   char lpszClassName[] = "位图";   char lpszTitle[]= "Example_For_BitMap";   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_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL) ;   hBm=LoadBitmap(hInstance,"pic5");//加载位图   GetObject( hBm, sizeof( BITMAP), (LPVOID)&bm) ;//获取位图尺寸   ShowWindow( hwnd, nCmdShow) ;   UpdateWindow(hwnd);    while(GetMessage(&Msg, NULL, 0, 0)){TranslateMessage( &Msg) ;DispatchMessage( &Msg) ;}   return Msg.wParam;}LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM  wParam,LPARAM  lParam){PAINTSTRUCT ps;switch(message){case WM_CREATE:hdc=GetDC(hwnd);//获取设备环境句柄hdcmem=CreateCompatibleDC(hdc);//获取内存设备环境句柄ReleaseDC(hwnd,hdc);//释放设备环境句柄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;}


//{{NO_DEPENDENCIES}}// Microsoft Visual C++ generated include file.// Used by 001.rc// Next default values for new objects// #ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_VALUE        101#define _APS_NEXT_COMMAND_VALUE         40001#define _APS_NEXT_CONTROL_VALUE         1001#define _APS_NEXT_SYMED_VALUE           101#endif#endif

pic5 BITMAP E:\Visual_Cpp_Files\chap5_Project2\chap5_Project2\pic5_2.bmp//定义位图资源


阅读全文
0 0
原创粉丝点击