Jump to content
  • Advertisement
Sign in to follow this  
DesignerX

Frustrating problem

This topic is 5448 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I hate burshes, pens and eveything that comes with windows GDI !!! I have this dialog. On this dialog I put a picture control (with the rectangle option selected). I want to fill this entire rectangle with a specific color on each dialog redraw. (The color is changed by user) What is the easiest way to do this ???

Share this post


Link to post
Share on other sites
Advertisement
Disclaimer: I'm really lazy. K?

I usually use a CRichEditCtrl, don't put any text in it, and use SetBackgroundColor. Be sure to AfxInitRichEdit. That way you don't even have to think about brushes or rect logic, just make the edit box the size you want and shut off the ability to input text. Might play around with the border settings to make it look like you want.

Or if you're using pure Win32, look at WM_CTLCOLORSTATIC and its brethren.

-fel

Share this post


Link to post
Share on other sites
Damn, I tought this is it, but it seems I can't find the function to change control's background.

I tried using SetBkColor((HDC)wParam, RGB()) but there wasn't a change in the background.

Is this the correct function or is there another one ?


P.S.

Yeah, you guessed it, I'm using Win32 Api, Not MFC (Yack !)

Share this post


Link to post
Share on other sites
True, I am female.

Okay, WM_CTLCOLORSTATIC is the way to go then, if doing pure Win32. Doing this out of memory so might need a little tweaking:


// somewhere before your handler
COLORREF backColor = RGB(255,0,0);
HBRUSH g_hbrBackground = CreateSolidBrush(backColor);

// in your windows message handler
case WM_CTLCOLORSTATIC:
{
backColor = // your color here for this round
DeleteObject(g_hbrBackground); // kill the latest brush implementation so you don't get a resource leak
g_hbrBackground = CreateSolidBrush(backColor);
SetBkColor((HDC)wParam, backColor);
return ((LRESULT)g_hbrBackground);
}

// make sure you DeleteObject your most recent g_hbrBackground prior to shutdown.



You can of course check your hwnd vs. the hwnd passed by the proc to pick which controls this is done to.

Also, note that WM_CTLCOLORSTATIC is used for static controls, and for edit boxes that are read-only. If your edit box isn't read only use WM_CTLCOLOREDIT.

Let me know if this doesn't work well, it's been a very long time.

Edit: looking around a bit, some people apparently don't think it's necessary to SetBkColor, if they return the right HBRUSH, and some people don't think it's necessary to return the right HBRUSH if they SetBkColor. Doing both is probably safest, but if you want to cut down on the DeleteObject calls for the brush, you might consider just returning some random brush and seeing if that works. Also, be aware that WM_CTLCOLOR triggers as a result of WM_PAINT on the control in question.

-fel

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!