// ウィンドウの基礎モジュール import win32.windows; import map; class BaseWindow { protected: static Map map; HWND hWnd; static extern(Windows) LRESULT WindowMapProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { BaseWindow target = cast(BaseWindow) map.GetProp(hWnd); if (!target) { switch (uMsg) { case WM_NCCREATE: case WM_CREATE: target = cast(BaseWindow) (cast(LPCREATESTRUCT)lParam).lpCreateParams; break; case WM_INITDIALOG: target = *(cast(BaseWindow*)lParam); map.SetProp(hWnd, target); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } } if (uMsg == WM_DESTROY) { map.RemoveProp(hWnd); } return target.WindowProc(hWnd, uMsg, wParam, lParam); } public: static this() { map = Map.GetInstance(); } void CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hParentWnd, HMENU nIDorHMenu, LPVOID lpParam) { WNDCLASSEX wcex; wcex.cbSize = WNDCLASSEX.sizeof; wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = cast(WNDPROC) &WindowMapProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = GetModuleHandle(NULL); wcex.hIcon = cast(HICON) LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE); // wcex.hCursor = cast(HCURSOR) LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE); wcex.hCursor = LoadCursor(null, IDC_ARROW); wcex.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH); wcex.lpszMenuName = NULL; wcex.lpszClassName = lpszClassName; wcex.hIconSm = NULL; PreCreateWindow(wcex); if (RegisterClassEx(&wcex) == 0) { assert(0); } hWnd = CreateWindowEx( dwExStyle, lpszClassName, lpszWindowName, dwStyle, x, y, nWidth, nHeight, hParentWnd, nIDorHMenu, GetModuleHandle(NULL), this ); if (hWnd == NULL) { assert(0); } map.SetProp(hWnd, this); // ShowWindow(hWnd, SW_SHOW); // UpdateWindow(hWnd); // MSG msg; // BOOL bRet; // while ((bRet = GetMessage(&msg, hWnd, 0, 0)) != 0) { // if (bRet == -1) { // assert(0); // // error // } else { // TranslateMessage(&msg); // DispatchMessage(&msg); // } // } } void PreCreateWindow(inout WNDCLASSEX wcex) { return; } extern(Windows) uint WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CLOSE: PostQuitMessage(0); break; case WM_DESTROY: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0; } HWND WindowHandle() { return hWnd; } } class Dialog : BaseWindow { protected: UINT dialog_id; public: this(UINT id) { dialog_id = id; } int CreateModal(HWND hParentWnd) { return DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(dialog_id), hParentWnd, cast(DLGPROC) &WindowMapProc, cast(LPARAM) &this); } extern(Windows) uint WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: OnInitDialog(hWnd); break; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: OnOK(hWnd); break; case IDCANCEL: OnCancel(hWnd); break; default: break; } break; default: return FALSE; } return TRUE; } void OnInitDialog(HWND hDlg) { return TRUE; } void OnOK(HWND hDlg) { EndDialog(hDlg, IDOK); } void OnCancel(HWND hDlg) { EndDialog(hDlg, IDCANCEL); } }