user3012804
user3012804

Reputation: 81

change the background color of static control text created using CreateWindow in vc++

I am working on a open source project in VC++ and want to change the backcolor of a static control..

hwndRenderMessage = CreateWindow(TEXT("STATIC"), Str("MainWindow.BeginMessage"),
    WS_CHILDWINDOW|WS_VISIBLE|WS_CLIPSIBLINGS|SS_CENTER,
    0, 0, 0, 0, hwndRenderFrame, NULL, hinstMain, NULL);

SendMessage(hwndRenderMessage, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);

and the parent control of this control is

 hwndRenderFrame = CreateWindow(OBS_RENDERFRAME_CLASS, NULL,
    WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN,
    0, 0, 0, 0,
    hwndMain, NULL, hinstMain, NULL);
if(!hwndRenderFrame)
    CrashError(TEXT("Could not create render frame"));

So how to change the Background color of Static Control..

I google it and getting tha same answer use

case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230));
return (INT_PTR)CreateSolidBrush(RGB(230,230,230));
}

But there is no switch case in the file so what to do?? Acctually i worked on c# but this is the first time on vc++

Button image

Upvotes: 1

Views: 1473

Answers (2)

manuell
manuell

Reputation: 7620

I downloaded the OBS source code from sourceforge.

The Window Proc is OBS::RenderFrameProc located in WindowStuff.cpp

At the bottom of the proc (but before the "return"), add:

else if(message == WM_CTLCOLORSTATIC ) {
    // HERE YOUR CODE
}

EDIT: Changing Button Background

First, an advice: "don't do that". Buttons are very important and common components of the windows GUI, and their look and feel should be consistent in all applications. Users have ways to customize things for their Desktop, as a whole, and this include "accessibility" issues and behavior. Applications that want do it in their "own special way"s only bring problems.

Second, try this code for changing the "Setting..." button background to an ugly green: Add a case in the WM_NOTIFY message processing in OBS::OBSProc, in the switch(wParam)

case ID_SETTINGS:
   if(nmh.code == NM_CUSTOMDRAW)
   {
      LPNMCUSTOMDRAW  lpcd = (LPNMCUSTOMDRAW)lParam;
      if (lpcd->dwDrawStage == CDDS_PREPAINT )
      {
         SetDCBrushColor(lpcd->hdc, RGB(0, 255, 0));
         SelectObject(lpcd->hdc, GetStockObject(DC_BRUSH));
         LONG lBorders = 0;
         LONG lElipse = 5;
         RoundRect(lpcd->hdc, lpcd->rc.left + lBorders, lpcd- rc.top + lBorders,
                   lpcd->rc.right - lBorders, lpcd->rc.bottom - lBorders, lElipse, lElipse);
         return CDRF_NOTIFYPOSTPAINT;
       }
    }
    break;             

An alternative, with more standard borders:

         SetDCBrushColor(lpcd->hdc, RGB(0, 255, 0));
         SetDCPenColor(lpcd->hdc, RGB(0, 255, 0));
         SelectObject(lpcd->hdc, GetStockObject(DC_BRUSH));
         SelectObject(lpcd->hdc, GetStockObject(DC_PEN));
         LONG lBorders = 3;

To be complete, you may want to check the uItemState member of lpcd, for the CDIS_HOT flag, changing the color accordingly.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612794

You need to put that code in a window procedure. The window procedure looks like this:

LRESULT CALLBACK RenderMessageWndProc(HWND hWnd, UINT message, WPARAM wParam,
    LPARAM lParam)
{
    switch (message)
    {
    case WM_CTLCOLORSTATIC:
        // your code goes here
        return ....
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

And you need to sub-class your window so that it uses this WndProc. Like this:

SetWindowLongPtr(hwndRenderMessage, GWLP_WNDPROC, (LONG_PTR)RenderMessageWndProc);

If you don't know what a window procedure is, or what sub-classing is, then you really need to step back and learn some basics. For instance, Petzold's classic book Programming Windows is still an excellent starting point.

Upvotes: 1

Related Questions