【发布时间】:2012-12-18 03:44:26
【问题描述】:
我有一个下拉菜单,我想用它来更改窗口的背景;该窗口默认为我的“冬季背景”。
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?如果没有,我还能如何更改背景?
【问题讨论】:
-
SetClassLongPtr 优于 SetClassLong 的要点在于第三个参数是 LONG_PTR 而不是 LONG。显然你的演员是不正确的。改进您的错误检查。
-
哎呀……就这么简单。