我有一个下拉菜单,我想用它来更改窗口的背景;该窗口默认为我的“冬季背景”。
wClass.hbrBackground = CreatePatternBrush(LoadBitmap(hInst, MAKEINTRESOURCE(WinterBG)));
Whenever an item in the drop-down menu is chosen, it sets off CBN_SELCHANGE, where I grab the length and string of the item chosen. 我希望在此基础上改变背景。
case WM_COMMAND:
{
switch(HIWORD(wParam))
{
case CBN_SELCHANGE:
{
ItemIndex = SendMessage((HWND)lParam, (UINT)CB_GETCURSEL, (WPARAM)0, (LPARAM)0);
int ItemLen = SendMessage((HWND)lParam, (UINT)CB_GETLBTEXTLEN, (WPARAM)0, (LPARAM)0);
char* ListItem = (char*)malloc(ItemLen+1);
(char)SendMessageA((HWND)lParam, (UINT)CB_GETLBTEXT, (WPARAM)ItemIndex, (LPARAM)ListItem);
//////////////////////////////////////////////////////////////////////////////////////////////////
// I am certain this can be optimized
if ( ItemLen == 5 && ListItem[0] == 'S' ) // Spring
{
MessageBox(NULL, L"Spring chosen", L"Confirmed", MB_ICONINFORMATION | MB_OK);
HBRUSH brush = CreatePatternBrush(LoadBitmap(wClass.hInstance, MAKEINTRESOURCE(SpringBG)));
SetClassLongPtr(hWnd, GCLP_HBRBACKGROUND, (LONG)brush);
InvalidateRect(hWnd, NULL, TRUE);
UpdateWindow(hWnd);
}
.....
SetClassLongPtr 没有按我想要的方式工作。更新窗口后(我阅读了 InvalidateRect 和 UpdateWindow 来实现),没有任何变化——背景仍然在我的“WinterBG”上。
我正确使用 SetClassLongPtr 吗?如果没有,我还能如何更改背景?