Revision: 9473 https://osdn.net/projects/ttssh2/scm/svn/commits/9473 Author: zmatsuo Date: 2021-10-17 01:11:30 +0900 (Sun, 17 Oct 2021) Log Message: ----------- アイコンをサブクラス化 Modified Paths: -------------- trunk/teraterm/ttpdlg/ttdlg.c -------------- next part -------------- Modified: trunk/teraterm/ttpdlg/ttdlg.c =================================================================== --- trunk/teraterm/ttpdlg/ttdlg.c 2021-10-16 16:11:19 UTC (rev 9472) +++ trunk/teraterm/ttpdlg/ttdlg.c 2021-10-16 16:11:30 UTC (rev 9473) @@ -36,7 +36,6 @@ #include <io.h> #include <direct.h> #include <commdlg.h> -#include <commctrl.h> #include <dlgs.h> #define _CRTDBG_MAP_ALLOC #include <stdlib.h> @@ -2526,8 +2525,7 @@ cx = GetSystemMetrics(SM_CXICON); cy = GetSystemMetrics(SM_CYICON); } -#if 0 // TODO -//#if defined(NTDDI_VISTA) && (NTDDI_VERSION >= NTDDI_VISTA) +#if 0 // defined(NTDDI_VISTA) && (NTDDI_VERSION >= NTDDI_VISTA) // LoadIconWithScaleDown() \x82\xCD vista\x82\xA9\x82\xE7 hr = LoadIconWithScaleDown(hInst, name, cx, cy, &hIcon); // LoadIconMetric(); @@ -2544,6 +2542,67 @@ return hIcon; } +typedef struct { + wchar_t *icon_name; + HICON icon; + WNDPROC prev_proc; +} IconSubclassData; + +static LRESULT IconProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) +{ + IconSubclassData *data = (IconSubclassData *)GetWindowLongPtr(hwnd, GWLP_USERDATA); + switch (msg) { + case WM_DPICHANGED: { + const HINSTANCE hinst = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE); + const UINT new_dpi = LOWORD(wp); + HICON icon = TTLoadIcon(hinst, data->icon_name, new_dpi); + if (icon != NULL) { + DestroyIcon(data->icon); + data->icon = icon; + SendMessage(hwnd, STM_SETICON, (WPARAM)icon, 0); + } + break; + } + case WM_NCDESTROY: { + LRESULT result = CallWindowProc(data->prev_proc, hwnd, msg, wp, lp); + DestroyIcon(data->icon); + if (HIWORD(data->icon_name) != 0) { + free(data->icon_name); + } + free(data); + return result; + } + default: + break; + } + return CallWindowProc(data->prev_proc, hwnd, msg, wp, lp); +} + +/** + * \x83_\x83C\x83A\x83\x8D\x83O\x82̃R\x83\x93\x83g\x83\x8D\x81[\x83\x8B\x82ɃA\x83C\x83R\x83\x93\x82\xF0\x83Z\x83b\x83g\x82\xB7\x82\xE9 + * + * \x97\xE1 + * SetDlgItemIcon(Dialog, IDC_TT_ICON, MAKEINTRESOURCEW(IDI_TTERM)); + * DPI\x82\xAA\x95ω\xBB\x82\xB5\x82\xBD\x82Ƃ\xAB\x82ɃA\x83C\x83R\x83\x93\x82̃T\x83C\x83Y\x82\xF0\x95ύX\x82\xB7\x82\xE9 + * case WM_DPICHANGED: + * SendDlgItemMessage(Dialog, IDC_TT_ICON, Message, wParam, lParam); + */ +void SetDlgItemIcon(HWND dlg, int nID, const wchar_t *name) +{ + IconSubclassData *data = (IconSubclassData *)malloc(sizeof(IconSubclassData)); + data->icon_name = (HIWORD(name) == 0) ? (wchar_t *)name : _wcsdup(name); + + const HINSTANCE hinst = (HINSTANCE)GetWindowLongPtr(dlg, GWLP_HINSTANCE); + const UINT dpi = GetMonitorDpiFromWindow(dlg); + data->icon = TTLoadIcon(hinst, name, dpi); + + const HWND hWnd = GetDlgItem(dlg, nID); + SendMessage(hWnd, STM_SETICON, (WPARAM)data->icon, 0); + + data->prev_proc = (WNDPROC)SetWindowLongPtrW(hWnd, GWLP_WNDPROC, (LONG_PTR)IconProc); + SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)data); +} + static INT_PTR CALLBACK AboutDlg(HWND Dialog, UINT Message, WPARAM wParam, LPARAM lParam) { static const DlgTextInfo TextInfos[] = { @@ -2565,6 +2624,7 @@ static int dlgw, dlgh; static HBITMAP dlgbmp = NULL, dlgprevbmp = NULL; static LPDWORD dlgpixel = NULL; + static HICON dlghicon = NULL; const int icon_x = 15, icon_y = 10, icon_w = 32, icon_h = 32; const int ID_EFFECT_TIMER = 1; RECT dlgrc = {0}; @@ -2584,11 +2644,9 @@ case WM_INITDIALOG: // \x83A\x83C\x83R\x83\x93\x82I\x82ɃZ\x83b\x83g { - HICON hicon; - UINT dpi; - #if defined(EFFECT_ENABLED) || defined(TEXTURE_ENABLED) int fuLoad = LR_DEFAULTCOLOR; + HICON hicon; if (IsWindowsNT4()) { fuLoad = LR_VGACOLOR; } @@ -2598,10 +2656,7 @@ // WM_PAINT \x82ŕ`\x89悷\x82\xE9\x81B dlghicon = hicon; #else - dpi = GetMonitorDpiFromWindow(Dialog); - hicon = TTLoadIcon(hInst, MAKEINTRESOURCEW(IDI_TTERM), dpi); - SendDlgItemMessage(Dialog, IDC_TT_ICON, STM_SETICON, (WPARAM)hicon, 0); - dlghicon = hicon; + SetDlgItemIcon(Dialog, IDC_TT_ICON, MAKEINTRESOURCEW(IDI_TTERM)); #endif } @@ -2868,13 +2923,9 @@ } break; #endif - case WM_DPICHANGED: { - const UINT new_dpi = LOWORD(wParam); - DestroyIcon(dlghicon); - dlghicon = TTLoadIcon(hInst, MAKEINTRESOURCEW(IDI_TTERM), new_dpi); - SendDlgItemMessage(Dialog, IDC_TT_ICON, STM_SETICON, (WPARAM)dlghicon, 0); + case WM_DPICHANGED: + SendDlgItemMessage(Dialog, IDC_TT_ICON, Message, wParam, lParam); break; - } case WM_DESTROY: DestroyIcon(dlghicon);