Create a new dialog box project;
Add 2 buttons, ID is IDC_ONE, IDC_TWO, and add another GO! Button
For GO! Button to add click message processing function;
Click code
void CTwotwoDlg::OnButton3()
{
// TODO: Add your control notification handler code here
CPen pen(PS_DOT, 3, RGB(0, 0, 0));//创建一个画笔
//CWnd *pwnd = GetDlgItem(IDC_STATIC);//获取画线所在控件
// CClientDC dc(pwnd);
CClientDC dc(this);//this表示在当前对话框中
dc.SelectObject(pen);//选择画笔
CRect rect1, rect2;
GetDlgItem(IDC_ONE)->GetWindowRect(&rect1);//获取控件(这里控件ID为IDC_ONE )相对于屏幕的位置
ScreenToClient(rect1);//转化为对话框上的相对位置
GetDlgItem(IDC_TWO)->GetWindowRect(&rect2);
ScreenToClient(rect2);
dc.MoveTo(rect1.left + (rect1.right - rect1.left) / 2, rect1.top + (rect1.bottom - rect1.top) / 2);//控件1中心点为起始位置
dc.LineTo(rect2.left + (rect2.right - rect2.left) / 2, rect2.top + (rect2.bottom - rect2.top) / 2);//连接两个控件中心点
}
Run; the effect is as follows; the connection is realized; but the effect is not spicy;