Solved

Cant make the WM_CTLCOLORBTN to work. Please help.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TTimer no longer functioning 7 85
Task manager indicates my c++ program memory consumption is growing? 12 100
FMX TCameraComponent Problem 2 54
Issues with C++ Class 19 81
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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

26 Experts available now in Live!

Get 1:1 Help Now