第十讲

    技术2022-05-19  24

    一、 创建一个简单的绘图软件 新建一个Graphic的可执行程序: 在资源中,创建一个对话框,改变其ID为IDD_DIG_SETTING,更名其Caption为“设置”。 线的宽度和画笔的选择: 1、 在对话框上添加一个静态文本框和编辑框,用来接收设置画笔的宽度。并且改变编辑框的ID为IDC_LINE_WIDTH。 2、 给对话框添加一个和它相对应的类:CSettingDlg。 3、 给编辑框添加一个和它对应的UINT型的值变量:m_nLineWidth。 4、 在菜单资源中,添加一个子菜单:绘图。其下的菜单项分别为:点、直线、矩形、椭圆、设置。 5、 分别为每个菜单项添加命令响应函数到View类中。 6、 在View类中,添加三个私有变量: UINT m_LineWidth;//保存线的宽度。 CPoint m_ptOrigin;//保存鼠标按下的点。 UINT m_nDrawType;//保存画笔的类型。 并在构造函数中进行初始化: m_nDrawType=0; m_ptOrigin=0; m_LineWidth=1; 7、 在菜单项的响应函数中,作如下操作: void CGraphic10View::OnDot() { // TODO: Add your command handler code here m_nDrawType=1; } void CGraphic10View::OnLine() { // TODO: Add your command handler code here m_nDrawType=2; } void CGraphic10View::OnRectangle() { // TODO: Add your command handler code here m_nDrawType=3; } void CGraphic10View::OnEllipse() { // TODO: Add your command handler code here m_nDrawType=4; } void CGraphic10View::OnSetting() { // TODO: Add your command handler code here CSettingDlg2 dlg; dlg.m_nLineWidth=m_LineWidth;//使每次启动该对话框时,都显示以前设置的值 if(IDOK==dlg.DoModal()) { m_LineWidth=dlg.m_nLineWidth;//将文本框内容保存在m_LineWidth中。 } } 8、 在View类中,再添加WM_ONLBUTTONDOWN和WM_ONLBUTTONUP消息响应函数,并进行如下操作: void CGraphic10View::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default m_ptOrigin=point; CView::OnLButtonDown(nFlags, point); } void CGraphic10View::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CClientDC dc(this); CPen pen(PS_SOLID,m_LineWidth,RGB(255,0,0)); CPen *pOldPen=dc.SelectObject(&pen); CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)); CBrush *pOldBrush=dc.SelectObject(pBrush); switch(m_nDrawType) { case 1: dc.SetPixel(point,RGB(255,0,0)); break; case 2: dc.MoveTo(m_ptOrigin); dc.LineTo(point); break; case 3: dc.Rectangle(CRect(m_ptOrigin,point)); break; case 4: dc.Ellipse(CRect(m_ptOrigin,point)); break; default: break; } dc.SelectObject(pOldPen); dc.SelectObject(pOldBrush); CView::OnLButtonUp(nFlags, point); } 线类型的选择: 1、 在“设置”对话框中,再添加一个单选框组件,将其设置为一组,并且关联一个变量m_nLineStyle。 2、 在View类中,再增加一个成员变量:m_LineStyle。在构造函数中,初始化为-1。 3、 在CGraphic10View类中的OnSetting()函数中,再增加如下: void CGraphic10View::OnSetting() { // TODO: Add your command handler code here CSettingDlg2 dlg; dlg.m_nLineWidth=m_LineWidth;//使每次启动该对话框时,都显示以前设置的值 dlg.m_nLineStyle=m_LineStyle; if(IDOK==dlg.DoModal()) { m_LineWidth=dlg.m_nLineWidth;//将文本框内容保存在m_LineWidth中。 m_LineStyle=dlg.m_nLineStyle;//将单选框的选择结构保存在m_LineStyle中。 } } 4、 在OnLButtonDown()中,作相应的修改。 颜色对话框的创建: 1、 在子菜单下再创建一个菜单项,叫做“颜色”。并给它进行命令响应。 2、 在View类中,添加一个私有变量:COLORREF m_clr;用来保存选择的颜色。 3、 在颜色的响应函数中,做如下操作: void CGraphic10View::OnColor() { // TODO: Add your command handler code here CColorDialog dlg;//定义一个颜色对话框的对象 dlg.m_cc.Flags |= CC_RGBINIT;//表示m_cc结构体可以被初始化。 dlg.m_cc.rgbResult=m_clr;//用之前选择的颜色进行初始化。 if(IDOK==dlg.DoModal()) { m_clr=dlg.m_cc.rgbResult;//选择的颜色保存在m_cc结构体的rgbResult中 } } 4、 在OnLButtonDown()中,作相应的修改。 字体对话框的创建: 1、 在子菜单中,新建一个菜单项“字体”,并给它进行命令响应函数。 2、 在View类中,新增两个私有变量: CString m_str; CFont m_font; 3、 在字体的响应函数中,做如下操作: void CGraphic10View::OnFont() { // TODO: Add your command handler code here CFontDialog dlg; if(IDOK==dlg.DoModal()) { if(m_font.m_hObject) m_font.DeleteObject(); m_font.CreateFontIndirect(dlg.m_cf.lpLogFont);//用lpLogFont初始化m_font。 m_str=dlg.m_cf.lpLogFont->lfFaceName;//把字体的名字保存到m_str中。 Invalidate();//关闭该窗口,并让其进行重绘。 } } 4、 在View类中的OnDraw()函数中,添加如下语句: CFont *pOldFont=pDC->SelectObject(&m_font); pDC->TextOut(0,0,m_str);//输出字体的名字。 pDC->SelectObject(pOldFont); 示例的创建: 1、 在“设置”对话框上,创建一个“示例”组合框,改变其ID为IDC_SAMPLE。 2、 分别对编辑框和每个单选按钮进行命令响应,编辑其每个命令响应函数内容为:Invalidate();目的是为了关闭当前窗口,从而进行重绘,转到OnPaint()中。从而避免了在每个命令响应函数中,要重复写一样的代码,只需在OnPaint()函数中,写一遍就可以了。 3、 在CSettingDlg类中,添加一个COLORREF m_clr2的公有变量。在View类中的OnSetting()函数中,添加如下语句:dlg.m_clr2=m_clr;将当前选择的颜色保存到m_clr2中。 4、 在CSettingDlg类中,添加一个消息响应函数,就是OnPaint(),就是在窗口进行重绘时,要调用该函数。在其中做如下操作: void CSettingDlg2::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here UpdateData();//将控件内容保存到值变量当中去。 CPen pen(m_nLineStyle,m_nLineWidth,m_clr2); CPen *pOldPen=dc.SelectObject(&pen); CRect rect; GetDlgItem(IDC_SAMPLE)->GetWindowRect(&rect); ScreenToClient(&rect);//屏幕坐标转换到客户区域坐标 dc.MoveTo(rect.left+20,rect.top+rect.Height()/2); dc.LineTo(rect.right-20,rect.bottom-rect.Height()/2); // Do not call CView::OnPaint() for painting messages } 改变对话框上控件的背景颜色: 1、 在CSettingDlg类中,定义一个私有变量:CBrush m_brush; 2、 在CSettingDlg类中,增加一个WM_CTLCOLOR消息处理函数,在其中作如下处理: HBRUSH CSettingDlg2::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here if(pWnd->GetDlgCtrlID()==IDC_LINE_STYLE) { pDC->SetTextColor(RGB(255,0,0));//设置控件字体的颜色 pDC->SetBkMode(TRANSPARENT);//把控件的背景设置成透明的 return m_brush; } if(pWnd->GetDlgCtrlID()==IDC_LINE_WIDTH) { pDC->SetTextColor(RGB(255,0,0)); pDC->SetBkColor(RGB(0,0,255));//对于编辑框,应该设置其背景色。 return m_brush; } // TODO: Return a different brush if the default is not desired return hbr; } 改变对话框上字体: 1、 添加一个静态文本框,改变其ID和内容。 2、 在在CSettingDlg类中,定义一个私有变量:CFont m_font;在构造函数中,初始化为:m_font.CreatePointFont(200,"华文行楷"); 3、 在OnCtlColor()函数中,添加如下: if(pWnd->GetDlgCtrlID()==IDC_TEXT) { pDC->SelectObject(&m_font); } 设置按钮的字体颜色: 1、 新增加一个继承自CButton的类CTestBtn类,在该类中增加一个虚函数: DrawItem(),这个函数是在对话框创建时,自动调用的。 2、 在DrawItem()函数中,做如下操作: void CTestBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item UINT uStyle = DFCS_BUTTONPUSH; // This code only works with buttons. ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON); // If drawing selected, add the pushed style to DrawFrameControl. if (lpDrawItemStruct->itemState & ODS_SELECTED) uStyle |= DFCS_PUSHED; // Draw the button frame. ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle); // Get the button's text. CString strText; GetWindowText(strText); // Draw the button text using the text color red. COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0)); ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER); ::SetTextColor(lpDrawItemStruct->hDC, crOldColor); } 3、 对OK按钮关联一个控件变量,基类是CBestBtn,并且在OK按钮的属性里,将其的“Styles”该为Owner Draw。 (这三步就搞定,但是要是改变按钮的背景色,我还没有练到火候) 在窗口中显示位图: 1、 在资源中,添加一个位图资源,把老婆的照片经过画图程序改变其格式为.bmp,导入到工程中。 2、 在View类中,添加一个消息响应函数WM_ERASEBKGND,在其中作如下操作: BOOL CGraphic10View::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); BITMAP bmp;//定义一个位图结构体 bitmap.GetBitmap(&bmp);//将位图的各方面信息填充到bmp中。 CDC dcCompatible;//定义一个兼容的DC dcCompatible.CreateCompatibleDC(pDC);//创建一个兼容的DC dcCompatible.SelectObject(&bitmap);//将位图加载到兼容DC中 CRect rect; GetClientRect(&rect); //pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY); //BitBlt()是像素一对一的复制。多大窗口,就只能显示多大的图片。 pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY); // StretchBlt()是根据图片的大小而调整窗口的大小。 return TRUE; //return CView::OnEraseBkgnd(pDC); }  


    最新回复(0)