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了
http://idcnews.net/html/edu/20071229/79440.html
我就不帮你写了
button.Create("Click Me", BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD, CRect(0,0,100,100), this, NULL);
virtual BOOL Create(
LPCTSTR lpszCaption,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID
);
消息响应函数通过Ctrl+W,由向导为你自动添加
(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了
如果是菜单有ON_COMMAND_RANGE 宏,可以指定一个id范围,然后到同一个函数中去处理
但是 ON_BN_CLICKED就没有类似的机制了
现在用了一个笨办法,先写好了一堆消息映射,和响应函数,比如20个
但是要创建的控件上限并不可知,超过20个以后怎么办?有没有什么好的办法