Hi all. I'm trying to draw 2 lines in MFC CStatic element.
I will be grateful for any advice and help
What I have tried:
This code is exist in project.
And I can't understand where is the problem
class CtestdrawinstaticDlg : public CDialogEx
{
public:
CtestdrawinstaticDlg(CWnd* pParent = nullptr);
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_TEST_DRAW_IN_STATIC_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
CStatic m_StaticDraw;
};
BEGIN_MESSAGE_MAP(CtestdrawinstaticDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
void CtestdrawinstaticDlg::OnPaint()
{
CPaintDC dc(this);
CRect rect;
m_StaticDraw.GetClientRect(&rect);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
memDC.FillSolidRect(rect, RGB(255, 255, 255));
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = memDC.SelectObject(&pen);
memDC.MoveTo(0, 0);
memDC.LineTo(rect.right, rect.bottom);
memDC.MoveTo(rect.right, 0);
memDC.LineTo(0, rect.bottom);
memDC.SelectObject(pOldPen);
memDC.SelectObject(pOldBitmap);
memDC.DeleteDC();
dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
}