幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3243|回复: 4

[通用编程] C++的GDI+编程

[复制链接]

24

主题

117

帖子

1274

积分

⑥精研

积分
1274
QQ
发表于 2010-6-10 01:55:06 | 显示全部楼层 |阅读模式
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <gdiplus.h>
  4. using namespace Gdiplus;
  5. HWND g_hWnd;
  6. Graphics * graphics;
  7. Bitmap * bitmap;
  8. Graphics * g_pGraphics;
  9. HDC hDC;
  10. GdiplusStartupInput gdiplusStartupInput;
  11. ULONG_PTR GdiToken;
  12. char title[255];
  13. int screenwidth = 640;
  14. int screenheight = 480;
  15. int x = 0, y = 0, a = 0;
  16. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  17. void Render();
  18. void OnMouseMove(WPARAM wParam, LPARAM lParam);
  19. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
  20.     WNDCLASS wndclass;
  21.     wndclass.cbClsExtra = 0;
  22.     wndclass.cbWndExtra = 0;
  23.     wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
  24.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  25.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  26.     wndclass.hInstance = hInstance;
  27.     wndclass.lpfnWndProc = WndProc;
  28.     wndclass.lpszClassName = "DLMEngine";
  29.     wndclass.lpszMenuName = NULL;
  30.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  31.     RegisterClass(&wndclass);
  32.     int x, y, width, height;
  33.     GetPrivateProfileString("Engine",  "Title", "Untitled", title, 255, ".\\Config.ini");
  34.     width = GetSystemMetrics(SM_CXDLGFRAME) * 2 + screenwidth;
  35.     height = GetSystemMetrics(SM_CYDLGFRAME) * 2 + screenheight + GetSystemMetrics(SM_CYCAPTION);
  36.     x = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
  37.     y = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
  38.     g_hWnd = CreateWindow("DLMEngine", title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, x, y, width, height, NULL, NULL, hInstance, NULL);
  39.     GdiplusStartup(&GdiToken, &gdiplusStartupInput,NULL);
  40.     hDC = GetDC(g_hWnd);
  41.     g_pGraphics = new Graphics(hDC);
  42.     bitmap = new Bitmap(screenwidth, screenheight);
  43.     graphics = Graphics::FromImage(bitmap);
  44.     graphics->SetTextRenderingHint(TextRenderingHintAntiAlias);
  45.     ShowWindow(g_hWnd, SW_SHOWNORMAL);
  46.     MSG msg;
  47.     while(true) {
  48.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  49.             if (msg.message == WM_QUIT) {
  50.                 break;
  51.             }
  52.             Render();
  53.             TranslateMessage(&msg);
  54.             DispatchMessage(&msg);
  55.         }
  56.     }
  57.     ReleaseDC(g_hWnd, hDC);
  58.     GdiplusShutdown(GdiToken);
  59.     return 0;
  60. }
  61. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  62.     switch(uMsg) {
  63.         case WM_PAINT :
  64.             Render();
  65.             return 0;
  66.         case WM_MOUSEMOVE :
  67.             OnMouseMove(wParam, lParam);
  68.             return 0;
  69.         case WM_DESTROY :
  70.             PostQuitMessage(0);
  71.             return 0;
  72.         default :
  73.             return DefWindowProc(hWnd, uMsg, wParam, lParam);
  74.     }
  75. }
  76. void Render() {
  77.     Font * font = new Font(L"SimHei", 18);
  78.     graphics->Clear(Color::Black);
  79.     graphics->DrawString(L"文字测试", 4, font, PointF(x, y), new SolidBrush(Color::White));
  80.     g_pGraphics->DrawImage(bitmap, 0, 0);
  81. }
  82. void OnMouseMove(WPARAM wParam, LPARAM lParam) {
  83.     x = LOWORD(lParam);
  84.     y = HIWORD(lParam);
  85.     a = wParam;
  86. }
复制代码
需要用到库文件 gdipluse.lib
Tamashii是啥意思? 魂! ======================= 我真是败给C++的面向对象了啊……
回复

使用道具 举报

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
发表于 2010-6-14 08:57:08 | 显示全部楼层
可惜,主程序的结构还是标准的win app结构,没有为游戏特别优化啊。
え~え~お!!!
回复 支持 反对

使用道具 举报

0

主题

29

帖子

246

积分

③业余

积分
246
发表于 2010-6-14 14:31:30 | 显示全部楼层
用来画一个主窗体吗?
回复 支持 反对

使用道具 举报

24

主题

117

帖子

1274

积分

⑥精研

积分
1274
QQ
 楼主| 发表于 2010-8-1 08:11:08 | 显示全部楼层
我先来说一下 GDI+ 的特点:
1.支持含有 Alpha 通道的32位图片(例如png)
2.全部面向对象的编程方式
3.支持双缓冲的画面(用一个Bitmap来做后台)
4.图片操作更加方便
Tamashii是啥意思? 魂! ======================= 我真是败给C++的面向对象了啊……
回复 支持 反对

使用道具 举报

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
发表于 2010-8-1 11:08:21 | 显示全部楼层
是啊,很多绘图形的工具都用GDI+来作了。唯一的不足就是没有硬件加速,作大动态游戏还是有点缺陷。DX11里新加了个D2D,当然比GDI+快那是肯定的,不知道比起GDI+来是否方便。
え~え~お!!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-4-19 07:49 , Processed in 0.018563 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表