1 module winsamp; 2 3 /+ Compile with: 4 + dmd winsamp winsamp.def 5 + or: 6 + dmd winsamp -L-Subsystem:Windows 7 + 8 + 64 bit version: 9 + dmd -m64 winsamp -L-Subsystem:Windows user32.lib 10 +/ 11 12 pragma(lib, "gdi32.lib"); 13 import core.runtime; 14 import core.sys.windows.windef; 15 import core.sys.windows.wingdi; 16 import core.sys.windows.winuser; 17 import std.string; 18 19 enum IDC_BTNCLICK = 101; 20 enum IDC_BTNDONTCLICK = 102; 21 22 extern(Windows) 23 int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) 24 { 25 int result; 26 27 try 28 { 29 Runtime.initialize(); 30 result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow); 31 Runtime.terminate(); 32 } 33 catch (Throwable e) 34 { 35 MessageBoxA(null, e.toString().toStringz, "Error", MB_OK | MB_ICONEXCLAMATION); 36 result = 0; 37 } 38 39 return result; 40 } 41 42 int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) 43 { 44 wstring caption = "The Hello Program"; 45 wstring className = "DWndClass"; 46 HWND hWnd, btnClick, btnDontClick; 47 MSG msg; 48 WNDCLASSW wndclass; 49 50 wndclass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; 51 wndclass.lpfnWndProc = &WindowProc; 52 wndclass.cbClsExtra = 0; 53 wndclass.cbWndExtra = 0; 54 wndclass.hInstance = hInstance; 55 wndclass.hIcon = LoadIconW(null, IDI_APPLICATION); 56 wndclass.hCursor = LoadCursorW(null, IDC_CROSS); 57 wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH); 58 wndclass.lpszMenuName = null; 59 wndclass.lpszClassName = className.ptr; 60 61 if (!RegisterClassW(&wndclass)) 62 { 63 MessageBoxW(null, "Couldn't register Window Class!", caption.ptr, MB_ICONERROR); 64 return 0; 65 } 66 67 hWnd = CreateWindowW(className.ptr, // window class name 68 caption.ptr, // window caption 69 WS_THICKFRAME | 70 WS_MAXIMIZEBOX | 71 WS_MINIMIZEBOX | 72 WS_SYSMENU | 73 WS_VISIBLE, // window style 74 CW_USEDEFAULT, // initial x position 75 CW_USEDEFAULT, // initial y position 76 600, // initial x size 77 400, // initial y size 78 HWND_DESKTOP, // parent window handle 79 null, // window menu handle 80 hInstance, // program instance handle 81 null); // creation parameters 82 83 if (hWnd is null) 84 { 85 MessageBoxW(null, "Couldn't create window.", caption.ptr, MB_ICONERROR); 86 return 0; 87 } 88 89 btnClick = CreateWindowW("BUTTON", "Click Me", WS_CHILD | WS_VISIBLE, 90 0, 0, 100, 25, hWnd, cast(HMENU)IDC_BTNCLICK, hInstance, null); 91 92 btnDontClick = CreateWindowW("BUTTON", "DON'T CLICK!", WS_CHILD | WS_VISIBLE, 93 110, 0, 100, 25, hWnd, cast(HMENU)IDC_BTNDONTCLICK, hInstance, null); 94 95 ShowWindow(hWnd, iCmdShow); 96 UpdateWindow(hWnd); 97 98 while (GetMessageW(&msg, null, 0, 0)) 99 { 100 TranslateMessage(&msg); 101 DispatchMessageW(&msg); 102 } 103 104 return cast(int) msg.wParam; 105 } 106 107 int* p; 108 extern(Windows) 109 LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow 110 { 111 switch (message) 112 { 113 case WM_COMMAND: 114 { 115 switch (LOWORD(wParam)) 116 { 117 case IDC_BTNCLICK: 118 if (HIWORD(wParam) == BN_CLICKED) 119 MessageBoxW(hWnd, "Hello, world!", "Greeting", 120 MB_OK | MB_ICONINFORMATION); 121 122 break; 123 124 case IDC_BTNDONTCLICK: 125 if (HIWORD(wParam) == BN_CLICKED) 126 { 127 MessageBoxW(hWnd, "You've been warned...", "Prepare to GP fault", 128 MB_OK | MB_ICONEXCLAMATION); 129 *p = 1; 130 } 131 132 break; 133 134 default: 135 } 136 137 break; 138 } 139 140 case WM_PAINT: 141 { 142 enum text = "D Does Windows"; 143 PAINTSTRUCT ps; 144 145 HDC dc = BeginPaint(hWnd, &ps); 146 scope(exit) EndPaint(hWnd, &ps); 147 RECT r; 148 GetClientRect(hWnd, &r); 149 HFONT font = CreateFontW(80, 0, 0, 0, FW_EXTRABOLD, FALSE, FALSE, 150 FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 151 DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial"); 152 HGDIOBJ old = SelectObject(dc, cast(HGDIOBJ) font); 153 SetTextAlign(dc, TA_CENTER | TA_BASELINE); 154 TextOutA(dc, r.right / 2, r.bottom / 2, text.ptr, text.length); 155 DeleteObject(SelectObject(dc, old)); 156 157 break; 158 } 159 160 case WM_DESTROY: 161 PostQuitMessage(0); 162 break; 163 164 default: 165 break; 166 } 167 168 return DefWindowProcW(hWnd, message, wParam, lParam); 169 }