Solved

Cant make the WM_CTLCOLORBTN to work. Please help.

Posted on 1998-08-28
5
3,086 Views
Last Modified: 2008-02-01
#include "windows.h"

 

   BOOL WINAPI MainDlgProc( HWND, UINT, WPARAM, LPARAM );


 

   int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmd,
                         int nShow )
   {                                    
             DialogBox( hInstance, MAKEINTRESOURCE( 10000 ), NULL, (DLGPROC)MainDlgProc );
      return( FALSE );
   }


 HBRUSH kulle,drulle;
   BOOL WINAPI MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
   {


      switch( msg )
      {
         case WM_INITDIALOG:      
                               {
             
                                     
                                          

                                    kulle=CreateSolidBrush( RGB( 0, 128, 128 ));
                                    drulle=CreateSolidBrush( RGB( 0, 0, 228 ));
                                          
                  
            return( TRUE );
                         }


        case WM_CTLCOLOREDIT:

                               SetTextColor( (HDC)wParam, RGB( 155, 55, 255 ) );
                              SetBkColor( (HDC)wParam, RGB( 55, 55, 255 ) );
            
      ;
                               return( (BOOL)kulle );
                               
                         case WM_CTLCOLORBTN:

                               if( GetWindowLong( (HWND)lParam, GWL_ID ) == 101 )  
            {
                                     
               SetBkColor( (HDC)wParam, RGB( 0, 25, 255 ) );      
               SetTextColor( (HDC)wParam, RGB( 0, 0, 0 ) );
            
              return( (BOOL)drulle );
            }
                              if( GetWindowLong( (HWND)lParam, GWL_ID ) == 102 )    
            {
               SetBkColor( (HDC)wParam, RGB( 0, 125, 255 ) );      
               SetTextColor( (HDC)wParam, RGB( 0, 0, 0 ) );        
                                           return( (BOOL)drulle );
            }
                              return((BOOL)drulle );
                        break;

         case WM_COMMAND:
            if( wParam == IDCANCEL )
            {
               if( kulle )        
                                           {
                  DeleteObject( kulle );
                                                      DeleteObject( drulle );
                                           }
               EndDialog( hDlg, TRUE );    
                                           return( TRUE );                              
                                    }
            break;
      }
      return( FALSE );
   }                                                  
---------------------------------------------------
//Resourcefile
10000 DIALOGEX 6, 18, 160, 113
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Color Read-Only Edit Control"
FONT 8, "MS Sans Serif"
BEGIN
    PUSHBUTTON      "Button1",101,4,5,34,14,NOT WS_TABSTOP,WS_EX_TRANSPARENT
    EDITTEXT        100,3,24,151,69,ES_AUTOHSCROLL
END
0
Comment
Question by:andla
  • 3
5 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1171476
If you read the documentation for the WM_CTLCOLORBTN message carefully you will find that the brush returned is not used!.  In addition, only the DC's text color is used and it is not used to affect the text in the button  it affects the color of the focus rectangle only.  Thus, the only thing you can do with this message is to set the color of the button's focus rectangle.
0
 
LVL 1

Expert Comment

by:marvinm
ID: 1171477
The documentation that nietod is referring to:
The text color of a check box or radio button applies to the box or button, its check mark, and the text. The focus rectangle for these buttons remains the system default color (typically black). The text color of a group box applies to the text but not to the line that defines the box. The text color of a push button applies only to its focus rectangle; it does not affect the color of the text.


0
 
LVL 1

Author Comment

by:andla
ID: 1171478
What shall i do to change the color of the button backgroung then. I don't like the gray color.

Yours sincerely
Andla :-)
0
 
LVL 1

Expert Comment

by:marvinm
ID: 1171479
You need to subclass the button and draw it yourself.
0
 
LVL 1

Accepted Solution

by:
marvinm earned 20 total points
ID: 1171480
Drawing the button yourself is not as difficult as you might think. Some things to remember:
Process WM_LBUTTONDBLCLK,WM_LBUTTONDOWN,WM_LBUTTONUP,WM_MOUSEMOVE,and WM_PAINT.
You'll have to set capture on WM_LBUTTONDOWN, and release capture on WM_LBUTTONUP.
You'll also need a way to keep track of whether or not your button was clicked on. That way you will know how to draw it when you have capture and receive WM_MOUSEMOVE.
Here's an example of drawing a button:
 
GetClientRect(hwnd,&rectButton);
hDC = BeginPaint(hwnd,&ps);
GetWindowText(hwnd,szBuf,iBufLen);
SetBkColor(hDC,GetSysColor(COLOR_3DFACE)); // if you want it grey
SetTextColor(hDC,/** some RGB **/);
SetBkMode(hDC,TRANSPARENT);
DrawEdge(hDC,&bRect,EDGE_RAISED,BF_RECT);
GetTextExtentPoint32(hDC,szBuf,strlen(szBuf),&tSize);
iTextPos = max(0,((rectButton.right - rectButton.left - tSize.cx)/2));
TextOut(hDC,iTextPos,0,szBuf,strlen(szBuf));
EndPaint(hwnd,&ps);
 


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question