图形的绘制

一、画点、线、矩形和椭圆
1,在View类中定义成员变量UINT m_DrawType;
2,在View类构造函数中初始化 m_DrawType=0;
3,添加四个菜单项,分别在其响应事件中将m_DrawType设置为1(画点),2(画线),3(画矩形),4(画椭圆);
4,添加成员变量CPoint m_ptOrigin
5,在OnLButtonDown中设置 m_ptOrigin=point;
6,在OnLButtonUp中添加以下代码:
 void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
 {
  CClientDC dc(this);
  CPen pen(m_LineType,m_LineWidth,m_clr);
  dc.SelectObject(&pen);
  dc.SelectObject(CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)));
   //透明画刷
  switch(m_DrawType)
  {
  case 0:
   break;
  case 1:
   dc.SetPixel(point,RGB(255,0,0));
   break;
  case 2:
   dc.MoveTo(m_Origin);
   dc.LineTo(point);
   break;
  case 3:
   dc.Rectangle(CRect(m_Origin,point));
   break;
  case 4:
   dc.Ellipse(CRect(m_Origin,point));
   break;
  }
  CView::OnLButtonUp(nFlags, point);
 }

二、创建颜色对话框
 CColorDialog dlg;
    dlg.m_cc.Flags|=CC_RGBINIT|CC_FULLOPEN; //颜色对话框关闭再打开时保持原来的设置
    dlg.m_cc.rgbResult=m_clr;
    if(IDOK==dlg.DoModal())  //用户点击的是OK按钮
    {
       m_clr=dlg.m_cc.rgbResult;//返回的颜色
    }
三、创建字体对话框(用用户选择的字体输入字体名称)
 CFontDialog dlg;
    if(IDOK==dlg.DoModal())
    { 
  if (m_font.m_hObject)  //若成员变量与某一对象关联,则删除该关联
  m_font.DeleteObject();
    m_font.CreateFontIndirect(dlg.m_cf.lpLogFont);
    m_strFontName=dlg.m_cf.lpLogFont->lfFaceName;
    Invalidate();//Invalidates the entire client area of CWnd
    }

四、改变控件的背景色 CWnd::OnCtlColor
HBRUSH CSetDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 if(pWnd->GetDlgCtrlID()==IDC_LINE_STYLE) //组框
 {
  pDC->SetTextColor(RGB(0,0,255));
  pDC->SetBkMode(TRANSPARENT);//改变文字背景为透明
 }
 if(pWnd->GetDlgCtrlID()==IDC_LINE_WIDTH)//编辑框
 {
  pDC->SetTextColor(RGB(0,0,255));
  //pDC->SetBkMode(TRANSPARENT);//改变文字背景为透明
  pDC->SetBkColor(RGB(0,255,0));
  return m_brush;
 }
 //此处不能更改Button的背景色
 return hbr;//此处返回控件的背景色
}

五、改变Button控件的文本颜色(重载CButton::DrawItem函数)
1、添加处定义类CMyButton,基类为CButton
2、为Button添加成员变量 CMyButton m_TestBtn;
3、重写CMyButton类的虚函数DrawItem  //查看MSDN
 void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
 {
    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);
 }
六、利用CSXBtn类更改Button控件背景(为Button添加成员变量 CSXBtn m_btnSX;)(孙鑫编写)
七、利用CBtnST类更改Button控件背景(网友编写)Graphic工程里
1、为Button控件添加成员变量 CBtnST m_btnST;
2、重载对话框的OnInitDialog虚函数
 BOOL CSetDlg::OnInitDialog()
 {
  CDialog::OnInitDialog();

  // TODO:  在此添加额外的初始化
  m_btnST.SetActiveBgColor(RGB(0,0,255));//设置活动时(鼠标移到其上时)的背景色
  m_btnST.SetActiveFgColor(RGB(0,255,0));

  m_btnST.SetInactiveBgColor(RGB(255,255,0));//设置不活动时的背景色
  m_btnST.SetInactiveFgColor(RGB(255,0,255));
  return TRUE;  // return TRUE unless you set the focus to a control
 }
八、在窗口中贴图
1、创建位图
 CBitmap bitmap;
 bitmap.LoadBitmap(IDB_BITMAP1);
2、创建兼容DC
 CDC dcCompatible;
 dcCompatible;.CreateCompatibleDC(pDC);
3、将位图选到兼容DC
 dcCompatible.SelectObject(&bitmap);
4、将兼容DC中的位图贴到到当前DC中
 pDC->BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);

实例:在WM_ERASEBKGND中实现(也可在OnDraw函数中实现)
BOOL CGraphicView::OnEraseBkgnd(CDC* pDC)
{
 // TODO: Add your message handler code here and/or call default
 CBitmap bitmap;
 bitmap.LoadBitmap(IDB_BITMAP1);

 CDC dcCompatible;
 dcCompatible.CreateCompatibleDC(pDC);

 dcCompatible.SelectObject(&bitmap);

     CRect rect;
 GetClientRect(&rect);
 //pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);//不能拉伸位图

 BITMAP bmp;
 bitmap.GetBitmap(&bmp);
     pDC->StretchBlt(0,0,rect.Width(),rect.Height  (),&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
  //可以拉伸位图以适应窗口大小
 //return CView::OnEraseBkgnd(pDC);//不能执行,否则位图背景会被擦除,无法显示
 return TRUE;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值