PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/win32_direct.d

http://github.com/AndrejMitrovic/cairoDSamples
D | 182 lines | 136 code | 37 blank | 9 comment | 5 complexity | d5c4193944b543121a82988782d4988d MD5 | raw file
  1module win32_direct;
  2
  3/+
  4 +           Copyright Andrej Mitrovic 2011.
  5 +  Distributed under the Boost Software License, Version 1.0.
  6 +     (See accompanying file LICENSE_1_0.txt or copy at
  7 +           http://www.boost.org/LICENSE_1_0.txt)
  8 +/
  9
 10// note: there's some kind of bug in cairo or in my understanding of drawing directly to GDI,
 11// this doesn't draw properly (when using DrawCairoRectangle) when a window is overlayed over our app window.
 12
 13import core.runtime;
 14import std.stdio;
 15import std.random;
 16import std.utf;
 17
 18pragma(lib, "gdi32.lib");
 19import win32.windef;
 20import win32.winuser;
 21import win32.wingdi;
 22
 23string appName     = "app";
 24string description = "";
 25
 26import cairo.cairo;
 27import cairo.win32;
 28
 29alias cairo.cairo.RGB RGB;
 30
 31extern (Windows)
 32int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
 33{
 34    int result;
 35    void exceptionHandler(Throwable e) { throw e; }
 36
 37    try
 38    {
 39        Runtime.initialize(&exceptionHandler);
 40        result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
 41        Runtime.terminate(&exceptionHandler);
 42    }
 43    catch (Throwable o)
 44    {
 45        MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
 46        result = 0;
 47    }
 48
 49    return result;
 50}
 51
 52int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
 53{
 54    HWND hwnd;
 55    MSG  msg;
 56    WNDCLASS wndclass;
 57
 58    wndclass.style         = CS_HREDRAW | CS_VREDRAW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
 59    wndclass.lpfnWndProc   = &WndProc;
 60    wndclass.cbClsExtra    = 0;
 61    wndclass.cbWndExtra    = 0;
 62    wndclass.hInstance     = hInstance;
 63    wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
 64    wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
 65    wndclass.hbrBackground = null;
 66    wndclass.lpszMenuName  = appName.toUTF16z;
 67    wndclass.lpszClassName = appName.toUTF16z;
 68
 69    if (!RegisterClass(&wndclass))
 70    {
 71        MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
 72        return 0;
 73    }
 74
 75    hwnd = CreateWindow(appName.toUTF16z,              // window class name
 76                        description.toUTF16z,          // window caption
 77                        WS_OVERLAPPEDWINDOW,           // window style
 78                        CW_USEDEFAULT,                 // initial x position
 79                        CW_USEDEFAULT,                 // initial y position
 80                        CW_USEDEFAULT,                 // initial x size
 81                        CW_USEDEFAULT,                 // initial y size
 82                        NULL,                          // parent window handle
 83                        NULL,                          // window menu handle
 84                        hInstance,                     // program instance handle
 85                        NULL);                         // creation parameters
 86
 87    ShowWindow(hwnd, iCmdShow);
 88    UpdateWindow(hwnd);
 89
 90    while (GetMessage(&msg, NULL, 0, 0))
 91    {
 92        TranslateMessage(&msg);
 93        DispatchMessage(&msg);
 94    }
 95
 96    return msg.wParam;
 97}
 98
 99extern (Windows)
100LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
101{
102    switch (message)
103    {
104        case WM_CREATE:
105        {
106            window = new Window();
107            return 0;
108        }
109        
110        default:
111    }
112    
113    if (window)
114        return window.process(hwnd, message, wParam, lParam);
115    else
116        return DefWindowProc(hwnd, message, wParam, lParam);
117}
118
119Window window;
120
121class Window
122{
123    int cxClient, cyClient;
124
125    LRESULT process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
126    {
127        switch (message)
128        {
129            case WM_DESTROY:
130                PostQuitMessage(0);
131                return 0;
132
133            case WM_PAINT:
134                OnPaint(hwnd, message, wParam, lParam);
135                return 0;
136
137            case WM_SIZE:
138                cxClient = LOWORD(lParam);
139                cyClient = HIWORD(lParam);                
140                return 0;
141            
142            default:
143        }
144        
145        return DefWindowProc(hwnd, message, wParam, lParam);    
146    }
147    
148    void OnPaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
149    {
150        PAINTSTRUCT ps;
151        auto hdc = BeginPaint(hwnd, &ps);  // ps.rcPaint
152        
153        //~ DrawGDIRectangle(hdc);
154        DrawCairoRectangle(hdc);
155
156        EndPaint(hwnd, &ps);
157    }
158
159    void DrawCairoRectangle(HDC hdc)
160    {
161        auto surf = new Win32Surface(hdc);
162        auto ctx = Context(surf);
163        
164        ctx.setSourceRGB(0, 0, 1);
165        ctx.paint();
166    }
167    
168    void DrawGDIRectangle(HDC hdc)
169    {
170        alias win32.wingdi.RGB RGB;
171        
172        HBRUSH hBrush;
173        RECT rect;
174
175        SetRect(&rect, 0, 0, cxClient, cyClient);
176        hBrush = CreateSolidBrush(RGB(0, 0, 255));
177        FillRect(hdc, &rect, hBrush);
178        
179        DeleteObject(hBrush);
180    }
181}
182