>_<:this is the first using mapping.
>_<:There will be introducing how to do:
- First load bitmap picture return handle give hbmp, as following:
- hbmp=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE);
- there "bg.bmp" must put in the project folder!
- Second if you want draw this picture in the window,I suggest you should create a CompatibleDC, maybe now you don't know why do this, the reason is also hard to explane. You just remember doing is better for showing many pictures reducing Flash.
- hdc=GetDC(hWnd);
- mdc=CreateCompatibleDC(hdc);
- Third when you finish upper two steps, you can use SelectObject(mdc,hbmp); give the picture to CompatibleDC mdc
- Finally just use BitBlt(hdc,0,0,600,450,mdc,0,0,SRCCOPY); copy mdc including to hdc to show in the window.
resourse.h
StdAfx.h
main.cpp
1 #include "stdafx.h" 2 #include "resourse.h" 3 4 #define MAX_LOADSTRING 100 5 6 // Global Variables: 7 HINSTANCE hInst; // current instance 8 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text 9 TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text 10 HBITMAP hbmp; 11 HDC mdc; 12 // Foward declarations of functions included in this code module: 13 ATOM MyRegisterClass(HINSTANCE hInstance); 14 BOOL InitInstance(HINSTANCE, int); 15 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 16 LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); 17 void MyPaint(HDC hdc); 18 //======================================================================================== 19 int APIENTRY WinMain(HINSTANCE hInstance, 20 HINSTANCE hPrevInstance, 21 LPSTR lpCmdLine, 22 int nCmdShow) 23 { 24 // TODO: Place code here. 25 MSG msg; 26 27 MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码; 28 29 // 调用InitInstance函数,进行初始化操作; 30 if (!InitInstance (hInstance, nCmdShow)) 31 { 32 return FALSE; 33 } 34 35 // 消息循环(通过消息循环来获取信息, 36 //进行必要的键盘信息转换而后将控制权交给操作系统, 37 //有操作系统决定哪个程序的消息处理函数处理消息 38 while (GetMessage(&msg, NULL, 0, 0)) //获取程序消息 39 { 40 TranslateMessage(&msg);//转换伪码及字符 41 DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序; 42 } 43 44 return msg.wParam; 45 } 46 //===================================================================================== 47 48 49 50 //============================================================================================= 51 //在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息, 52 //并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。 53 //============================================================================================== 54 ATOM MyRegisterClass(HINSTANCE hInstance) 55 { 56 WNDCLASSEX wcex; //申请一个窗口类别“WNDCLASSEX”和结构”wcex“ 57 //-------------------------------------------------------------- 58 //定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc) 59 //为WNDPROC,类别名称为(lpszClassName)为”fe"; 60 //-------------------------------------------------------------- 61 wcex.cbSize = sizeof(WNDCLASSEX); 62 63 wcex.style = CS_HREDRAW | CS_VREDRAW; 64 wcex.lpfnWndProc = (WNDPROC)WndProc; 65 wcex.cbClsExtra = 0; 66 wcex.cbWndExtra = 0; 67 wcex.hInstance = hInstance; 68 wcex.hIcon = NULL; 69 wcex.hCursor = NULL; 70 wcex.hCursor = LoadCursor(NULL,IDC_ARROW); 71 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 72 wcex.lpszMenuName = NULL; 73 wcex.lpszClassName = "fe"; 74 wcex.hIconSm = NULL; 75 76 return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串 77 //此字符串即为类别名称”fe"; 78 } 79 //============================================================================================ 80 81 82 //============================================================================================ 83 //按照前面所定义的窗口类别来建立并显示实际的程序窗口 84 //============================================================================================ 85 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 86 { 87 HWND hWnd; 88 HDC hdc; 89 hInst = hInstance; // 把instance handle 储存在全局变量中; 90 91 hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW, 92 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 93 //----------------------------------------------- 94 //调用CreateWindow函数来建立一个窗口对象 95 //第一个参数就是窗口建立依据的类别名称 96 //----------------------------------------------- 97 if (!hWnd) 98 { 99 return FALSE;100 }101 //------------------------------------------------102 //设定窗口的位置及窗口的大小,然后绘制显示在设备上103 //-------------------------------------------------104 MoveWindow(hWnd,10,10,600,450,true);//位置及大小105 ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态106 UpdateWindow(hWnd);//将窗口绘制在显示设备上107 108 hdc=GetDC(hWnd);109 mdc=CreateCompatibleDC(hdc);110 111 hbmp=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE);112 //名、类型、大小、加载方式;113 SelectObject(mdc,hbmp);114 115 MyPaint(hdc);116 117 ReleaseDC(hWnd,hdc);118 119 120 return TRUE;121 }122 //============================================================================================123 124 125 //============================================================================================126 //绘图函数,初始化函数中将图片已经放在mdc中了,现在将mdc再贴在窗口DC上127 //============================================================================================128 void MyPaint(HDC hdc)129 {130 BitBlt(hdc,0,0,600,450,mdc,0,0,SRCCOPY);//在窗口位置、大小、原图剪切位131 }132 //============================================================================================133 134 135 //============================================================================================136 //在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型137 //来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个138 //程序都会接收信息,选择性接受、处理;139 //============================================================================================140 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)141 {142 PAINTSTRUCT ps;143 HDC hdc;144 145 switch (message) //判断消息类型146 {147 case WM_PAINT: //窗口重绘制148 hdc = BeginPaint(hWnd, &ps);149 MyPaint(hdc);150 EndPaint(hWnd, &ps);151 break;152 case WM_DESTROY: //处理窗口结束消息153 DeleteDC(mdc);154 DeleteObject(hbmp);155 PostQuitMessage(0);156 break;157 default:158 return DefWindowProc(hWnd, message, wParam, lParam);159 }160 return 0;161 }162 //============================================================================================