There is a call back function in my main code which included by Switch case statements. After each case I defined a SetWindowText
function to print a text in a static Control which created on a dialog (or parent window),something like this:
::SetWindowText(GetDlgItem(IDC_STATIC)->m_hWnd, "loading");
I am going to set background of static control as the background of dialog. Everything goes well except the text of all the cases which place on each other and I receive a static control with overlapped texts, somthing like this:
I don't know why in each step it does not close the static window to avoid such kind of problems.
I added OnEraseBkgnd
, OnDestroy
, OnCtlColor
messages as follow:
BOOL CmainDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap* pOldbitmap = dcMemory.SelectObject(&CBmp);
CRect rcClient;
GetClientRect(&rcClient);
const CSize& sbitmap = bitmapSize;
pDC->BitBlt(0, 0, sbitmap.cx, sbitmap.cy, &dcMemory, 0, 0, SRCCOPY);
dcMemory.SelectObject(pOldbitmap);
return TRUE;
}
HBRUSH CmainDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_STATIC)
//Example of changing Text colour specific to a certain
//Static Text Contol in this case IDC_STATIC.
{
pWnd->GetExStyle() & (WS_EX_TRANSPARENT);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255, 255, 255));
}
if (pWnd->GetDlgCtrlID() == IDC_OPERATION)
{
pWnd->GetExStyle() & (WS_EX_TRANSPARENT);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255, 255, 0));
// Return handle to our CBrush object
}
return reinterpret_cast<HBRUSH>(GetStockObject(NULL_BRUSH));
}
void CmainDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
Background.DeleteObject(); // Delete Background bitmap
BrushHol.DeleteObject();
}
//subclass the static control, just to make sure the code is the only one handling WM_ERASEBKGND and WM_PAINT messages.
void CmainDlg::PreSubclassWindow()
{
CWnd::PreSubclassWindow();
const LONG_PTR exStyle = GetWindowLongPtr(m_hWnd, GWL_EXSTYLE);
SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, exStyle | WS_EX_TRANSPARENT);
}
Update:
I commented OnEraseBkgnd
, OnDestroy
, OnCtlColor
functions. So I received the same overlapped texts and now with a bit more certainty I can say the problem comes from setWindowText
because after finishing each Case the text remains on static control window which I defined in each of switch case statments.
I tried to use the following commands but nothing happened:
EnableWindow( GetDlgItem(m_hWnd, IDC_STATIC), FALSE);
m_static.EnableWindow(FALSE);
or
::SetDlgItemText(m_hWnd, IDC_STATIC, "");
I'll appreciate any help.
SetBkMode(TRANSPARENT)
function, the static control loses its parent background! – Braiano