程序想要获取Picture控件在对话框中的位置,并从对话框起点画直线到控件正中点。程序如下:
CRect DrawLine;
GetDlgItem(IDC_DrawLine)->GetWindowRect(&DrawLine);
CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(255,0,0));
dc.SelectObject(&pen);
CPoint point;
point.x=(DrawLine.left+DrawLine.right)/2;
point.y=(DrawLine.top+DrawLine.bottom)/2;
dc.MoveTo(0,0);
dc.LineTo(point);
不知为何,画出的直线位置显示不对。
请大虾们指点迷津……

解决方案 »

  1.   

    画直线的两端点是相对于整个屏幕的,要相对于控件,需要做转化运算
            CRect DrawLine,WindowRect; 
    GetDlgItem(IDC_DrawLine)->GetWindowRect(&DrawLine); 
    this->GetWindowRect(&WindowRect);
    CClientDC dc(this); 
    CPen pen(PS_SOLID,1,RGB(255,0,0)); 
    dc.SelectObject(&pen); 
    dc.MoveTo((-WindowRect.left + DrawLine.left),(-WindowRect.top + DrawLine.top -29));
    dc.LineTo((-WindowRect.left +DrawLine.CenterPoint().x),(-WindowRect.top +DrawLine.CenterPoint().y-29));29是偏移调整,我自己是这么做的,也不是很明白为什么,不同机子还不太一样的
      

  2.   

    GetDlgItem(IDC_DrawLine)->GetWindowRect(&DrawLine);我用GetClientRect