CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2008
    Posts
    26

    TextColor in Command Button

    Can anyone tell me an easy way to set the text color of a button (forecolor) to any other color. Eg. red.

    I followed a similar procedure as for that of an edit control.But it does not work. Here is the code.

    int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)
    {
    HWND hwnd = CreateWindowEx ( //form options ); //Callback set to WindowProcedure
    HWND hwnd2=CreateWindowEx(0,"BUTTON,""button1",WS_CHILD | WS_VISIBLE ,10,10,60,30,hwnd,NULL,hThisInstance,NULL);
    }

    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message)
    {
    case WM_DESTROY:
    PostQuitMessage (0);
    break;

    case WM_CTLCOLORBTN: /*it is WM_CTLCOLOREDIT for an edit control. I am creating a button so i changed it to WM_CTLCOLORBTN*/
    SetTextColor((HDC)wParam,RGB(254,0,0));
    break;

    default:
    return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
    }

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: TextColor in Command Button

    I'm afraid WM_CTLCOLOR handling in the dialog cannot change the color of buttons.

    Try this:
    http://www.codeguru.com/Cpp/controls...icle.php/c5169
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    Apr 2008
    Posts
    26

    Re: TextColor in Command Button

    I checked the link out and it seems the example uses dialogs. In my code i use APIs to create the windows. I need to change the forecolor and backcolor of the command button which is made by createwindow api.Any ideas?

  4. #4
    Join Date
    Apr 2008
    Posts
    133

    Re: TextColor in Command Button

    as per msdn documentation "The WM_CTLCOLORBTN message is sent to the parent window of a button before drawing the button. The parent window can change the button's text and background colors. However, only owner-drawn buttons respond to the parent window processing this message."
    So your only option is to go for an owner drawn button

  5. #5
    Join Date
    Apr 2008
    Posts
    26

    Re: TextColor in Command Button

    What exactly is an owner drawn button? Can it be implemented with createwindow api in a form also created by createwindow api? Could you please show me an example of how to create it and change the text color.

  6. #6
    Join Date
    Apr 2008
    Posts
    133

    Re: TextColor in Command Button

    An owner draw button is a cutom button whose drawing is handled in the code at run time. You can do this by setting the BS_OWNERDRAW flag in your button's CreateWindowEx. Also you need to handle the WM_DRAWITEM for this button and basically draw the text and background color inside this handler. This will help you customize the button in any whichever way you want.

  7. #7
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: TextColor in Command Button

    Buttons with any one of the button styles BS_PUSHBUTTON, BS_DEFPUSHBUTTON, or BS_PUSHLIKE do not use the brush returned from
    handling a WM_CTLCOLORBTN message. I read somewhere that this "feature" goes back to the early days of windows.

    So you are going to have to handle the drawing of the button yourself

    Include BS_OWNERDRAW as one of the window styles for your button set in the dwStyle field of the CreateWindow function.

    You would then handle drawing the button from WM_DRAWITEM. Below is an example
    for an MFC button, but you should be able to change it for Win32 calls

    Code:
    void MASKED_BITMAP_BUTTON::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    	CDC* dc = CDC::FromHandle(lpDrawItemStruct->hDC);
    	CRect rect(lpDrawItemStruct->rcItem);
    	dc->SaveDC();
    	
    	rect.DeflateRect(0,0,0,2);
    	IsLite_ ? dc->FillSolidRect(&rect, LightColor_) : dc->FillSolidRect(&rect, ::GetSysColor(COLOR_BTNFACE));
    	
    	if(UseText_)
    	{
    		if(TextFont_.GetSafeHandle())
    			dc->SelectObject(TextFont_);
    		
    		dc->SetBkMode(TRANSPARENT);
    		dc->SetTextColor(RGB(0,0,0));
    		dc->DrawText(Text_.c_str(), &rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
    	}
    
    	
    	if(TheBitmap_.GetSafeHandle())
    	{
    		if(Stretch_)
    			TheBitmap_.DrawTransparentStretch(dc, rect, TransparentColor_);
    		else			
    			TheBitmap_.DrawTransparent(dc, rect.left + LeftSideOffset_, rect.top + TopSideOffset_, TransparentColor_);
    	}
    
    	rect.InflateRect(0,0,0,2);
    	dc->DrawEdge(&rect, ((lpDrawItemStruct->itemState & ODS_SELECTED) ? EDGE_SUNKEN : EDGE_RAISED), BF_RECT);
    
    	dc->RestoreDC(-1);
    }
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  8. #8
    Join Date
    Jan 2008
    Posts
    178

    Re: TextColor in Command Button

    Quote Originally Posted by blastingblast
    What exactly is an owner drawn button?
    Why don't you read MSDN ?
    There are plenty of complete (C-Win32 api) samples for Owner-Drawn buttons (KB, PSDK, etc)

  9. #9
    Join Date
    Apr 2008
    Posts
    26

    Smile Re: TextColor in Command Button

    Thanks,I finally understood it.Managed to use souldog's code after converting.Thanks Guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured