I wrote a blog before. It's a way to draw a rectangle, but today's method is better than the previous one. It's convenient for me and my friends......
Direct code:
Write the following code in the header file
protected: HICON m_hIcon; CPoint m_ptbegin; CPoint m_ptEnd; BOOL m_blBtnDown; CDC m_dcMemory;
In the implementation file:
void CModifyRectDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: add message handler code and / or call defaults here //GetCursorPos(&point); CWnd *pWnd = GetDlgItem(IDC_STATIC); CDC *pDC = pWnd->GetDC(); CRect rcClient1; GetCursorPos(&point); pWnd->ScreenToClient(&point); pWnd->GetClientRect(&rcClient1); if(point.x> rcClient1.left&&point.x <rcClient1.right&&point.y <rcClient1.bottom&&point.y> rcClient1.top)//Limit click location { //ClipCursor(&rcClient); m_blBtnDown = TRUE; m_ptbegin = point; // pWnd->GetWindowRect(&rcClient); //ScreenToClient(&point); CBitmap oBitmap; oBitmap.CreateCompatibleBitmap(pDC, rcClient1.Width(), rcClient1.Height());//Create an object that is the same as the original if (m_dcMemory.m_hDC != NULL) { m_dcMemory.DeleteDC(); } m_dcMemory.CreateCompatibleDC(pDC);//First, set up the data in this DC, and then complete the refresh of this window in the DC copied to the window m_dcMemory.SelectObject(&oBitmap); m_dcMemory.BitBlt(0, 0, rcClient1.Width(), rcClient1.Height(), pDC, 0, 0, SRCCOPY);//Transfer the device of one memory block to another oBitmap.DeleteObject(); ReleaseDC(pDC); } CDialog::OnLButtonDown(nFlags, point); }
This is a double dc method. CreateCompatibleBitmap, CreateCompatibleDC copies a dc rectangle for drawing, and BitBlt transfers the device of one memory block to another memory block for display
void CModifyRectDlg::OnMouseMove(UINT nFlags, CPoint point) { // TODO: add message handler code and / or call defaults here if (m_blBtnDown==TRUE) { CWnd *pWnd = GetDlgItem(IDC_STATIC); CDC *pDC = pWnd->GetDC(); CRect rcClient; pWnd->GetClientRect(&rcClient); GetCursorPos(&point); pWnd->ScreenToClient(&point); if(point.x> rcClient.left&&point.x <rcClient.right&&point.y <rcClient.bottom&&point.y> rcClient.top) { pDC->BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &m_dcMemory, 0, 0, SRCCOPY); pDC->SetROP2(R2_NOTXORPEN); //Transparent rectangle CRect rc(m_ptbegin, point); rc.NormalizeRect(); pDC->Rectangle(&rc); //ScreenToClient(&rc);// ReleaseDC(pDC); } } CDialog::OnMouseMove(nFlags, point); }
void CModifyRectDlg::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: add message handler code and / or call defaults here m_blBtnDown = FALSE; CWnd *pWnd = GetDlgItem(IDC_STATIC); CDC *pDC = pWnd->GetDC(); pDC->SetROP2(R2_NOTXORPEN); CPen pen(PS_SOLID,2,RGB(0,255,0));//Draw a colored rectangle with a brush CPen* pOldPen; pOldPen=pDC->SelectObject (&pen); CRect rcClient; pWnd->GetClientRect(&rcClient); GetCursorPos(&point); pWnd->ScreenToClient(&point); if(point.x> rcClient.left&&point.x <rcClient.right&&point.y <rcClient.bottom&&point.y> rcClient.top) { m_ptEnd = point; CRect rc(m_ptbegin, m_ptEnd); rc.NormalizeRect(); pDC->Rectangle(&rc); m_dcMemory.DeleteDC();//Remove device context } //Invalidate(); CDialog::OnLButtonUp(nFlags, point); }
Results: