Skip to main content

How to change the background color of an MFC edit control

I am sure that many of you might thought to change the background color of Edit Control and Dialog. So i present you sample article which servers your purpose.

To change the background color of an edit control in an MFC application, you must override the OnCtlColor() message-handling function of the window containing the edit control.

In the new OnCtlColor() function, set the background color and return a handle to a brush that will be used for painting the background. This must be done in response to receiving both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX messages in the OnCtlColor() function.

This is also documented in the "Class Library Reference" under CWnd::OnCtlColor().

Look at sample source code:

// editdlg.h : header file
//

////////////////////////////////////////////////////////////////////////
///
// CEditDialog dialog

class CEditDialog : public CDialog
{
// Construction
public:
CEditDialog(CWnd* pParent = NULL); // standard constructor

// Add a CBrush* to store the new background brush for edit controls.
CBrush* m_pEditBkBrush;

// Dialog Data
//{{AFX_DATA(CEditDialog)
enum { IDD = IDD_EDITDIALOG };
// NOTE: The ClassWizard will add data members here.
//}}AFX_DATA

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CEditDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CEditDialog)
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

// editdlg.cpp : implementation file
//

#include "stdafx.h"
#include "mdi.h"
#include "editdlg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////
// CEditDialog dialog

CEditDialog::CEditDialog(CWnd* pParent /*=NULL*/)
: CDialog(CEditDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CEditDialog)
// NOTE: The ClassWizard will add member initialization here.
//}}AFX_DATA_INIT

// Instantiate and initialize the background brush to black.
m_pEditBkBrush = new CBrush(RGB(0, 0, 0));
}

void CEditDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEditDialog)
// NOTE: The ClassWizard will add DDX and DDV calls here.
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEditDialog, CDialog)
//{{AFX_MSG_MAP(CEditDialog)
ON_WM_CTLCOLOR()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////
// CEditDialog message handlers

HBRUSH CEditDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
switch (nCtlColor) {

case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
// Set color to green on black and return the background
brush.
pDC->SetTextColor(RGB(0, 255, 0));
pDC->SetBkColor(RGB(0, 0, 0));
return (HBRUSH)(m_pEditBkBrush->GetSafeHandle());

default:
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
}

void CEditDialog::OnDestroy()
{
CDialog::OnDestroy();

// Free the space allocated for the background brush
delete m_pEditBkBrush;
}

Comments

Popular posts from this blog

Android ANR Time, UI Thread

Useful Android Performance / Stability debugging info  1. ANR Waiting times: Performing heavy I/O , Network access or orher heavy operations on UI thread ?? A BIG NO NO ..you are inviting ANR.   No response to an input event (such as key press or screen touch events) within 5 seconds. A  BroadcastReceiver  hasn't finished executing within 10 seconds.

What is the Current Directory in Windows CE & Windows Mobile ?

Lately i encountered  a situation where i had to find the current directory from where my application is running and all my config files reside. Question is "How do I find the current directory?" on Windows CE/Mobile devices. Desktop / PC it is just piece of cake but Windows CE / Mobile devices don't have a concept of a current directory.  Which means all pats are absolute and there is no concept called relative path. Due to lack of relative paths most of the files are loaded to the "Windows" directory and that is how Windows directory is crowded. Alternatively you can hard code directory path and insist user to load files always there. But i hate to hard code values or copy files to Windows directory.  So this has triggered to find a solution to identify my current directory. Since there is no concept of a current directory on a Windows CE / Windows Mobile device how would one locate a resource for which only a relative path is known?  That is what following c...

Andoird Key codes , adb keyevent

Useful Android Keyevents inputs: Some time it is extremely useful to send keyevent via adb for automation and other purposes. #adb shell  #input usage: input [text|keyevent] input text input keyevent adb shell input text "HELLO" LIST OF KEY CODES:  1 --> "KEYCODE_MENU" 3 --> "KEYCODE_HOME" 4 --> "KEYCODE_BACK" 19 --> "KEYCODE_DPAD_UP" 20 --> "KEYCODE_DPAD_DOWN" 21 --> "KEYCODE_DPAD_LEFT" 22 --> "KEYCODE_DPAD_RIGHT" 23 --> "KEYCODE_DPAD_CENTER" 24 --> "KEYCODE_VOLUME_UP" 25 --> "KEYCODE_VOLUME_DOWN" 26 --> "KEYCODE_POWER" 27 --> "KEYCODE_CAMERA" 28 --> "KEYCODE_CLEAR" 55 --> "KEYCODE_COMMA" 56 --> "KEYCODE_PERIOD" 57 --> "KEYCODE_ALT_LEFT" 58 --> "KEYCODE_ALT_RIGHT" 59 --> "KEYCODE_SHIFT_LEFT" 60...