1)基本代码展示 还是上一个那个总代码:
1 #include<Windows.h> 2 #include<gdiplus.h>//GDI+的头文件 3 using namespace std; 4 using namespace Gdiplus;//GDI+的命名空间 5 6 #pragma comment(lib,"gdiplus.lib")//加载GDI+的库,去导入一个库 7 8 9 //这个叫 窗口消息处理函数 10 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 11 { 12 switch(uMsg) 13 { 14 case WM_CLOSE://点那个×,窗口关闭 15 ::PostQuitMessage(0);//消息推出 16 break; 17 case WM_PAINT: 18 HDC hdc=::GetDC(hWnd);//还是先取得这个窗口的句柄hdc 19 //==================定义图形对象, 20 //而且这个要和我的窗口关联,因为我要往我的窗口hdc中画 21 22 Graphics graphics(hdc);//传入那个hdc,就相当于关联了 23 24 //=================加载图片,就是那个png图片的对象 25 Image image(L"time.png");//L,把字符串转化成宽字符集 26 27 //==================加载完后 要进行贴图了 28 graphics.DrawImage(&image,0,0);// 29 ::ReleaseDC(hWnd,hdc);//取得了 就得释放这个取得的hdc 30 break; 31 32 33 34 } 35 return ::DefWindowProc( hWnd, uMsg, wParam, lParam); 36 } 37 38 39 int CALLBACK WinMain(HINSTANCE hIstance, 40 HINSTANCE hPreInstance, 41 LPSTR pCmdLine, 42 int nCmdShow) 43 { 44 45 //============①要去调用一个开始去做GDI+操作的那一个函数============ 46 ULONG_PTR uLong_ptr; 47 GdiplusStartupInput gdiplusStartupInput; 48 ::GdiplusStartup(&uLong_ptr,&gdiplusStartupInput,NULL); 49 50 51 HBRUSH hBrush=::CreateSolidBrush(RGB(0,0,255)); 52 53 54 //1.设计---->就是给WNDCLASSEX结构体初始化(结构体有12个) 55 WNDCLASSEX wndclass; 56 wndclass.cbClsExtra=0; 57 wndclass.cbWndExtra=0;//这两个是确定是否要分配额外的空间 58 wndclass.cbSize=sizeof(WNDCLASSEX); 59 wndclass.hbrBackground=hBrush; 60 wndclass.hCursor=NULL;//光标 61 //因为上面的那个LoadCUrsor是需要字符串,但是那个IDC_CURSOR1是一个整型数字,所以 需要用那个宏给转定义一下 62 wndclass.hIcon=NULL;//窗口图标 63 wndclass.hIconSm=NULL;//窗口左上的图标 64 wndclass.hInstance=hIstance; 65 wndclass.lpfnWndProc=WndProc; 66 wndclass.lpszClassName="wangchao"; 67 wndclass.lpszMenuName=NULL; 68 wndclass.style=CS_HREDRAW|CS_VREDRAW; 69 70 //2.注册 71 if(::RegisterClassEx(&wndclass)==FALSE) 72 { 73 ::MessageBox(NULL,"dhsakfljadsf","提示",MB_OK); 74 75 return 0; 76 } 77 //3.创建 78 79 HWND hWnd=::CreateWindow("wangchao","xiao_hua",WS_OVERLAPPEDWINDOW,100,0,500,500,NULL,NULL,hIstance,NULL); 80 81 if(hWnd==NULL) 82 { 83 ::MessageBox(NULL,"创建失败","提示",MB_OK); 84 return 0; 85 } 86 //4.显式 87 88 ::ShowWindow(hWnd,SW_SHOW); 89 90 //5.消息循环 91 92 MSG msg; 93 94 while(::GetMessage(&msg,0,0,0)) 95 { 96 //第一步先翻译 97 ::TranslateMessage(&msg); 98 //第二部分发 99 ::DispatchMessageA(&msg); 100 101 } 102 103 104 ::DeleteObject(hBrush); 105 //================②游戏关闭 我要关闭GDI+ 106 //这个的变量为啥是这个,看帮助文档来的 107 ::GdiplusShutdown(uLong_ptr); 108 return 0; 109 }
2)当前目录信息:
3)我还可以用一个bmp格式的图片,按照以前的步骤 我画一个bmp格式的图片:
4)结果展示:
5) 我还可以得到图片的大小信息:
6)所有代码展示:
#include<Windows.h> #include<gdiplus.h>//GDI+的头文件 using namespace std; using namespace Gdiplus;//GDI+的命名空间 #pragma comment(lib,"gdiplus.lib")//加载GDI+的库,去导入一个库 float nAngle=10; //这个叫 窗口消息处理函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_CLOSE://点那个×,窗口关闭 ::PostQuitMessage(0);//消息推出 break; case WM_PAINT: break; case WM_TIMER: { HDC hdc=::GetDC(hWnd);//还是先取得这个窗口的句柄hdc //==================定义图形对象, //而且这个要和我的窗口关联,因为我要往我的窗口hdc中画 Graphics graphics(hdc);//传入那个hdc,就相当于关联了 //=================加载图片,就是那个png图片的对象 Image image(L"time.png");//L,把字符串转化成宽字符集 int nWidth=image.GetWidth(); int nHeight=image.GetHeight(); //设置旋转的中心点 graphics.TranslateTransform(float(image.GetWidth()/2),float(image.GetHeight()/2));//(以图片的中心点做为旋转点) //旋转 graphics.RotateTransform(nAngle);//他需要一个角度,所以在主函数上面定义一个变量nAngle //==================加载完后 要进行贴图了 graphics.DrawImage(&image,0,0);// ::ReleaseDC(hWnd,hdc);//取得了 就得释放这个取得的hdc } break; case WM_CREATE: ::SetTimer(hWnd,1,100,NULL); } return ::DefWindowProc( hWnd, uMsg, wParam, lParam); } int CALLBACK WinMain(HINSTANCE hIstance, HINSTANCE hPreInstance, LPSTR pCmdLine, int nCmdShow) { //============①要去调用一个开始去做GDI+操作的那一个函数============ ULONG_PTR uLong_ptr; GdiplusStartupInput gdiplusStartupInput; ::GdiplusStartup(&uLong_ptr,&gdiplusStartupInput,NULL); HBRUSH hBrush=::CreateSolidBrush(RGB(0,0,255)); //1.设计---->就是给WNDCLASSEX结构体初始化(结构体有12个) WNDCLASSEX wndclass; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0;//这两个是确定是否要分配额外的空间 wndclass.cbSize=sizeof(WNDCLASSEX); wndclass.hbrBackground=hBrush; wndclass.hCursor=NULL;//光标 //因为上面的那个LoadCUrsor是需要字符串,但是那个IDC_CURSOR1是一个整型数字,所以 需要用那个宏给转定义一下 wndclass.hIcon=NULL;//窗口图标 wndclass.hIconSm=NULL;//窗口左上的图标 wndclass.hInstance=hIstance; wndclass.lpfnWndProc=WndProc; wndclass.lpszClassName="wangchao"; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW|CS_VREDRAW; //2.注册 if(::RegisterClassEx(&wndclass)==FALSE) { ::MessageBox(NULL,"dhsakfljadsf","提示",MB_OK); return 0; } //3.创建 HWND hWnd=::CreateWindow("wangchao","xiao_hua",WS_OVERLAPPEDWINDOW,100,0,500,500,NULL,NULL,hIstance,NULL); if(hWnd==NULL) { ::MessageBox(NULL,"创建失败","提示",MB_OK); return 0; } //4.显式 ::ShowWindow(hWnd,SW_SHOW); //5.消息循环 MSG msg; while(::GetMessage(&msg,0,0,0)) { //第一步先翻译 ::TranslateMessage(&msg); //第二部分发 ::DispatchMessageA(&msg); } ::DeleteObject(hBrush); //================②游戏关闭 我要关闭GDI+ //这个的变量为啥是这个,看帮助文档来的 ::GdiplusShutdown(uLong_ptr); return 0; }
7)结果展示: