WM_CTLCOLORSTATIC不适用于Win7 [英] WM_CTLCOLORSTATIC doesnt work on Win7
问题描述
这是一个小测试程序。单击该按钮应该更改编辑控件的背景颜色,其为WS_DISABLED样式,因此我通过 WM_CTLCOLORSTATIC 来控制backgroundcolor。
基于XP和VS 2010构建,它工作正常。如此构建的exe在Win7上无法正常工作。当我使用VS 2012在Win7上构建程序时,它也没有按预期工作。
这是什么原因,我该如何解决?
This is a little test program. Clicking the button should change the backgroundcolor of the edit control, which as WS_DISABLED style and therefor I trie to control the backgroundcolor via WM_CTLCOLORSTATIC.
Built on XP with VS 2010 it works fine. The so built exe doesnt work properly on Win7. When I build the program on Win7 with VS 2012, it also doesnt work as expected.
What is the reason for that, and how can I fix it?
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "FensterKlasse";
RegisterClass (&wndclass);
//Ein einfaches Fenster erstellen
hwnd = CreateWindow ("FensterKlasse", "Fenster",
WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT,
200, 100,
NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//Zwei Brushes für zwei verschiedene Hintergrundfarben
static HBRUSH brush0 = CreateSolidBrush(RGB(255, 0, 0));
static HBRUSH brush1 = CreateSolidBrush(RGB(0, 255, 0));
//ein Editfeld
static HWND editFeld;
//Status der Hintergrundfarbe, kann 0 oder 1 annehmen
static int brushNr = 0;
switch (message)
{
case WM_CREATE :
//ein Editfeld mit dem Attribut WS_DISABLED erstellen
editFeld = CreateWindowEx (WS_EX_CLIENTEDGE, "edit", NULL,
WS_CHILD | WS_VISIBLE | WS_DISABLED | ES_LEFT,
3, 3, 80, 20, hwnd, (HMENU) 1,
((LPCREATESTRUCT) lParam) -> hInstance, NULL);
//Einen Button erstellen
CreateWindow ("button", "Knopp",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
3, 33, 80, 20, hwnd, (HMENU) 2,
((LPCREATESTRUCT) lParam) -> hInstance, NULL);
return 0;
case WM_CTLCOLORSTATIC:
//Hintergrundfarbe für das disabled Editfeld abhängig vom Status der Hintergrundfarbe zurückgeben
if(brushNr)
return (LRESULT)brush1;
else
return (LRESULT)brush0;
case WM_COMMAND :
//Auf Knopfdruck den Status der Hintergrundfarbe toggeln
brushNr = !brushNr;
//Neuzeichnen des Editfeldes veranlassen
InvalidateRect(editFeld, 0, TRUE);
return 0;
case WM_DESTROY :
DeleteObject(brush0);
DeleteObject(brush1);
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
推荐答案
在VS 2012下编译的Woah
这会通过编译器????
Woah that compiles under VS 2012
This gets passed the compiler????
//Zwei Brushes für zwei verschiedene Hintergrundfarben
static HBRUSH brush0 = CreateSolidBrush(RGB(255, 0, 0));
static HBRUSH brush1 = CreateSolidBrush(RGB(0, 255, 0));
忽略那个让我惊呆了并继续前进的问题。
To回答你的实际问题XP的行为实际上是错误的,而不是Win32 API定义的Win7上的行为。看起来他们纠正了XP中存在的错误...尝试
Ignoring that issue which I am sort of stunned at and moving on.
To answer you actual question the behaviour of XP is actually wrong not the behaviour on Win7 as defined by the Win32 API. It looks like they corrected a bug that was present in XP ... Try
case WM_CTLCOLORSTATIC:
//Hintergrundfarbe für das disabled Editfeld abhängig vom Status der Hintergrundfarbe zurückgeben
hDC = (HDC) wParam;
SetBkMode(hDC, TRANSPARENT);
if(brushNr)
return (LRESULT)brush1;
else
return (LRESULT)brush0;
另一个选择是物理设置文本颜色而不是透明它
The other alternative would have been to physically set the text colors rather than transparent it
case WM_CTLCOLORSTATIC:
//Hintergrundfarbe für das disabled Editfeld abhängig vom Status der Hintergrundfarbe zurückgeben
if (brushNr) {
SetBkColor((HDC) wParam, RGB(0, 255, 0));
return (LRESULT)brush1;
} else {
SetBkColor((HDC) wParam, RGB(255, 0, 0));
return (LRESULT)brush0;
}
我还可以强烈建议您使用前面的32位API接口,而不是将旧的16位调用随机混合使用。在Windows XP上有点好,但是如果你打算为Win7编写,请使用CreateWindowEx,RegisterClassEx等,有一些陷阱可以将它们混合起来。
Can I also strongly suggest they you either use only the 32bit API interface going forward and not keep mixing them up randomly with the old 16 bit calls. It was sort of okay on windows XP but if you are going to write for Win7 use the CreateWindowEx, RegisterClassEx etc there are a few traps in mixing them up going forward.
这篇关于WM_CTLCOLORSTATIC不适用于Win7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!