I have a MFC CDateTimeCtrl object in my MFC based Dialog.
My question is, how can I change the background color and the text color of the edit box ?
After an extensive search I tried the following code for a subclassed CDateTimeCtrl but this shows my background color only if I click in the DateTimeCtrl box.
BOOL CCHDateTimeCtrl::OnEraseBkgnd(CDC *pDC)
{
CRect rect;
pDC->GetClipBox(&rect);
pDC->FillSolidRect(&rect, getBackgroundColor());
return TRUE;
}
Hopefully someone has a suggestion.
Related
How Do I Set the Background Color of buttons including a Checkbox button?
I struggled to find the answer to this today - thinking it should be simple to answer this, but the information I stumbled on was less than helpful, so at the risk of duplicating stuff that's out there but I couldn't find, I'll make this quick'n'dirty how-to...
All 'Button' class windows send WM_CTLCOLORSTATIC to their parent window, which can then call ::SetBkColor((HDC)wParam, rgbBkColor), and return a brush for that color.
If this is all using system colors, then the brush handle doesn't need to be managed, you can simply ask for the ::GetSysColor(sysIndex), and return the ::GetSysColorBrush(sysIndex) for the returned brush.
If you're using a custom color, then you'll need to create your own brush and manage the handle for that.
I needed this code for a Message Box replacement, which has the upper part using a white background, and the lower part using a gray background, per the Windows standard message box. So my static control (icon) needed to be white, while my other buttons (including a "Don't ask again" checkbox) needed to have a gray background (checkboxes normally have a white background).
So, I handle WM_ERASEBKGND to paint the two portions of the background correctly, and then I handle WM_CLTLCOLORSTATIC to ensure that all buttons are properly "transparent" for the background that they appear on. In my case, the I used a "Static" control for the icon, which draws its background in gray, and a couple of push-buttons plus a checkbox button - which a checkbox button always paints its background in white, so both required a fix.
My example is using MFC, but hopefully you can translate that trivially enough for your purposes:
// add to the message map:
ON_MESSAGE(WM_CTLCOLORSTATIC, OnCtlColorStatic)
// create the implementation:
LRESULT CRTFMessageBox::OnCtlColorStatic(WPARAM wParam, LPARAM lParam)
{
// buttons and static controls (icon) send WM_CTLCOLORSTATIC, so we can force them to use the correct background color here...
const HDC hdc = (HDC)wParam;
const int idc = ::GetDlgCtrlID((HWND)lParam);
// choose a system color or brush based on if this is icon (static) or another control (a button)
const int idx = idc == IDC_STATIC ? COLOR_WINDOW : COLOR_3DFACE;
// select system color
::SetBkColor(hdc, GetSysColor(idx));
// return system brush (which we don't need to delete!)
return (LRESULT)GetSysColorBrush(idx);
}
I made an MFC application based on a dialog. Into this, I added a CStatusBar into bottom of the dialog addin this code into CMyDialog::InitDIalog() :
//Create status bar
BOOL bReturn = m_wndStatusBar.Create(this);
m_wndStatusBar.SetIndicators(indicators,1);
// Find the Size of Dialog box
CRect rect;
GetClientRect(&rect);
// Size the two panes
m_wndStatusBar.SetPaneInfo(0,IDD_INDICATOR_STATUS, SBPS_NORMAL, rect.Width());
// This is where we actually draw it
RepositionBars( AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, IDD_INDICATOR_STATUS ) ;
Note in the dialog editor, I had to keep some space at the bottom for the status bar. Work fine so far, status bar is correctly displayed :
BUT problem occurs when I resize the dialog. I put this code into CMyDialog::OnSize() :
RepositionBars( AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, IDD_INDICATOR_STATUS) ;
m_wndStatusBar.SetForegroundWindow();
The position of the CStatusBar window is correctly adapted... but status bar is displayed behind other controls.
Any idea how to solve this ? Why the call to SetForegroungWindow() is not working in this case ?
I have an mfc application. I have some richedit controls on the dialog. I want to show a yellow colored filled frame around the controls. What is the way to do this?
I tried to create one more rich edit ctrl around the existing richedit ctrl and use SetBackgroundColor on its variable, but it colors the entire area and other richedit ctrls become invisible. Also, I want to change the surrounding color at run time.
Please help me. I am stuck with this.
There may be a better way to accomplish this, but, the following should work. If you derive your own class from CRichEditCtrl, you can leverage the WM_NCPAINT message to render the border. Something like…
void RichEdit::OnNcPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
GetWindowRect(&rect);
ScreenToClient(rect);
CPen pen;
pen.CreatePen(PS_SOLID, 10, RGB(255, 255, 0));
dc.SelectObject(pen);
dc.Rectangle(&rect);
CHARFORMAT cf = { 0 };
int txtLen = GetTextLength();
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_ITALIC;
SetSel(txtLen, -1); ReplaceSel("Some text");
// Apply formating to the just inserted text.
SetSel(txtLen, GetTextLength());
SetSelectionCharFormat(cf);
SetFocus();
// Do not call CRichEditCtrl::OnNcPaint() for painting messages
}
Will render the border as Yellow, and, write the corresponding text. Here’s what it will look like.
I have created a CMFCPropertyGridCtrl on my form, however when setting the "Border" option to "True" in visual studio's Properties window for that control, it has no effects and the property grid always looks like it does in the below screenshot (with no border drawn around the control).
I also tried to enable the border from within my code but with no luck.
What are my options? Is this some kind of bug? I was thinking perhaps manually drawing a rectangle around the control to simulate a border as a last resort.
The border-less control:
http://img818.imageshack.us/img818/6337/8j1l.png
Thanks
So I found a solution myself
In the overridden OnPaint method of your dialog box add the following code:
CMFCPropertyGridCtrl* pPropGrid = (CMFCPropertyGridCtrl*) GetDlgItem(IDC_PROPSYSCHECK);
CPaintDC dc(this);
CPen BluePen(PS_SOLID, 1, RGB(137, 140, 149));
CPen *OldPen = dc.SelectObject(&BluePen);
CRect rect;
pPropGrid->GetWindowRect(&rect);
ScreenToClient(&rect);
dc.Rectangle(&rect);
dc.SelectObject(BluePen);
CDialogEx::OnPaint();
It draws a custom border around the control.
Visaul Studio contains the bug: Resource Editor does not add border style to the control description in dialog resource. So, add this style manually and be lucky :)
BOOL CMyDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
// add WS_BORDER style manualy...
GetDlgItem(IDC_PROPSYSCHECK)->ModifyStyle(0, WS_BORDER);
return TRUE;
}
I am using an animation control in my MFC dialog box to display an animation. I want to change the background color of this control to match the background color of my dialog box, which is white. I have tried using the OnCtlColor() method, but apparently the animation control does not invoke OnCtlColor().
Any help on how I can change animation control's background to white? Thanks
I am not sure if this would help or not but surely give this a try.
you have the option of displaying the original background color of the video or seeing through. When creating a video, its author can do it with transparency to allow seeing the color of the host. In this case, to display the color of the host while the video is playing, set the Transparent property to True. If you are creating the control programmatically, add the ACS_TRANSPARENT style:
BOOL CControlsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
RECT Recto = { 5, 5, 360, 360 };
Player->Create(WS_CHILD | WS_VISIBLE |
ACS_TRANSPARENT | ACS_AUTOPLAY,
Recto, this, 0x1884);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
...
For more info, kindly visit the below link and hopefully you should get some idea from this.
Link: http://www.functionx.com/visualc/controls/animation.htm
Hope this helps.
Cheers.