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

Filling a rectangle with colour

davidgraham
Beginner
940 Views

I have created a Picture object as a Frame IDC_FRAME_OLDAD and want to fill it with colour.

I think the following code should work.

ps is type paintstructure and rect type rect

hdcClr = BeginPaint(hWnd,ps)
hBox=GetDlgItem(hWnd,IDC_FRAME_OLDAD)
lret=GetWindowRect(hBox,rect)
hbr = CreateSolidBrush (RGB(255,0,255))
lret = FillRect (hdcClr, rect, hbr)
lret = DeleteObject (hbr)
lret=EndPaint(hWnd,ps)

What am I doing wrong?

Thanks,

David

0 Kudos
6 Replies
anthonyrichards
New Contributor III
940 Views
Giving the static control a Frame propertyjust draws a rectangular frame using the default color (probably black). In resource Editor, open its properties page and you can there specify it as a Rectangle. You can specify its initial fill the colour there too. To change the rectangle fill colour at run-time, you just need to intercept the WM_CTLCOLORSTATIC command for the control that is issued to the window containing it just before it is drawn and then return a brush handle for the color you want. WM_PAINT doesn't come into it, I think.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
940 Views

BeginPaint does not make much (if any) sense outside of WM_PAINT context: basically, you should paint only when you're sent a WM_PAINT.

The simplest workaround is to use an owner-drawn static control, which sends a WM_DRAWITEM message to the parent (during its own WM_PAINT processing), allowing you to paint whatever you like on provided DRAWITEMSTRUCT.hDC.

For examples, check the old Xflogm code (just search for "ownerdraw" and "drawitem" within). Please see also the notes and remarks under DLG_DRAWITEM heading: I'm not sure, but it's possible that you will still have to manually adjust the .rc file to insert SS_OWNERDRAW style.

0 Kudos
davidgraham
Beginner
940 Views

I think this is what you ment, but case (IDC_FRAME_OLDAC) is never reached.

case (WM_CTLCOLORSTATIC)
iCtrl=LOWORD(wParam)
select case (iCtrl)
case (IDC_FRAME_OLDAD)
hbr = CreateSolidBrush (RGB(255,0,255))
end select

David

0 Kudos
anthonyrichards
New Contributor III
940 Views

On testing what I recommended, I found that the WM_CTLCOLORSTATIC message for the Picture control was indeed sent and intercepted, but the returned brush is not used to paint the rectangle (hbrush=SelectObject(hdcstatic, hbackbrush) also fails to set the color).

If all you want is a rectangle to fill with color, why not chose a static text control and delete the text? I know for certainthat willwork with WM_CTLCOLORSTATIC message processing and returning a brush handle.

If you want to draw anything more complicated than text in the rectangle, then you will have to do what Jugoslav recommends and make it 'ownerdrawn' and then draw in the rectangle after you intercept the WM_DRAWITEM message for the control.

The following works:

1) Open your .RC file as text (NOT auto!) and add or change the type for your control to SS_OWNERDRAWN. Then save it.

2) Add the following to your window procedure

USE DFWINTY
type (T_DRAWITEMSTRUCT) ds
Pointer (pdrawitem, ds)
integerret, hitem
....

case (WM_DRAWITEM)
pdrawitem=lparam
hitem=wparam
if(IDC_STATIC_PICTURE.eq.hitem) then
ret=MessageBox(hdlg,'MyDlgProc reached..fill a YELLOW Rectangle'c, &
'MyDlgProc WM_DRAWITEM message'C, MB_ICONEXCLAMATION)
! Create a yellow brush
hbackbrush=CreateSolidBrush(RGB(245, 255, 0))
MyDlgProc=FillRect(ds%hdc,ds%rcItem,hbackbrush)
return
endif

0 Kudos
Jugoslav_Dujic
Valued Contributor II
940 Views

Yes, Tony is right: if you just want the color, use a normal static text control. You can add a WS_BORDER, WS_EX_CLIENTEDGE or WS_EX_STATICEDGE for nicer effects.

Tony, you forgot DeleteObject(hbackbrush). ;-)

0 Kudos
davidgraham
Beginner
940 Views

Thanks all,

I decided just to set the type as bitmap, it was easier but not so flexible, as I only wanted 4 rectangles with 4 set colours.

David

0 Kudos
Reply