MFC在Picture控件上绘制ROI

前面介绍了 MFC对话框程序绘制ROI,但是ROI不是绘制在Picture控件上,而是整个窗体,离我们项目中的需求还有一段距离。待继续实现:

1.ROI的坐标改为相对Picture控件左上顶点为原点;

2.在Picture控件上绘制,移动ROI;

3.绘制区域需要保证在Picture控件之内;

 

1.ROI的坐标改为相对Picture控件左上顶点为原点

解决办法:

1)定义一个CRect 变量,存储Picture控件相对对话框左上顶点的位置

CRect m_RectPicCtl;

在初始化时添加如下两行代码即可,

//转化为对话框上的相对位置

ScreenToClient(m_RectPicCtl);

//获得客户区域

GetDlgItem(IDC_STATIC_SHOWIMAGE)->GetWindowRect(&m_RectPicCtl);      

添加一个获取相对坐标的函数,输入参数为当前点,输出为Picture控件上的点相对于Picture控件左上顶点的坐标

CPoint  CXXXDlg::GetRelativeCoordinates(CPoint curpoint)

{

         CPoint point;

         point.x = curpoint.x - m_RectPicCtl.left;

         point.y = curpoint.y - m_RectPicCtl.top;

         return point;

}

2.在Picture控件上绘制,移动ROI

1)图片需要重绘

在Paint函数中添加显示图片的函数,绘制和刷新Picture区域

void CXXXDlg::OnPaint()

{       

         CPaintDC dc(this);

         if (IsIconic())

         {                

                   //CPaintDC dc(this); // 用于绘制的设备上下文

                   SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

                   // 使图标在工作区矩形中居中

                   int cxIcon = GetSystemMetrics(SM_CXICON);

                   int cyIcon = GetSystemMetrics(SM_CYICON);

                   CRect rect;

                   GetClientRect(&rect);

                   int x = (rect.Width() - cxIcon + 1) / 2;

                   int y = (rect.Height() - cyIcon + 1) / 2;

                   // 绘制图标

                   dc.DrawIcon(x, y, m_hIcon);            

         }

         else

         {                

                   ShowMethord1();

                   CRect rtlbl;

                   GetDlgItem(IDC_STATIC_SHOWIMAGE)->GetWindowRect(&rtlbl);

                   //ScreenToClient(&rtlbl); //

                   InvalidateRect(&rtlbl);//只刷新我的图片框

                   UpdateWindow();                  

                   //ShowMethord1();

                   if (m_IsChose)

                   {

                            //若选择了该区域,则显示边框以及8个调整点
                            m_rctCurTracker.Draw(&dc);
                            //m_rctCurTracker.Draw(dc2);
                            //输出坐标信息
                            CRect rect = m_rctCurTracker.m_rect;

                            CPoint pt1 = GetRelativeCoordinates(m_rctCurTracker.m_rect.TopLeft());


                            CString strLT = _T("");
                            //strLT.Format(_T("%d,%d"),rect.left,rect.top);
                            strLT.Format(_T("%d,%d"),pt1.x, pt1.y);
                            SetDlgItemTextA(IDC_EDIT_CURROILT, strLT);
                            CPoint pt2 = GetRelativeCoordinates(m_rctCurTracker.m_rect.BottomRight());
                            CString strRB = _T("");
                            strRB.Format(_T("%d,%d"), pt2.x, pt2.y);
                            SetDlgItemTextA(IDC_EDIT_CURROIRB, strRB);


                            //输出当前ROI的长宽信

                            CString strWidth = _T("");
                            strWidth.Format(_T("%d"), rect.Width());                        

                            SetDlgItemTextA(IDC_EDIT_ROIWIDTH, strWidth);


                            CString strHeight = _T("");

                            strHeight.Format(_T("%d"), rect.Height());

                            SetDlgItemTextA(IDC_EDIT_ROIHEIGHT, strHeight);

                   }                           

                   CPen pen(PS_SOLID, 1, RGB(100, 255, 200));

                   dc.SelectObject(&pen);

                   //dc2->SelectObject(&pen);

                   CBrush *pbrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));

                   dc.SelectObject(pbrush);

                   //dc2->SelectObject(pbrush);

                   CRect rect;

                   m_rctCurTracker.GetTrueRect(&rect);//得到矩形区域的大小

                   dc.Rectangle(&rect);//画出矩形

                   //dc2->Rectangle(&rect);//画出矩形

                   CSize rct_size;

                   for (int i = 0; i < MAX_RECT_NUM; i++)

                   {

                            m_rctTracker[i].GetTrueRect(&rect);//得到矩形区域的大小

                            rct_size = m_rctTracker[i].m_rect.Size();



                            if (rct_size.cx * rct_size.cy == 0 || i == m_rctChoseNum)

                            {

                                     continue;

                            }

                            dc.Rectangle(&rect);//画出矩形

                            //dc2->Rectangle(&rect);//画出矩形

                   }

                   CDialogEx::OnPaint();
         }
}

3.解决绘制区域确保在Picture控件之内

1)鼠标左键按下,点必须在Picture控件内;

2)当鼠标拖动ROI至Picture控件外的区域时,需要整体刷新一遍

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页