一、GDI+ 加载图片
包含的头文件:
#include <gdiplus.h>
using namespace Gdiplus;
相关的函数:
GdiplusStartup( OUT ULONG_PTR *token, const GdiplusStartupInput *input,
OUT GdiplusStartupOutput *output);//初始化GDI+函数库
GdiplusShutdown(ULONG_PTR token);
//清理由Microsoft Windows GDI+使用过的资源。每次调用GdiplusStartup函数都要对应的使用一次该函数来完成清理工作。
加载图像的过程
case WM_PAINT:
{
hdc=GetDC(hWnd);
//定义图形对象
Graphics graphics(hdc);
//加载图像
Image image(L"2017-08-26_012737.png") ; // L 把字符串转换为宽字符集
graphics.DrawImage(&image,0,0,80,80);//后面参数 贴图位置; 大小(压缩,扩展)
ReleaseDC(hWnd,hdc);//释放目标DC
}
详细代码及结果:
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib") //导入GDI+库
//GDI+绘图
//准备:将图片放到与代码的同一级目录下
HINSTANCE GhInstance;
// 窗口消息 处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
HDC HdcMem;
switch(uMsg)
{
case WM_CLOSE: // 点X 窗口关闭的消息
::PostQuitMessage(0); // 发送退出的消息
break;
case WM_PAINT:
{
hdc=GetDC(hWnd);
//定义图形对象
Graphics graphics(hdc);
//加载图像
Image image(L"2017-08-26_012737.png") ; // L 把字符串转换为宽字符集
graphics.DrawImage(&image,0,0,461,387); //后面参数 贴图位置 大小(压缩,扩展)
ReleaseDC(hWnd,hdc);//释放目标DC
}
break;
}
return ::DefWindowProc( hWnd, uMsg, wParam, lParam);
}
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance, LPSTR pCmdLine,int nCmdShow)
{
GhInstance=hInstance;
//调用GDI+开始函数
ULONG_PTR ulToken;
GdiplusStartupInput gdiplusStartupInput;
::GdiplusStartup(&ulToken,&gdiplusStartupInput,NULL) ;
// 1. 设计
WNDCLASSEX wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0; // 是否要分配额外的空间
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.hbrBackground =NULL; // 背景颜色默认
wndclass.hCursor = NULL; // 默认光标
wndclass.hIcon =NULL; //默认图标
wndclass.hIconSm=NULL; // 窗口左上小图标
wndclass.hInstance = hInstance; // 当前实例的句柄
wndclass.lpfnWndProc = WndProc; // 消息处理函数
wndclass.lpszClassName = "123"; // 注册窗口类的名
wndclass.lpszMenuName = NULL; // 菜单名
wndclass.style = CS_HREDRAW|CS_VREDRAW; // 窗口类的样式
// 2. 注册
if(::RegisterClassEx(&wndclass) == FALSE)
{
::MessageBox(NULL,"注册失败","提示",MB_OK);
return 0;
}
// 3. 创建
HWND hWnd = ::CreateWindow(wndclass.lpszClassName,"TEST",WS_OVERLAPPEDWINDOW,100,0,500,500,NULL,NULL,hInstance,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);
// 分发
::DispatchMessage(&msg);
}
//关闭GDI+
GdiplusShutdown(ulToken);
return 0;
}
一、GDI+ 旋转图片
#include <windows.h>
#include <gdiplus.h> // GDI+ 头文件
using namespace std;
using namespace Gdiplus; // GDI+ 命名空间
#pragma comment(lib,"gdiplus.lib") // 加载GDI+ 库
// GDI+ 绘图
HINSTANCE GhInstance;
float nAngle = 10; // 旋转的角度
// 窗口消息 处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_PAINT:
{
}
break;
case WM_TIMER: // 定时器发送的消息
{
if(nAngle == 360)
nAngle = 10;
else
nAngle+=10;
// 旋转图片
HDC dc = ::GetDC(hWnd);
//==============================================
// 定义 图形对象
Graphics graphics(dc);
// 加载图片
Image image(L"bitmap1.bmp"); // L 把字符串 转换成宽字符集
// 获取图片的 宽度和高度
int nWeight = image.GetWidth();
int nHeight = image.GetHeight();
// 设置旋转的中心点(将graphics坐标原点移到矩形中心点)
graphics.TranslateTransform(float(nWeight/2),float(nHeight/2));
// 旋转(沿当前原点)
graphics.RotateTransform(nAngle);
//绘图(!!!-(nWeight/2),-(nHeight/2)!!)
graphics.DrawImage(&image,-(nWeight/2),-(nHeight/2),nWeight,nHeight);
//==============================================
::ReleaseDC(hWnd,dc);
}
break;
case WM_CREATE: // 窗口创建的时候发送 初始化的过程
::SetTimer(hWnd,1,10,NULL); // 启动定时器
break;
case WM_CLOSE: // 点X 窗口关闭的消息
::PostQuitMessage(0); // 发送退出的消息
break;
}
return ::DefWindowProc( hWnd, uMsg, wParam, lParam);
}
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance, LPSTR pCmdLine,int nCmdShow)
{
GhInstance=hInstance;
//调用GDI+开始函数
ULONG_PTR ulToken;
GdiplusStartupInput gdiplusStartupInput;
::GdiplusStartup(&ulToken,&gdiplusStartupInput,NULL) ;
// 1. 设计
WNDCLASSEX wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0; // 是否要分配额外的空间
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.hbrBackground =NULL; // 背景颜色默认
wndclass.hCursor = NULL; // 默认光标
wndclass.hIcon =NULL; //默认图标
wndclass.hIconSm=NULL; // 窗口左上小图标
wndclass.hInstance = hInstance; // 当前实例的句柄
wndclass.lpfnWndProc = WndProc; // 消息处理函数
wndclass.lpszClassName = "123"; // 注册窗口类的名
wndclass.lpszMenuName = NULL; // 菜单名
wndclass.style = CS_HREDRAW|CS_VREDRAW; // 窗口类的样式
// 2. 注册
if(::RegisterClassEx(&wndclass) == FALSE)
{
::MessageBox(NULL,"注册失败","提示",MB_OK);
return 0;
}
// 3. 创建
HWND hWnd = ::CreateWindow(wndclass.lpszClassName,"TEST",WS_OVERLAPPEDWINDOW,100,0,500,500,NULL,NULL,hInstance,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);
// 分发
::DispatchMessage(&msg);
}
//关闭GDI+
GdiplusShutdown(ulToken);
return 0;
}
箭头在旋转着呢^_^