Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Colour in an edit box

michael_green
Beginner
577 Views
I would like to be able to colour the background of an edit box in a dialog (to indicate the colour chosen from the colour picker, for example). Can this be done and if so, how?

With many thanks in advance,

Mike
0 Kudos
5 Replies
Jugoslav_Dujic
Valued Contributor II
577 Views

Yes, (in a bit unexpected way) -- the edit control sends WM_CTLCOLOREDIT message to the parent window. On receiving it, the dialog is supposed to

1) return a brush handle which will be used to fill edit control's background (this is one of rare situations where DialogProc should not return plain TRUE/FALSE)

2)either do a SetBkMode(hDC, TRANSPARENT) or do a SetBkColor(hDC, same_color_as_the_brush) on given DC to ensure that "text background" is of same color as "window background".

3)do a SetTextColor(hDC) if you want to change the foreground color as well

Pay special attention not to create a GDI memory leak on 1) -- it's a grave sin to do:

case (WM_CTLCOLORSTATIC)
DialogProc = CreateSolidBrush(MyColor)

this will create a brush every while but neverdelete one, so you have to invent a bookkeeping scheme as well. The simplest one is to declare a STATIC hBrush, CreateSolidBrush on WM_INITDIALOG, and DeleteObject on WM_DESTROY, (as well as when it's about to change).

You can take a look at XFLOGM code (either 1.0 or 2.0/XFT) to take a look how it's implemented there.

Jugoslav

0 Kudos
michael_green
Beginner
577 Views
Thanks Jugoslav,

So far so good, I can colour an edit box when the dialog box is first drawn now.

But what I really want to do is respond to a user's selection of an rgb combination and show the resulting colour somehow - I thought an edit box was most suitable, but I don't care what it is.

It looks like the WM_CTLCOLOREDIT message is only sent when the edit box is about to be drawn, so I don't think I can use that more than once. How do I change colour dynamically?

With many thanks again,

Mike
0 Kudos
Jugoslav_Dujic
Valued Contributor II
577 Views

WM_CTLCOLOREDIT is sent whenever the edit box receives WM_PAINT. Thus, if you want to generate it programatically,call InvalidateRect(hwndEdit, NULL) (+UpdateWindow(hwndEdit)) (of course, manage to change the hBrush accordingly).

Static control (maybe with SS_SUNKEN or WS_EX_STATICEDGE) looks like a better choice than edit control (as it doesn't display caret). The corresponding message is WM_CTLCOLORSTATIC, with identical usage.

Jugoslav

0 Kudos
onkelhotte
New Contributor II
577 Views

Hi there,
I tried to change the backgroundcolor of an edit boxl, butit wont do anything. I tried it withSetBKColor, but it wont work. SetBKColor returns 16777215, but nothing else happens.

use dfwina
implicit none
include 'resource.fd'
integer*4 color,hWnd,hDC
type(dialog) dlg

hWnd=getDlgItem(dlgMain%hwnd,IDC_EDIT1)
hDC=getDC(hWnd)
color=setBkColor(hDC,#0000FF)

Thanks in advance,
Markus

0 Kudos
Jugoslav_Dujic
Valued Contributor II
577 Views
Exactly. The bottom line is: never use GetDC: it doesn't work as one might expect (although I'd expect some fine print in MSDN docs clarifying that). By its nature, it returns just a "temporary" DC, which will get overdrawn on next WM_PAINT. (Besides, SetBkColor does nothing visible per se, but even if you'd put e.g. a Rectangle call, it would still be fairy futile, as you might see the effect only temporarily).

No, you can't do that (modulo complicated dialog subclassing) using DFLOGM. As pointed above, it's possible either:

* Using native Windows dialog APIs (DialogBox->handle WM_CTLCOLOREDIT as described above)
* Using Xeffort/XFLOGM's DLG_BKCOLOR extension (it's 99% source-code compatible with DFLOGM, so you can just replace USE DFLOGM with USE XFLOGM and link with Xeffort.lib).
0 Kudos
Reply