Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
{
// Construction
public:
	CtestdrawinstaticDlg(CWnd* pParent = nullptr);	// standard constructor

// Dialog Data
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_TEST_DRAW_IN_STATIC_DIALOG };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	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);
}
Posted
Updated 22-Jul-23 5:37am
Comments
Richard MacCutchan 21-Jul-23 8:05am    
"And I can't understand where is the problem"
You have not explained what problem you get when you try this.

 
Share this answer
 
According to the documentation at CStatic Class | Microsoft Learn[^], you need to override the DrawItem method.
 
Share this answer
 
Thank u all!
The problem is solved!
 
Share this answer
 
Comments
Richard MacCutchan 22-Jul-23 11:47am    
Then please add the solution, so others can benefit in the future.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900