Solved

Cant make the WM_CTLCOLORBTN to work. Please help.

Posted on 1998-08-28
5
2,962 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now