我生成了一个单文档的程序,我要实现的是在单文档的view中动态生成一个按钮button,然后鼠标拖动按钮的效果。现在的问题是能动态生成按钮,但是无法拖动,请高人帮忙看看,代码是: 
void CText_2View::OnInitialUpdate() 

CView::OnInitialUpdate(); 
CButton *m_pButton=new CButton;  
ASSERT_VALID(m_pButton);  
m_pButton->Create(_T("Title"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(20,20,100,100),this,IDC_MYBUTTON);  
// TODO: Add your specialized code here and/or call the base class 

void CText_2View::OnLButtonUp(UINT nFlags, CPoint point) 

// TODO: Add your message handler code here and/or call default 
Mou=FALSE; 
ReleaseCapture(); 
CView::OnLButtonUp(nFlags, point); 
} void CText_2View::OnMouseMove(UINT nFlags, CPoint point) 

// TODO: Add your message handler code here and/or call default 
CRect rect,rt; 
GetClientRect(&rt); 
if(Mou) 

if(rect.PtInRect(point)) 

m_pButton->GetClientRect(&rt); 
rt.left = point.x; 
rt.right = rt.right + point.x; 
rt.top = point.y; 
rt.bottom = rt.bottom + point.y; 
m_pButton->MoveWindow(&rt); 

} CView::OnMouseMove(nFlags, point); 
} void CText_2View::OnLButtonDown(UINT nFlags, CPoint point) 

// TODO: Add your message handler code here and/or call default 
Mou=TRUE; 
SetCapture(); 
CView::OnLButtonDown(nFlags, point); 

解决方案 »

  1.   

    不用这么麻烦,你在OnLButtonDown时:PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));即可
      

  2.   

    而且有个问题HTCAPTION是点击标题,WM_NCLBUTTONDOWN是点击标题栏吧。我现在是动态生成一个BUTTON,然后拖动这个BUTTON
      

  3.   

    自己派生一个按钮类,响应按钮的OnLButtonDown
    写上:
    PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y)); 
      

  4.   

    按照你说的派生一个CButton类后成功了,按钮拖动。但是起初我并不是想这么做,想在view中先动态生成一个按钮,然后在view类中去操作这个动态按钮的拖动,是这样。