問題描述
我正在使用 Windows API 創建一個基本的 GUI,但我遇到了一個問題.它從一個主窗口開始,該窗口以我設置的自定義背景顏色打開 (RGB(230,230,230))
.然后用靜態控件在左上角顯示文本.
I am creating a basic GUI with the Windows API and I have run into an issue. It starts with a main window that opens with a custom background color I set (RGB(230,230,230))
. It then displays text in the upper left corner with the static control.
settingstext = CreateWindow("STATIC",
"SETTINGS",
SS_LEFT | WS_CHILD,
12,
20,
100,
20,
hwnd,
NULL,
proginstance,
NULL);
ShowWindow(settingstext, 1);
這是可行的,但是當顯示文本時,我需要一種方法來更改它的背景以匹配主窗口,否則它看起來就像沒有融合在一起.
This works, but when the text is displayed I need a way to change the background of it to match the main window or else it just looks like it doesn't blend in.
我的問題是,我該怎么做?我目前使用下面的方法并且它有效,但我想知道,有沒有辦法在靜態控件的 CreateWindow
函數之后以某種方式永久設置背景顏色,而無需更改系統顏色,以及只需將它應用于那個控件,而不是任何發送 WM_CTLCOLORSTATIC
消息的控件.我已經嘗試在消息循環之外使用 GetDC
函數和 SetBkColor
函數,但沒有任何效果.
My question is, how do I do this? I currently use the method below and it works, but I wanted to know, is there a way to permanently set the background color somehow, right after the CreateWindow
function for the static control without changing system colors, and just have it apply to that one control and not anything that sends the WM_CTLCOLORSTATIC
message. I have experimented around with using the GetDC
function and SetBkColor
function outside of the message loop but nothing works.
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230));
return (INT_PTR)CreateSolidBrush(RGB(230,230,230));
}
我想這樣做是因為...
I want to do this because...
- 我不想用每次窗口重繪時都需要調用的函數填滿我的消息循環.
- 讓更改僅應用于此靜態控件.
我將非常感謝可以提供的任何幫助,至少為我指明正確的方向,謝謝.
I would be very thankful for any help that could be provided, at least pointing me in the right direction, thanks.
推薦答案
對于靜態文本控件,沒有永久的方法來設置文本顏色或其背景.即使您想將更改應用于單個靜態控件;您仍然需要在即將繪制控件時在父 dlgproc 中處理 WM_CTLCOLORSTATIC 通知消息.
For static text controls there's no permanent way to set the text color or their background. Even if you want to apply the changes to a single static control; you would still have to handle WM_CTLCOLORSTATIC notification message in parent dlgproc just when the control is about to be drawn.
這是由于 DefWindowProc
每次處理 WM_CTLCOLORSTATIC
時都會覆蓋您對設備上下文的更改,如 MSDN:
This is due to the DefWindowProc
overwriting your changes to the device context each time it handles WM_CTLCOLORSTATIC
as stated in the MSDN:
默認情況下,DefWindowProc 函數為靜態控件選擇默認系統顏色.
By default, the DefWindowProc function selects the default system colors for the static control.
static HBRUSH hBrush = CreateSolidBrush(RGB(230,230,230));
case WM_CTLCOLORSTATIC:
{
if (settingstext == (HWND)lParam)
//OR if the handle is unavailable to you, get ctrl ID
DWORD CtrlID = GetDlgCtrlID((HWND)lParam); //Window Control ID
if (CtrlID == IDC_STATIC1) //If desired control
{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230));
return (INT_PTR)hBrush;
}
}
如果您想讓控件的背景在父對話框上透明,您可以使用 SetBkMode(hdcStatic, TRANSPARENT)
.
If you're looking to make the control's background transparent over a parent dialog you could use SetBkMode(hdcStatic, TRANSPARENT)
.
這篇關于使用 C++ 靜態控制背景顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!