|
| 1 | +/* |
| 2 | + * PROJECT: M2-Team Common Library |
| 3 | + * FILE: M2Win32GUIHelpers.cpp |
| 4 | + * PURPOSE: Implementation for the Win32 desktop GUI helper functions |
| 5 | + * |
| 6 | + * LICENSE: The MIT License |
| 7 | + * |
| 8 | + * DEVELOPER: Mouri_Naruto (Mouri_Naruto AT Outlook.com) |
| 9 | + */ |
| 10 | + |
| 11 | +#include "stdafx.h" |
| 12 | + |
| 13 | +#include <Windows.h> |
| 14 | +#include <VersionHelpers.h> |
| 15 | + |
| 16 | +#include "M2Win32GUIHelpers.h" |
| 17 | +#include "M2MessageDialogResource.h" |
| 18 | + |
| 19 | +/** |
| 20 | + * Enables the Per-Monitor DPI Aware for the specified dialog using the |
| 21 | + * internal API from Windows. |
| 22 | + * |
| 23 | + * @return INT. If failed. returns -1. |
| 24 | + * @remarks You need to use this function in Windows 10 Threshold 1 or later. |
| 25 | + */ |
| 26 | +INT M2EnablePerMonitorDialogScaling() |
| 27 | +{ |
| 28 | + // Fix for Windows Vista and Server 2008. |
| 29 | + if (!IsWindowsVersionOrGreater(10, 0, 0)) return -1; |
| 30 | + |
| 31 | + typedef INT(WINAPI *PFN_EnablePerMonitorDialogScaling)(); |
| 32 | + |
| 33 | + HMODULE hModule = nullptr; |
| 34 | + PFN_EnablePerMonitorDialogScaling pFunc = nullptr; |
| 35 | + |
| 36 | + hModule = GetModuleHandleW(L"user32.dll"); |
| 37 | + if (!hModule) return -1; |
| 38 | + |
| 39 | + pFunc = reinterpret_cast<PFN_EnablePerMonitorDialogScaling>( |
| 40 | + GetProcAddress(hModule, (LPCSTR)2577)); |
| 41 | + if (!pFunc) return -1; |
| 42 | + |
| 43 | + return pFunc(); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * The parameter struct of the message dialog. |
| 48 | + */ |
| 49 | +struct DIALOG_BOX_PARAM |
| 50 | +{ |
| 51 | + HINSTANCE hInstance; |
| 52 | + LPCWSTR lpIconName; |
| 53 | + LPCWSTR lpTitle; |
| 54 | + LPCWSTR lpContent; |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * The callback function used of the message dialog. |
| 59 | + * |
| 60 | + * @param hwndDlg A handle to the message dialog. |
| 61 | + * @param uMsg The message. |
| 62 | + * @param wParam Additional message-specific information. |
| 63 | + * @param lParam Additional message-specific information. |
| 64 | + * @return Typically, the dialog box procedure should return TRUE if it |
| 65 | + * processed the message, and FALSE if it did not. If the dialog box |
| 66 | + * procedure returns FALSE, the dialog manager performs the default |
| 67 | + * dialog operation in response to the message. |
| 68 | + */ |
| 69 | +INT_PTR CALLBACK M2MessageDialogDialogCallBack( |
| 70 | + _In_ HWND hwndDlg, |
| 71 | + _In_ UINT uMsg, |
| 72 | + _In_ WPARAM wParam, |
| 73 | + _In_ LPARAM lParam) |
| 74 | +{ |
| 75 | + UNREFERENCED_PARAMETER(lParam); |
| 76 | + |
| 77 | + if (WM_INITDIALOG == uMsg) |
| 78 | + { |
| 79 | + HICON hIcon = reinterpret_cast<HICON>(LoadImageW( |
| 80 | + reinterpret_cast<DIALOG_BOX_PARAM*>(lParam)->hInstance, |
| 81 | + reinterpret_cast<DIALOG_BOX_PARAM*>(lParam)->lpIconName, |
| 82 | + IMAGE_ICON, |
| 83 | + 256, |
| 84 | + 256, |
| 85 | + LR_SHARED)); |
| 86 | + if (nullptr != hIcon) |
| 87 | + { |
| 88 | + SendMessageW( |
| 89 | + hwndDlg, |
| 90 | + WM_SETICON, |
| 91 | + ICON_SMALL, |
| 92 | + reinterpret_cast<LPARAM>(hIcon)); |
| 93 | + SendMessageW( |
| 94 | + hwndDlg, |
| 95 | + WM_SETICON, |
| 96 | + ICON_BIG, |
| 97 | + reinterpret_cast<LPARAM>(hIcon)); |
| 98 | + } |
| 99 | + |
| 100 | + SetWindowTextW( |
| 101 | + hwndDlg, |
| 102 | + reinterpret_cast<DIALOG_BOX_PARAM*>(lParam)->lpTitle); |
| 103 | + SetWindowTextW( |
| 104 | + GetDlgItem(hwndDlg, IDC_MESSAGE_DIALOG_EDIT), |
| 105 | + reinterpret_cast<DIALOG_BOX_PARAM*>(lParam)->lpContent); |
| 106 | + |
| 107 | + return (INT_PTR)TRUE; |
| 108 | + } |
| 109 | + else if ( |
| 110 | + (WM_CLOSE == uMsg) || |
| 111 | + (WM_COMMAND == uMsg && IDOK == LOWORD(wParam))) |
| 112 | + { |
| 113 | + EndDialog(hwndDlg, 0); |
| 114 | + } |
| 115 | + |
| 116 | + return FALSE; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Creates and shows the message dialog. |
| 121 | + * |
| 122 | + * @param hInstance A handle to the module which contains the message dialog |
| 123 | + * resource. If this parameter is nullptr, then the current |
| 124 | + * executable is used. |
| 125 | + * @param hWndParent A handle to the window that owns the message dialog. |
| 126 | + * @param lpIconName Pointer that references the icon to be displayed in the |
| 127 | + * message dialog. If this parameter is nullptr or the |
| 128 | + * hInstance parameter is nullptr, no icon will be displayed. |
| 129 | + * This parameter must be an integer resource identifier |
| 130 | + * passed to the MAKEINTRESOURCE macro. |
| 131 | + * @param lpTitle Pointer to the string to be used for the message dialog |
| 132 | + * title. This parameter is a null-terminated, Unicode string. |
| 133 | + * @param lpContent Pointer to the string to be used for the message dialog |
| 134 | + * content. This parameter is a null-terminated, Unicode |
| 135 | + * string. |
| 136 | + * @return If the function succeeds, the return value is the value of the |
| 137 | + * nResult parameter specified in the call to the EndDialog function |
| 138 | + * used to terminate the message dialog. If the function fails because |
| 139 | + * the hWndParent parameter is invalid, the return value is zero. The |
| 140 | + * function returns zero in this case for compatibility with previous |
| 141 | + * versions of Windows. If the function fails for any other reason, the |
| 142 | + * return value is –1. To get extended error information, call |
| 143 | + * GetLastError. |
| 144 | + */ |
| 145 | +INT_PTR WINAPI M2MessageDialog( |
| 146 | + _In_opt_ HINSTANCE hInstance, |
| 147 | + _In_opt_ HWND hWndParent, |
| 148 | + _In_opt_ LPCWSTR lpIconName, |
| 149 | + _In_ LPCWSTR lpTitle, |
| 150 | + _In_ LPCWSTR lpContent) |
| 151 | +{ |
| 152 | + DIALOG_BOX_PARAM Param = { hInstance, lpIconName,lpTitle,lpContent }; |
| 153 | + |
| 154 | + M2EnablePerMonitorDialogScaling(); |
| 155 | + |
| 156 | + return DialogBoxParamW( |
| 157 | + hInstance, |
| 158 | + MAKEINTRESOURCEW(IDD_MESSAGE_DIALOG), |
| 159 | + hWndParent, |
| 160 | + M2MessageDialogDialogCallBack, |
| 161 | + reinterpret_cast<LPARAM>(&Param)); |
| 162 | +} |
0 commit comments