tamashii 发表于 2010-6-10 01:55:06

C++的GDI+编程


#include <stdio.h>
#include <windows.h>
#include <gdiplus.h>

using namespace Gdiplus;

HWND g_hWnd;
Graphics * graphics;
Bitmap * bitmap;
Graphics * g_pGraphics;

HDC hDC;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR GdiToken;

char title;

int screenwidth = 640;
int screenheight = 480;


int x = 0, y = 0, a = 0;

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void Render();
void OnMouseMove(WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    WNDCLASS wndclass;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hInstance = hInstance;
    wndclass.lpfnWndProc = WndProc;
    wndclass.lpszClassName = "DLMEngine";
    wndclass.lpszMenuName = NULL;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wndclass);
    int x, y, width, height;
    GetPrivateProfileString("Engine","Title", "Untitled", title, 255, ".\\Config.ini");
    width = GetSystemMetrics(SM_CXDLGFRAME) * 2 + screenwidth;
    height = GetSystemMetrics(SM_CYDLGFRAME) * 2 + screenheight + GetSystemMetrics(SM_CYCAPTION);
    x = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
    y = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
    g_hWnd = CreateWindow("DLMEngine", title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, x, y, width, height, NULL, NULL, hInstance, NULL);
    GdiplusStartup(&GdiToken, &gdiplusStartupInput,NULL);
    hDC = GetDC(g_hWnd);
    g_pGraphics = new Graphics(hDC);
    bitmap = new Bitmap(screenwidth, screenheight);
    graphics = Graphics::FromImage(bitmap);
    graphics->SetTextRenderingHint(TextRenderingHintAntiAlias);
    ShowWindow(g_hWnd, SW_SHOWNORMAL);
    MSG msg;
    while(true) {
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_QUIT) {
                break;
            }
            Render();
            TranslateMessage(&msg);
            DispatchMessage(&msg);
      }
    }
    ReleaseDC(g_hWnd, hDC);
    GdiplusShutdown(GdiToken);
    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch(uMsg) {
      case WM_PAINT :
            Render();
            return 0;
      case WM_MOUSEMOVE :
            OnMouseMove(wParam, lParam);
            return 0;
      case WM_DESTROY :
            PostQuitMessage(0);
            return 0;
      default :
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
}

void Render() {
    Font * font = new Font(L"SimHei", 18);
    graphics->Clear(Color::Black);
    graphics->DrawString(L"文字测试", 4, font, PointF(x, y), new SolidBrush(Color::White));
    g_pGraphics->DrawImage(bitmap, 0, 0);
}

void OnMouseMove(WPARAM wParam, LPARAM lParam) {
    x = LOWORD(lParam);
    y = HIWORD(lParam);
    a = wParam;
}

需要用到库文件 gdipluse.lib

shawind 发表于 2010-6-14 08:57:08

可惜,主程序的结构还是标准的win app结构,没有为游戏特别优化啊。

inffu 发表于 2010-6-14 14:31:30

用来画一个主窗体吗?

tamashii 发表于 2010-8-1 08:11:08

我先来说一下 GDI+ 的特点:
1.支持含有 Alpha 通道的32位图片(例如png)
2.全部面向对象的编程方式
3.支持双缓冲的画面(用一个Bitmap来做后台)
4.图片操作更加方便

shawind 发表于 2010-8-1 11:08:21

是啊,很多绘图形的工具都用GDI+来作了。唯一的不足就是没有硬件加速,作大动态游戏还是有点缺陷。DX11里新加了个D2D,当然比GDI+快那是肯定的,不知道比起GDI+来是否方便。
页: [1]
查看完整版本: C++的GDI+编程