- 注册时间
- 2004-11-1
- 最后登录
- 2018-4-24
版主
- 积分
- 548
|
以前看到不少的例子,都是要从WinMain入口,然后自己手动开启GC子。
后来玩GDC的时候,发现用这个方法就很麻烦了。
gdc的gc方面的函数名和dmd不一样,最后link的时候,还得去用gcc的lib,不是一般的麻烦。
后来参考了下dfl的处理办法,可以直接从D的main入口,就是不知道这么做,会不会有什么问题。
(对这些东西背后的原理不熟悉。)
- import win32.windows;
- pragma(lib,"gdi32.lib");
- HINSTANCE Hi;
- HWND Hw;
- WNDCLASSEXW Wc;
- int main(char[][] args)
- {
-     Hi = GetModuleHandleW(null);
-     assert(Hi);
-     Wc.lpszClassName = "MyClass";
-     Wc.cbSize = WNDCLASSEXW.sizeof;
-     Wc.lpfnWndProc = &winProc;
-     Wc.hInstance = Hi;
-     Wc.style = CS_CLASSDC;
-     Wc.hIcon = null;
-     Wc.hCursor = null;
-     Wc.hbrBackground = cast(HBRUSH)GetStockObject(BLACK_BRUSH);
-     Wc.lpszMenuName = null;
-     Wc.cbClsExtra = 0;
-     Wc.cbWndExtra = 0;
-     auto t = RegisterClassExW(&Wc);
-     assert(t);
-     
-     int WindowWidth = 800 + GetSystemMetrics(SM_CXEDGE) + GetSystemMetrics(SM_CXBORDER) + GetSystemMetrics(SM_CXDLGFRAME);
-     int WindowHeight = 600 + GetSystemMetrics(SM_CYEDGE) + GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYDLGFRAME) + GetSystemMetrics(SM_CYCAPTION);
-     int WindowPositionX = GetSystemMetrics(SM_CXSCREEN)/2 - WindowWidth/2;
-     int WindowPositionY = GetSystemMetrics(SM_CYSCREEN)/2 - WindowHeight/2;
-     
-     Hw = CreateWindowExW(WS_EX_APPWINDOW, "MyClass", "一个例子",
-                                WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX,
-                                WindowPositionX, WindowPositionY, WindowWidth, WindowHeight,
-                                null, null, Hi, null);
-     assert(Hw);
-     
-     ShowWindow(Hw,SW_SHOW);
-     
-     auto MSG Msg;
-     
-     while(GetMessageW(&Msg, cast(HWND)null, 0, 0))
-     {
-         TranslateMessage(&Msg);
-         DispatchMessageW(&Msg);
-     }
-     
-     UpdateWindow(Hw);
-     
-     return 0;
- }
- extern(Windows) int winProc(HWND Hwnd, uint Msg, WPARAM Wparam, LPARAM Lparam)
- {
-     switch (Msg)
-     {
-         case WM_CLOSE:
-             DestroyWindow(Hwnd);
-         break;
-         case WM_DESTROY:
-             PostQuitMessage(0);
-         break;
-         
-         case WM_PAINT:
-             showText(Hwnd);
-         break;
-         default:
-         break;
-     }
-     return DefWindowProcW(Hwnd, Msg, Wparam, Lparam);
- }
- void showText(HWND Hwnd)
- {
-     HDC Hdc;
-     PAINTSTRUCT Paint;
-     wchar* str = "只是一个空窗口";
-     Hdc = BeginPaint(Hwnd, &Paint);
-     TextOutW(Hdc, 300, 200, str, 2*str.sizeof);
-     EndPaint(Hwnd, &Paint);
- }
复制代码 需要bindings项目中的win32库,这里面的unicode系的winapi函数才是全的。
存为uft-16le格式后,dmd test.d -L/exet:nt/su:windows:4.0 即可。 |
|