文本框在主对话框里移动
创建Edit的扩展类CMyEditEx
(1)鼠标按下
void CMyEditEx::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
pPoint=point;
SetTimer(1,5,NULL); // 引发定时器
CEdit::OnLButtonDown(nFlags, point);
}
(2)鼠标弹起
void CMyEditEx::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
KillTimer(1); // 关闭定时器
CEdit::OnLButtonUp(nFlags, point);
}
(3)定时时间
void CMyEditEx::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
//获得鼠标当前位置,转换为父窗口内的坐标
CPoint cursor_Pos;
GetCursorPos(&cursor_Pos);//该函数检取光标的位置,以屏幕坐标表示。
GetParent()->ScreenToClient(&cursor_Pos); //该函数把屏幕上指定点的屏幕坐标转换成用户坐标。
CPoint edit_Pos=pPoint;
ClientToScreen(&edit_Pos);//该函数将指定点的用户坐标转换成屏幕坐标
GetParent()->ScreenToClient(&edit_Pos);
// 获得控件的大小,并且转换到父窗口的坐标,主要是为了获得左上角的坐标,方便下面的movewindow调用
CRect edit_rect;
GetWindowRect(edit_rect);//该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
GetParent()->ScreenToClient(&edit_rect);
CRect parentRect;
GetParent()->GetClientRect(parentRect);
// 利用向量相等的性质求得鼠标移动后的新左上角坐标
int x=edit_rect.left+(cursor_Pos.x-edit_Pos.x);
int y=edit_rect.top+(cursor_Pos.y-edit_Pos.y);
if (x<0)
{
x=0;
}
if (y<0)
{
y=0;
}
if (x+edit_rect.Width()>parentRect.Width())
{
x = parentRect.Width() - edit_rect.Width();
}
if (y+edit_rect.Height()>parentRect.Height())
{
y = parentRect.Height() - edit_rect.Height();
}
MoveWindow(x,y,edit_rect.Width(),edit_rect.Height());
}
CEdit::OnTimer(nIDEvent);
}
(4)在DLG窗口中添加改扩展类的控件就OK了
这是一个大哥回答的,这里CPoint edit_Pos=pPoint;的pPoint没定义,还有那里它获得主框和文本框的坐标,要不要用到他们的ID,
我在我的对话框里建了一个文本框,它还是不能在对话框里移动。
求哪位下下指导指导!!