Solved

Cant make the WM_CTLCOLORBTN to work. Please help.

Posted on 1998-08-28
5
3,059 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 108
Copy output image from TWindowsMediaPlayer 6 48
Precision Problem in C++ 7 44
Need some help with mailto 16 20
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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