Trouble getting device context
-
I don't know how to correctly acquire the device context for a control. If I put similar code in the OnCtlColor routine where a pointer to the device context is provided, the code works. However, when I try setting the device context, the code doesn't work. Two samples are provided - the first is the code that doesn't work. I appreciate any help fixing it. Thank-you. This code compiles and runs without error, but does not achieve the color change. (c_navValid is a text box control)
LOGFONT lf;
CDC* dc = new CDC;
dc->Attach(c_navValid.GetDC()->m_hDC);GetFont()->GetObject(sizeof(lf), &lf);
font.CreateFontIndirect(&lf);dc->SelectObject(&font);
dc->SetTextColor(RGB(0,100,0));
font.DeleteObject();This code works.
HBRUSH CNavDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
W_Position_Data_Type* pPositionMsg;
pPositionMsg = (W_Position_Data_Type*)&CInMsgs::wPositionMsg;HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_NAV_VALID)
{
if (pPositionMsg->Mode_Word.Nav_Valid)
{
LOGFONT lf;
GetFont()->GetObject(sizeof(lf), &lf);
font.CreateFontIndirect(&lf);
pDC->SelectObject(&font);
pDC->SetTextColor(RGB(0,100,0));
font.DeleteObject();
}
} -
I don't know how to correctly acquire the device context for a control. If I put similar code in the OnCtlColor routine where a pointer to the device context is provided, the code works. However, when I try setting the device context, the code doesn't work. Two samples are provided - the first is the code that doesn't work. I appreciate any help fixing it. Thank-you. This code compiles and runs without error, but does not achieve the color change. (c_navValid is a text box control)
LOGFONT lf;
CDC* dc = new CDC;
dc->Attach(c_navValid.GetDC()->m_hDC);GetFont()->GetObject(sizeof(lf), &lf);
font.CreateFontIndirect(&lf);dc->SelectObject(&font);
dc->SetTextColor(RGB(0,100,0));
font.DeleteObject();This code works.
HBRUSH CNavDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
W_Position_Data_Type* pPositionMsg;
pPositionMsg = (W_Position_Data_Type*)&CInMsgs::wPositionMsg;HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_NAV_VALID)
{
if (pPositionMsg->Mode_Word.Nav_Valid)
{
LOGFONT lf;
GetFont()->GetObject(sizeof(lf), &lf);
font.CreateFontIndirect(&lf);
pDC->SelectObject(&font);
pDC->SetTextColor(RGB(0,100,0));
font.DeleteObject();
}
}Use
CClientDC
orCWindowDC
to get a DC for a window. Use the former to draw in the window's client area only, or the latter if you need to draw in the non-client area too. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ Wizard needs food, badly! -
Use
CClientDC
orCWindowDC
to get a DC for a window. Use the former to draw in the window's client area only, or the latter if you need to draw in the non-client area too. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ Wizard needs food, badly!Could you please show me the code? To use CClientDC I need a pointer to a window. How do I convert the static control to a window pointer?
cCliendDc dc(c_navValid);
I know isn't correct. What parameter do I pass to CClientDC to get a dc for the control? Thanks.