Skip to content
This repository was archived by the owner on Jan 28, 2022. It is now read-only.

Commit 981e5fc

Browse files
committed
Add M2Win32GUIHelpers.
1 parent f832dba commit 981e5fc

6 files changed

Lines changed: 257 additions & 0 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* PROJECT: M2-Team Common Library
3+
* FILE: M2MessageDialogResource.h
4+
* PURPOSE: Definition for the message dialog resource
5+
*
6+
* LICENSE: The MIT License
7+
*
8+
* DEVELOPER: Mouri_Naruto (Mouri_Naruto AT Outlook.com)
9+
*/
10+
11+
#define IDD_MESSAGE_DIALOG 105
12+
#define IDC_MESSAGE_DIALOG_EDIT 1003
2.1 KB
Binary file not shown.

M2TeamCommonLibrary/M2TeamCommonLibrary.vcxitems

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
<ItemGroup>
1717
<ClCompile Include="$(MSBuildThisFileDirectory)M2BaseHelpers.cpp" />
1818
<ClCompile Include="$(MSBuildThisFileDirectory)M2CXHelpers.cpp" />
19+
<ClCompile Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.cpp" />
1920
<ClCompile Include="$(MSBuildThisFileDirectory)M2Win32Helpers.cpp" />
2021
</ItemGroup>
2122
<ItemGroup>
2223
<ClInclude Include="$(MSBuildThisFileDirectory)M2BaseHelpers.h" />
2324
<ClInclude Include="$(MSBuildThisFileDirectory)M2CXHelpers.h" />
25+
<ClInclude Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.h" />
26+
<ClInclude Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.h" />
2427
<ClInclude Include="$(MSBuildThisFileDirectory)M2Win32Helpers.h" />
2528
</ItemGroup>
29+
<ItemGroup>
30+
<ResourceCompile Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.rc" />
31+
</ItemGroup>
2632
</Project>

M2TeamCommonLibrary/M2TeamCommonLibrary.vcxitems.filters

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<Filter Include="M2Win32Helpers">
1111
<UniqueIdentifier>{75df954e-079c-4788-ab4f-c856a74a7e13}</UniqueIdentifier>
1212
</Filter>
13+
<Filter Include="M2Win32GUIHelpers">
14+
<UniqueIdentifier>{c3376011-3a37-453f-8b61-e247c38d599a}</UniqueIdentifier>
15+
</Filter>
1316
</ItemGroup>
1417
<ItemGroup>
1518
<ClCompile Include="$(MSBuildThisFileDirectory)M2CXHelpers.cpp">
@@ -21,6 +24,9 @@
2124
<ClCompile Include="$(MSBuildThisFileDirectory)M2Win32Helpers.cpp">
2225
<Filter>M2Win32Helpers</Filter>
2326
</ClCompile>
27+
<ClCompile Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.cpp">
28+
<Filter>M2Win32GUIHelpers</Filter>
29+
</ClCompile>
2430
</ItemGroup>
2531
<ItemGroup>
2632
<ClInclude Include="$(MSBuildThisFileDirectory)M2CXHelpers.h">
@@ -32,5 +38,16 @@
3238
<ClInclude Include="$(MSBuildThisFileDirectory)M2Win32Helpers.h">
3339
<Filter>M2Win32Helpers</Filter>
3440
</ClInclude>
41+
<ClInclude Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.h">
42+
<Filter>M2Win32GUIHelpers</Filter>
43+
</ClInclude>
44+
<ClInclude Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.h">
45+
<Filter>M2Win32GUIHelpers</Filter>
46+
</ClInclude>
47+
</ItemGroup>
48+
<ItemGroup>
49+
<ResourceCompile Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.rc">
50+
<Filter>M2Win32GUIHelpers</Filter>
51+
</ResourceCompile>
3552
</ItemGroup>
3653
</Project>
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* PROJECT: M2-Team Common Library
3+
* FILE: M2Win32GUIHelpers.h
4+
* PURPOSE: Definition 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+
#pragma once
12+
13+
#ifndef _M2_WIN32_GUI_HELPERS_
14+
#define _M2_WIN32_GUI_HELPERS_
15+
16+
#include <Windows.h>
17+
18+
/**
19+
* Enables the Per-Monitor DPI Aware for the specified dialog using the
20+
* internal API from Windows.
21+
*
22+
* @return INT. If failed. returns -1.
23+
* @remarks You need to use this function in Windows 10 Threshold 1 or later.
24+
*/
25+
INT M2EnablePerMonitorDialogScaling();
26+
27+
/**
28+
* Creates and shows the message dialog.
29+
*
30+
* @param hInstance A handle to the module which contains the message dialog
31+
* resource. If this parameter is nullptr, then the current
32+
* executable is used.
33+
* @param hWndParent A handle to the window that owns the message dialog.
34+
* @param lpIconName Pointer that references the icon to be displayed in the
35+
* message dialog. If this parameter is nullptr or the
36+
* hInstance parameter is nullptr, no icon will be displayed.
37+
* This parameter must be an integer resource identifier
38+
* passed to the MAKEINTRESOURCE macro.
39+
* @param lpTitle Pointer to the string to be used for the message dialog
40+
* title. This parameter is a null-terminated, Unicode string.
41+
* @param lpContent Pointer to the string to be used for the message dialog
42+
* content. This parameter is a null-terminated, Unicode
43+
* string.
44+
* @return If the function succeeds, the return value is the value of the
45+
* nResult parameter specified in the call to the EndDialog function
46+
* used to terminate the message dialog. If the function fails because
47+
* the hWndParent parameter is invalid, the return value is zero. The
48+
* function returns zero in this case for compatibility with previous
49+
* versions of Windows. If the function fails for any other reason, the
50+
* return value is –1. To get extended error information, call
51+
* GetLastError.
52+
*/
53+
INT_PTR WINAPI M2MessageDialog(
54+
_In_opt_ HINSTANCE hInstance,
55+
_In_opt_ HWND hWndParent,
56+
_In_opt_ LPCWSTR lpIconName,
57+
_In_ LPCWSTR lpTitle,
58+
_In_ LPCWSTR lpContent);
59+
60+
#endif // _M2_WIN32_GUI_HELPERS_

0 commit comments

Comments
 (0)