学习MFC(二)

1.添加对话框
step1: 在Resource View下,右键单击Dialog->Insert Dialog,出现对话框并且有Dialog Editor。
step2: 拖动Static Text到对话框后,在右下方Properties->Caption 添加静态文本。
step3: 拖动Edit Control到对话框,右键单击该对话框->Add Class,添加类名,该类属于这个对话框类;再右键单击该对话框->Add Variable,这样添加的变量就是之前这个类的成员变量。注意,要勾选Control variable, 并且Category下选择Value, 然后选择Variable type, 再添加变量名。
step4: 通过在类..view下创建鼠标响应事件,或者通过添加菜单,都可以同对话框中的control variable关联起来。
如下产生随机点:

 

void CApproxDelaunayTriangulationView::OnOperationGeneratepoints() { // TODO: Add your command handler code here srand(time(NULL)); CPointsNumber dlg(10); if (dlg.DoModal() == IDOK) //IDOK是OK按钮的ID, 当按下ok时,执行.. { inputPoints.clear(); CRect rect; GetClientRect(&rect);//get client window for (int i = 0; i < dlg.m_number; i++) { CPoint point(rand()%rect.Width(), rand()%rect.Height()); inputPoints.push_back(point); } Invalidate(TRUE); } }

 

2. 橡皮筋功能
用Invalidate()来实现

 

void CRubberBandView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default startPoint = point; CView::OnLButtonDown(nFlags, point); } void CRubberBandView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default endPoint = point; CView::OnLButtonUp(nFlags, point); } void CRubberBandView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if (nFlags == MK_LBUTTON) { endPoint = point; Invalidate(TRUE); } CView::OnMouseMove(nFlags, point); }

 

用擦除之前那条线来实现

 

void CRubberBandView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default Invalidate(TRUE);//在鼠标按下的那一刻刷新,在画新线时,之前那条线没有了 startPoint = point; endPoint = point; //初始化endPoint CView::OnLButtonDown(nFlags, point); } void CRubberBandView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default endPoint = point; Invalidate(TRUE);//动作结束,刷新调用OnDraw()函数 CView::OnLButtonUp(nFlags, point); } void CRubberBandView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CClientDC dc(this); if (nFlags == MK_LBUTTON) //左键鼠标按下的状态 { dc.SetROP2(R2_NOT); //用背景反色来画旧线 dc.MoveTo(startPoint); dc.LineTo(endPoint); endPoint = point; // 该point就是最新的点,表示画新线 dc.MoveTo(startPoint); dc.LineTo(endPoint); } CView::OnMouseMove(nFlags, point); }

你可能感兴趣的:(command,null,Class,mfc,dialog)