Win32 SDK显示图像(GDI+)
来源:互联网 发布:微信加好友软件 编辑:程序博客网 时间:2024/04/28 20:37
转载自:http://www.cnblogs.com/nbsofer/archive/2012/05/12/2497109.html
刚学了GDI+, 发现显示图片很方便, 以前用OleLoadPicture+IPicture接口显示, 别提有多
悲剧了, 现在学了GDI+, 太方便友好了, 哈哈~
提供一个GDI+显示图片的示例供那些不知道怎么用Win32SDK显示图片的新手程序猿们一快速简单的方法.
程序工作方式, 程序启动后, 任意拖动一张图片到窗口即可显示图像, 格式包括但不限于JPG,BMP,PNG,...
预览:
程序代码:
#include <windows.h>#include <gdiplus.h>#pragma comment(lib,"gdiplus")using namespace Gdiplus;//根据图片的宽度和高度更新窗口客户区的大小void set_window_size(HWND hWnd,int width,int height){ RECT rcWindow,rcClient; int border_width,border_height; GetWindowRect(hWnd,&rcWindow); GetClientRect(hWnd,&rcClient); border_width = (rcWindow.right-rcWindow.left) - (rcClient.right-rcClient.left); border_height = (rcWindow.bottom-rcWindow.top) - (rcClient.bottom-rcClient.top); SetWindowPos(hWnd,0,0,0,border_width+width,border_height+height,SWP_NOMOVE|SWP_NOZORDER);}void draw_image(HWND hWnd,wchar_t* file){ HDC hdc; int width,height; //加载图像 Image image(file); if(image.GetLastStatus() != Status::Ok){ MessageBox(hWnd,"图片无效!",NULL,MB_OK); return; } //取得宽度和高度 width = image.GetWidth(); height = image.GetHeight(); //更新窗口大小 set_window_size(hWnd,width,height); hdc = GetDC(hWnd); //绘图 Graphics graphics(hdc); graphics.DrawImage(&image,0,0,width,height); ReleaseDC(hWnd,hdc); return;}LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam){ switch(uMsg) { case WM_DROPFILES://拖动图片文件时进行图像显示 { wchar_t file[MAX_PATH]; DragQueryFileW((HDROP)wParam,0,file,sizeof(file)/sizeof(*file)); draw_image(hWnd,file); DragFinish((HDROP)wParam); return 0; } case WM_LBUTTONDOWN://左键单击时拖动窗体 SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0); return 0; case WM_CREATE: return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,uMsg,wParam,lParam);}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){ MSG msg; WNDCLASSEX wce={0}; HWND hWnd; wce.cbSize = sizeof(wce); wce.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wce.hCursor = LoadCursor(NULL,IDC_ARROW); wce.hIcon = LoadIcon(NULL,IDI_APPLICATION); wce.hInstance = hInstance; wce.lpfnWndProc = WndProc; wce.lpszClassName = "MyClassName"; wce.style = CS_HREDRAW|CS_VREDRAW; if(!RegisterClassEx(&wce)) return 1; char* title = "Win32SDK GDI+ 图像显示示例程序(拖动图片文件到窗口以显示)"; hWnd = CreateWindowEx(0,"MyClassName",title,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); if(hWnd == NULL) return 1; //GdiPlus初始化 ULONG_PTR gdiplusToken; GdiplusStartupInput gdiplusInput; GdiplusStartup(&gdiplusToken,&gdiplusInput,NULL); //窗口接收文件拖放 DragAcceptFiles(hWnd,TRUE); ShowWindow(hWnd,nShowCmd); UpdateWindow(hWnd); while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg); DispatchMessage(&msg); } //GdiPlus 取消初始化 GdiplusShutdown(gdiplusToken); return msg.wParam;}
0 0
- Win32 SDK显示图像(GDI+)
- GDI+ 显示图像
- 深入GDI图像显示
- 深入GDI图像显示
- 用GDI+显示图像
- 深入GDI图像显示
- GDI图像显示
- GDI+[26] 显示图像
- GDI+ 显示DICOM 图像测试
- GDI+(3.图像显示)
- vc win32 gdi+ 怎么显示PNG图片
- Win32 GDI+ 学习笔记(一)之win32显示图片
- WIN32 控件显示RGB 图像 / Mat 图像
- WIN32 控件显示RGB 图像 / Mat 图像
- VB利用GDI+显示PNG图像
- GDI实现图像的简单显示特效
- GDI+(图像的显示和保存)
- gdal读取图像数据,构建gdi+显示
- 面向行的输入 getline()和get()的使用
- hibernate缓存:一级缓存和二级缓存
- 【转载】越洋巨鸢---苏联图-114远程客机
- 将博客搬至CSDN
- Ado.Net Entities Framework实例
- Win32 SDK显示图像(GDI+)
- HDU 4741 Save Labman No.004 (异面直线距离&直线与平面的交点)
- Cocos2d-x适配解决方案及原理分析
- 不要这样说啊
- centos下搭建lnmp1.0,使用putty登录linux
- 各种快排调用模板 c语言版
- 文本文件与二进制文件的区别
- JAVASCRIPT学习的不错的网站(转)
- UVA 11768 Lattice Point or Not(扩展欧几里得)