Thread View: borland.public.cppbuilder.nativeapi
7 messages
7 total messages
Started by Mark Morrisson
Fri, 09 May 2003 07:57
Problem with window subclassing...
Author: Mark Morrisson
Date: Fri, 09 May 2003 07:57
Date: Fri, 09 May 2003 07:57
55 lines
2433 bytes
2433 bytes
Hi everyone !
I want to change the text color of a static control, so I tried to subclass
the control but it didn't work. I precise I don't use MFC nor ATL. This
control
is created within a dialog box :
BOOL CALLBACK DlgGraphProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
lParam)
{
...
switch (uMsg)
{
case WM_INITDIALOG:
...
label = CreateWindow("STATIC", "", WS_CHILD | WS_VISIBLE | SS_LEFT,
15, 20, 32, 16, hGrp_Graph, NULL, hInst, NULL);
...
hGrp_Graph is the handle of a group box where the label is created. Later in
the WM_INITDIALOG message handler, I do the subclassing :
oldLabelProc = (WNDPROC) SetWindowLong(label, GWL_WNDPROC, (LONG)
labelProc);
Here is the labelProc procedure :
LRESULT CALLBACK labelProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
lParam)
{
switch (uMsg)
{
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SetTextColor(hdc, RGB(50, 10, 100));
SetBkColor(hdc, GetSysColor(COLOR_3DFACE));
EndPaint(hwnd, &ps);
return 0;
}
return CallWindowProc(oldLabelProc, hwnd, uMsg, wParam, lParam);
}
Well, when I run the program, the label doesn't have its color changed !
I've
tried to subclass an inner component of the dialog box (defined in the
resource
file) and it worked ! Though, I don't see the difference with a component
that
is created inside the dialog box... Is there an additional flag I should
specify
in the CreateWindow function in order to subclass the component ?
Thanks for your help,
Mark
Re: Problem with window subclassing...
Author: Gary Setter
Date: Fri, 09 May 2003 12:30
Date: Fri, 09 May 2003 12:30
11 lines
1390 bytes
1390 bytes
"Mark Morrisson" <mark4@NO_SPAMvoila.fr> wrote in message news:3ebb5ec5$1@newsgroups.borland.com... > Hi everyone ! > I want to change the text color of a static control, <snip> Have you looked into the WM_CTLCOLORSTATIC message? I think you can do what you want by handleling that message in the dialog proc. Hope that helps, Gary
Re: Problem with window subclassing...
Author: Mark Morrisson
Date: Fri, 09 May 2003 16:02
Date: Fri, 09 May 2003 16:02
32 lines
1723 bytes
1723 bytes
Hi Gary,
Thanks for your reply. No, I fear I can't handle the WM_CTLCOLORSTATIC
message because I create an array of labels and it wouldn't be smart to test
each
window handle in the switch statement :
case WM_CTLCOLORSTATIC:
{
HDC hdc = (HDC) wParam;
HWND hwndCtl = (HWND) lParam;
switch (hwndCtl)
{
case label[0]:
case label[1]:
case label[2]:
...
case label[99]:
SetTextColor(hdc, RGB(0, 0, 192));
return 0;
}
break;
}
But I'd like to know why the subclassing solution doesn't work... When I
apply it to a
control created in the resource file with CTEXT, for instance, it works
fine. But when
I apply it to a control created on the fly, it doesn't work... Why ?
Mark
Re: Problem with window subclassing...
Author: Gary Setter
Date: Sun, 11 May 2003 14:04
Date: Sun, 11 May 2003 14:04
34 lines
2298 bytes
2298 bytes
<snip>.. But what intrigued me is that text was displayed all the same... This makes me think that your subclassing did not work. If you want, you can still post a small working sample. > In fact, it's the > SetWindowText function that drew the text. WM_PAINT paints the text. > The SetWindowLong function is the only method I found (with SetProp) to > store the text of a label. SetWindowText and SetDlgItemText are the low level functions for storing text. I've never needed anything else. > > About the multiple labels, of course it prevents me from using the > CTLCOLORSTATIC > methos because it forces me to put lots of "case" statements in the switch > statement... > Since all variables tested in the "case" statements will be HWND, I don't > see any > way to determine its associated color in one instruction... I mean, I'm not > certain > that all these HWND's will form a serie (like 41001, 41002, 41003, etc). > Give the controls you want to work with an ID (see the hMenu param of CreateWindow[Ex]) Then use GetDlgCtrlID in the WM_CTLCOLORSTATIC handler to get the ID back. Hope that helps Gary
Re: Problem with window subclassing...
Author: Mark Morrisson
Date: Sun, 11 May 2003 17:06
Date: Sun, 11 May 2003 17:06
24 lines
2107 bytes
2107 bytes
Hi Gary ! I hope you're doing fine. Well, I gave you all the pieces of code that do the subclassing along my previous posts. The subclassing does work, I have set up a debug point inside the new WM_PAINT handler. And I can -assure- you that WM_PAINT isn't called when SetWindowText is called, only WM_SETTEXT is called. The default handler of WM_SETTEXT stores the text in an internal buffer, and displays it (that's why you can call SetWindowText at any moment). If I don't intercept WM_SETTEXT, then Windows displays the text, and when my WM_PAINT handler is called, it writes over the previous displayed text. Since I reduced the font, I can still see the text displayed by Windows behind my new text. Look at Win32.hlp, it says that SetWindowText stores the text and a buffer AND display it, without calling WM_PAINT. Your idea to use the hMenu parameter in CreateWindow is pretty good, I hadn't thought of it. But I guess that subclassing is a bit "cleaner", don't you think ? Have a nice day, Mark
Re: Problem with window subclassing...
Author: Gary Setter
Date: Mon, 12 May 2003 12:59
Date: Mon, 12 May 2003 12:59
21 lines
2061 bytes
2061 bytes
<snip> > But I guess that subclassing is a bit "cleaner", don't you > think ? > <snip> Clean can mean different things to different people. If the code for changing the text color using subclassing was more straightforward then the code using WM_CTLCOLORSTATIC, then you could say subclassing is cleaner. However, subclassing and handling WM_PAINT gets you involved in more things then you need to. Handling WM_CTLCOLORSTATIC gives you a chance to do what you need to do and nothing more. Generally, I only subclass when I need to trap messages that only go to the control. I had a similar need to yours in one project, used WM_CTLCOLORSTATIC and was happy with the result. Try both methods. See what is clean straightforward and easy to understand for you. Best of luck, Gary
Re: Problem with window subclassing...
Author: Mark Morrisson
Date: Mon, 12 May 2003 20:56
Date: Mon, 12 May 2003 20:56
3 lines
1286 bytes
1286 bytes
Ok, Gary, you're right, your method is more straightforward than mine. I thank you again for your advices and I hope all goes well. Mark
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads