标题是我的问题。我设法设置了静态的颜色并编辑 当我将它们都设置为相同颜色时进行控制,但我无法弄清楚 设置一种颜色而不影响其他颜色。我在互联网上四处张望,但我发现的所有内容都与如何设置一种颜色无关。 静态或编辑控件。这是我目前用来设置所有静态文本字段颜色的代码:
#include <windows.h>
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND staticTextField, staticTextFieldTwo;
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = CreateSolidBrush(RGB(0, 255, 255));
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
"Windows app",
WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
HDC hdcStatic = (HDC)wParam;
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_CREATE:
staticTextField = CreateWindow("STATIC", "static 1", WS_VISIBLE
| WS_CHILD, 20, 20, 300, 25, hwnd, NULL, NULL, NULL);
staticTextFieldTwo = CreateWindow("STATIC", "static2", WS_VISIBLE |
WS_CHILD | WS_BORDER, 100, 200, 300, 20, hwnd, NULL, NULL, NULL);
break;
case WM_CTLCOLORSTATIC:
SetTextColor(hdcStatic, RGB(255, 0, 0));
SetBkColor(hdcStatic, RGB(0, 255, 255));
return (INT_PTR)CreateSolidBrush(RGB(0, 255, 255));
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
任何帮助将不胜感激,我花了很长时间试图弄清楚我几乎已经放弃了! :-) 预先感谢。
答案 0 :(得分:4)
响应WM_CTLCOLORSTATIC
时,请选中保存控件句柄的lParam
。
CreateSolidBrush
创建一个GDI对象。应该使用DeleteObject
将其删除,否则会导致资源泄漏。您可以将画笔声明为static
并在末尾将其删除。示例:
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HBRUSH hbrush = NULL;
switch(message)
{
case WM_DESTROY:
if (hbrush) DeleteObject(hbrush);
hbrush = NULL;
PostQuitMessage(0);
break;
case WM_CREATE:
staticTextField = CreateWindow("STATIC", "static 1", WS_VISIBLE
| WS_CHILD, 20, 20, 300, 25, hwnd, NULL, NULL, NULL);
staticTextFieldTwo = CreateWindow("STATIC", "static2", WS_VISIBLE |
WS_CHILD | WS_BORDER, 100, 200, 300, 20, hwnd, NULL, NULL, NULL);
break;
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC)wParam;
if(lParam == (LPARAM)staticTextFieldTwo)
{
SetTextColor(hdcStatic, RGB(0, 255, 0));
SetBkColor(hdcStatic, RGB(0, 255, 255));
if (!hbrush)
hbrush = CreateSolidBrush(RGB(0, 255, 255));
return (LRESULT)hbrush;
}
break;
}
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}