From 7e60078d4a7fe748a39c135def9e84a2421ab474 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Sun, 31 Oct 2021 14:20:44 -0500 Subject: Avoid using control ids inside of thmutil. --- src/samples/thmviewer/display.cpp | 8 ++++---- src/samples/thmviewer/precomp.h | 1 + src/samples/thmviewer/thmviewer.cpp | 37 +++++++++++++++++++++++++------------ 3 files changed, 30 insertions(+), 16 deletions(-) (limited to 'src/samples') diff --git a/src/samples/thmviewer/display.cpp b/src/samples/thmviewer/display.cpp index 9ef88018..f40b50b5 100644 --- a/src/samples/thmviewer/display.cpp +++ b/src/samples/thmviewer/display.cpp @@ -188,7 +188,7 @@ static DWORD WINAPI DisplayThreadProc( THEME_CONTROL* pControl = pCurrentHandle->pTheme->rgControls + i; if (!pControl->wPageId) { - ThemeShowControl(pCurrentHandle->pTheme, pControl->wId, nCmdShow); + ThemeShowControl(pControl, nCmdShow); } } @@ -264,7 +264,7 @@ static LRESULT CALLBACK DisplayWndProc( break; case WM_TIMER: - if (!lParam && SUCCEEDED(ThemeSetProgressControl(pHandleTheme->pTheme, wParam, dwProgress))) + if (!lParam && SUCCEEDED(ThemeSetProgressControl(reinterpret_cast(wParam), dwProgress))) { dwProgress += rand() % 10 + 1; if (dwProgress > 100) @@ -322,12 +322,12 @@ static BOOL DisplayOnThmLoadedControl( // Pre-populate some control types with data. if (THEME_CONTROL_TYPE_RICHEDIT == pControl->type) { - hr = ThemeLoadRichEditFromResource(pTheme, pControl->wId, MAKEINTRESOURCEA(THMVWR_RES_RICHEDIT_FILE), ::GetModuleHandleW(NULL)); + hr = WnduLoadRichEditFromResource(pControl->hWnd, MAKEINTRESOURCEA(THMVWR_RES_RICHEDIT_FILE), ::GetModuleHandleW(NULL)); ExitOnFailure(hr, "Failed to load richedit text."); } else if (THEME_CONTROL_TYPE_PROGRESSBAR == pControl->type) { - DWORD dwId = ::SetTimer(pTheme->hwndParent, pControl->wId, 500, NULL); + DWORD dwId = ::SetTimer(pTheme->hwndParent, reinterpret_cast(pControl), 500, NULL); dwId = dwId; // prevents warning in "ship" build. Assert(dwId == pControl->wId); } diff --git a/src/samples/thmviewer/precomp.h b/src/samples/thmviewer/precomp.h index f11d3534..762a0623 100644 --- a/src/samples/thmviewer/precomp.h +++ b/src/samples/thmviewer/precomp.h @@ -28,6 +28,7 @@ #include "shelutil.h" #include "strutil.h" #include "thmutil.h" +#include "wndutil.h" #include "resource.h" diff --git a/src/samples/thmviewer/thmviewer.cpp b/src/samples/thmviewer/thmviewer.cpp index 94b1ef55..38f3c4dc 100644 --- a/src/samples/thmviewer/thmviewer.cpp +++ b/src/samples/thmviewer/thmviewer.cpp @@ -390,6 +390,12 @@ static void OnThemeLoadError( LPWSTR* psczErrors = NULL; UINT cErrors = 0; TVINSERTSTRUCTW tvi = { }; + const THEME_CONTROL* pTreeControl = NULL; + + if (!ThemeControlExistsById(pTheme, THMVWR_CONTROL_TREE, &pTreeControl)) + { + ExitWithRootFailure(hr, E_INVALIDSTATE, "THMVWR_CONTROL_TREE control doesn't exist."); + } // Add the application node. tvi.hParent = NULL; @@ -397,7 +403,7 @@ static void OnThemeLoadError( tvi.item.mask = TVIF_TEXT | TVIF_PARAM; tvi.item.lParam = 0; tvi.item.pszText = L"Failed to load theme."; - tvi.hParent = reinterpret_cast(ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi))); + tvi.hParent = reinterpret_cast(::SendMessage(pTreeControl->hWnd, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi))); if (!vsczThemeLoadErrors) { @@ -405,13 +411,13 @@ static void OnThemeLoadError( ExitOnFailure(hr, "Failed to format error message."); tvi.item.pszText = sczMessage; - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); + ::SendMessage(pTreeControl->hWnd, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); hr = StrAllocFromError(&sczMessage, hrFailure, NULL); ExitOnFailure(hr, "Failed to format error message text."); tvi.item.pszText = sczMessage; - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); + ::SendMessage(pTreeControl->hWnd, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); } else { @@ -421,11 +427,11 @@ static void OnThemeLoadError( for (DWORD i = 0; i < cErrors; ++i) { tvi.item.pszText = psczErrors[i]; - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); + ::SendMessage(pTreeControl->hWnd, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); } } - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_EXPAND, TVE_EXPAND, reinterpret_cast(tvi.hParent)); + ::SendMessage(pTreeControl->hWnd, TVM_EXPAND, TVE_EXPAND, reinterpret_cast(tvi.hParent)); LExit: ReleaseStr(sczMessage); @@ -439,6 +445,7 @@ static void OnNewTheme( __in HANDLE_THEME* pHandle ) { + const THEME_CONTROL* pTreeControl = NULL; HANDLE_THEME* pOldHandle = reinterpret_cast(::GetWindowLongPtrW(hWnd, GWLP_USERDATA)); THEME* pNewTheme = pHandle->pTheme; @@ -460,17 +467,23 @@ static void OnNewTheme( ::SetWindowLongPtrW(hWnd, GWLP_USERDATA, reinterpret_cast(pHandle)); + if (!ThemeControlExistsById(pTheme, THMVWR_CONTROL_TREE, &pTreeControl)) + { + TraceError(E_INVALIDSTATE, "Tree control doesn't exist."); + return; + } + // Remember the currently selected item by name so we can try to automatically select it later. // Otherwise, the user would see their window destroyed after every save of their theme file and // have to click to get the window back. item.mask = TVIF_TEXT; item.pszText = wzSelectedPage; item.cchTextMax = countof(wzSelectedPage); - item.hItem = reinterpret_cast(ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_GETNEXTITEM, TVGN_CARET, NULL)); - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_GETITEM, 0, reinterpret_cast(&item)); + item.hItem = reinterpret_cast(::SendMessage(pTreeControl->hWnd, TVM_GETNEXTITEM, TVGN_CARET, NULL)); + ::SendMessage(pTreeControl->hWnd, TVM_GETITEM, 0, reinterpret_cast(&item)); // Remove the previous items in the tree. - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_DELETEITEM, 0, reinterpret_cast(TVI_ROOT)); + ::SendMessage(pTreeControl->hWnd, TVM_DELETEITEM, 0, reinterpret_cast(TVI_ROOT)); // Add the application node. tvi.hParent = NULL; @@ -480,7 +493,7 @@ static void OnNewTheme( tvi.item.pszText = pHandle && pHandle->pTheme && pHandle->pTheme->sczCaption ? pHandle->pTheme->sczCaption : L"Window"; // Add the pages. - tvi.hParent = reinterpret_cast(ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi))); + tvi.hParent = reinterpret_cast(::SendMessage(pTreeControl->hWnd, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi))); tvi.hInsertAfter = TVI_SORT; for (DWORD i = 0; i < pNewTheme->cPages; ++i) { @@ -490,7 +503,7 @@ static void OnNewTheme( tvi.item.pszText = pPage->sczName; tvi.item.lParam = i + 1; //prgdwPageIds[i]; - TODO: do the right thing here by calling ThemeGetPageIds(), should not assume we know how the page ids will be calculated. - HTREEITEM hti = reinterpret_cast(ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi))); + HTREEITEM hti = reinterpret_cast(::SendMessage(pTreeControl->hWnd, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi))); if (*wzSelectedPage && CSTR_EQUAL == ::CompareStringW(LOCALE_NEUTRAL, 0, pPage->sczName, -1, wzSelectedPage, -1)) { htiSelected = hti; @@ -503,10 +516,10 @@ static void OnNewTheme( htiSelected = tvi.hParent; } - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_EXPAND, TVE_EXPAND, reinterpret_cast(tvi.hParent)); + ::SendMessage(pTreeControl->hWnd, TVM_EXPAND, TVE_EXPAND, reinterpret_cast(tvi.hParent)); if (htiSelected) { - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_SELECTITEM, TVGN_CARET, reinterpret_cast(htiSelected)); + ::SendMessage(pTreeControl->hWnd, TVM_SELECTITEM, TVGN_CARET, reinterpret_cast(htiSelected)); } } -- cgit v1.2.3-55-g6feb