1)创建位图步骤,首先 我们需要创建窗口的基本代码,代码展示:
1 #include<Windows.h> 2 #include"resource.h" 3 4 5 //这个叫 窗口消息处理函数 6 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 7 { 8 switch(uMsg) 9 { 10 case WM_CLOSE://点那个×,窗口关闭 11 ::PostQuitMessage(0);//消息推出 12 break; 13 14 15 } 16 return ::DefWindowProc( hWnd, uMsg, wParam, lParam); 17 } 18 19 20 int CALLBACK WinMain(HINSTANCE hIstance, 21 HINSTANCE hPreInstance, 22 LPSTR pCmdLine, 23 int nCmdShow) 24 { 25 26 HBRUSH hBrush=::CreateSolidBrush(RGB(0,0,255)); 27 28 29 //1.设计---->就是给WNDCLASSEX结构体初始化(结构体有12个) 30 WNDCLASSEX wndclass; 31 wndclass.cbClsExtra=0; 32 wndclass.cbWndExtra=0;//这两个是确定是否要分配额外的空间 33 wndclass.cbSize=sizeof(WNDCLASSEX); 34 wndclass.hbrBackground=hBrush; 35 wndclass.hCursor=NULL;//光标 36 //因为上面的那个LoadCUrsor是需要字符串,但是那个IDC_CURSOR1是一个整型数字,所以 需要用那个宏给转定义一下 37 wndclass.hIcon=NULL;//窗口图标 38 wndclass.hIconSm=NULL;//窗口左上的图标 39 wndclass.hInstance=hIstance; 40 wndclass.lpfnWndProc=WndProc; 41 wndclass.lpszClassName="wangchao"; 42 wndclass.lpszMenuName=NULL; 43 wndclass.style=CS_HREDRAW|CS_VREDRAW; 44 45 //2.注册 46 if(::RegisterClassEx(&wndclass)==FALSE) 47 { 48 ::MessageBox(NULL,"dhsakfljadsf","提示",MB_OK); 49 50 return 0; 51 } 52 //3.创建 53 54 HWND hWnd=::CreateWindow("wangchao","xiao_hua",WS_OVERLAPPEDWINDOW,100,0,500,500,NULL,NULL,hIstance,NULL); 55 56 if(hWnd==NULL) 57 { 58 ::MessageBox(NULL,"创建失败","提示",MB_OK); 59 return 0; 60 } 61 //4.显式 62 63 ::ShowWindow(hWnd,SW_SHOW); 64 65 //5.消息循环 66 67 MSG msg; 68 69 while(::GetMessage(&msg,0,0,0)) 70 { 71 //第一步先翻译 72 ::TranslateMessage(&msg); 73 //第二部分发 74 ::DispatchMessageA(&msg); 75 76 } 77 78 79 ::DeleteObject(hBrush); 80 }
2)然后 创建位图 步骤:
1)首先是 视图--->资源试图--->Bitmap--->新建
2)然后 新建:
3)可以直接修改位图的大小
4)看我们的那个resource.h中的代码
4)真正的修改部分
5)代码展示:
1 #include<Windows.h> 2 #include"resource.h" 3 4 HBITMAP hBitMap; 5 //这个叫 窗口消息处理函数 6 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 7 { 8 switch(uMsg) 9 { 10 case WM_CLOSE://点那个×,窗口关闭 11 ::PostQuitMessage(0);//消息推出 12 break; 13 case WM_PAINT: 14 HDC hdc=::GetDC(hWnd);//获得当前这个HDC的句柄 15 HDC hdn_jianrong=::CreateCompatibleDC(hdc);//创建一个兼容性HDC 16 SelectObject(hdn_jianrong, hBitMap);//把刚刚创建的那个位图hBitMap给hdc_jianrong 17 BitBlt(hdc,0,0,500,500,hdn_jianrong,0,0,SRCCOPY);//那个位图属性设置 18 ::DeleteDC(hdn_jianrong);//删除刚刚那个兼容性HDC 19 ::ReleaseDC(hWnd,hdc);//释放当前的HDC 20 21 } 22 return ::DefWindowProc( hWnd, uMsg, wParam, lParam); 23 } 24 25 26 int CALLBACK WinMain(HINSTANCE hIstance, 27 HINSTANCE hPreInstance, 28 LPSTR pCmdLine, 29 int nCmdShow) 30 { 31 32 HBRUSH hBrush=::CreateSolidBrush(RGB(0,0,255)); 33 34 35 //1.设计---->就是给WNDCLASSEX结构体初始化(结构体有12个) 36 WNDCLASSEX wndclass; 37 wndclass.cbClsExtra=0; 38 wndclass.cbWndExtra=0;//这两个是确定是否要分配额外的空间 39 wndclass.cbSize=sizeof(WNDCLASSEX); 40 wndclass.hbrBackground=hBrush; 41 wndclass.hCursor=NULL;//光标 42 //因为上面的那个LoadCUrsor是需要字符串,但是那个IDC_CURSOR1是一个整型数字,所以 需要用那个宏给转定义一下 43 wndclass.hIcon=NULL;//窗口图标 44 wndclass.hIconSm=NULL;//窗口左上的图标 45 wndclass.hInstance=hIstance; 46 wndclass.lpfnWndProc=WndProc; 47 wndclass.lpszClassName="wangchao"; 48 wndclass.lpszMenuName=NULL; 49 wndclass.style=CS_HREDRAW|CS_VREDRAW; 50 51 //2.注册 52 if(::RegisterClassEx(&wndclass)==FALSE) 53 { 54 ::MessageBox(NULL,"dhsakfljadsf","提示",MB_OK); 55 56 return 0; 57 } 58 //3.创建 59 60 HWND hWnd=::CreateWindow("wangchao","xiao_hua",WS_OVERLAPPEDWINDOW,100,0,500,500,NULL,NULL,hIstance,NULL); 61 62 if(hWnd==NULL) 63 { 64 ::MessageBox(NULL,"创建失败","提示",MB_OK); 65 return 0; 66 } 67 //4.显式 68 69 ::ShowWindow(hWnd,SW_SHOW); 70 hBitMap=::LoadBitmap(hIstance,MAKEINTRESOURCE(BACKGROUND)); 71 //5.消息循环 72 73 MSG msg; 74 75 while(::GetMessage(&msg,0,0,0)) 76 { 77 //第一步先翻译 78 ::TranslateMessage(&msg); 79 //第二部分发 80 ::DispatchMessageA(&msg); 81 82 } 83 84 85 ::DeleteObject(hBrush); 86 }