设置点、线、矩形、椭圆
设置颜色、字体
设置背景色、字体颜色与样式
代码:
//构造函数中初始化变量
CGraphicView::CGraphicView()
: m_nDrawType(0)
, m_ptOrigin(0)
, m_nLineWidth(0)
, m_nLineStyle(-1)
,m_clr(RGB(255,0,0))
, m_strFontName(_T(""))
{
// TODO: 在此处添加构造代码
}
// CGraphicView 绘制
void CGraphicView::OnDraw(CDC* pDC)
{
CGraphicDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CFont *pOldFont=pDC->SelectObject(&m_font);
pDC->TextOutW(0,0,m_strFontName);
pDC->SelectObject(pOldFont);
// TODO: 在此处为本机数据添加绘制代码
/*CBitmap bitmap;
bitmap.LoadBitmapW(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);*/
}
//画点
void CGraphicView::OnPoint()
{
// TODO: 在此添加命令处理程序代码
m_nDrawType=1;
}
//画线
void CGraphicView::OnLine()
{
// TODO: 在此添加命令处理程序代码
m_nDrawType=2;
}
//画矩形
void CGraphicView::OnRectangle()
{
// TODO: 在此添加命令处理程序代码
m_nDrawType=3;
}
//画椭圆
void CGraphicView::OnEllipse()
{
// TODO: 在此添加命令处理程序代码
m_nDrawType=4;
}
//鼠标左键按下时保存起始点
void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_ptOrigin=point;
CView::OnLButtonDown(nFlags, point);
}
//鼠标左键按起时画点、线、矩形、椭圆
void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
CPen pen(m_nLineStyle,m_nLineWidth,m_clr);
dc.SelectObject(&pen);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(pBrush);
switch(m_nDrawType)
{
case 1:
dc.SetPixel(point,m_clr);
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;
}
CView::OnLButtonUp(nFlags, point);
}
//点击设置菜单
void CGraphicView::OnSetting()
{
// TODO: 在此添加命令处理程序代码
CSetting dlg;
dlg.m_nLineWidth=m_nLineWidth;
dlg.m_nLineStyle=m_nLineStyle;
dlg.m_clr=m_clr;
if(IDOK==dlg.DoModal())
{
m_nLineWidth=dlg.m_nLineWidth;
m_nLineStyle=dlg.m_nLineStyle;
}
}
//设置颜色窗口
void CGraphicView::OnColor()
{
// TODO: 在此添加命令处理程序代码
CColorDialog dlg;
dlg.m_cc.Flags|=CC_RGBINIT |CC_FULLOPEN;
dlg.m_cc.rgbResult=m_clr;
if(IDOK==dlg.DoModal())
{
m_clr=dlg.m_cc.rgbResult;
}
}
//设置字体窗口
void CGraphicView::OnFont()
{
// TODO: 在此添加命令处理程序代码
CFontDialog dlg;
if(IDOK==dlg.DoModal())
{
if(m_font.m_hObject)
m_font.DeleteObject();
m_font.CreateFontIndirectW(dlg.m_cf.lpLogFont);
m_strFontName=dlg.m_cf.lpLogFont->lfFaceName;
Invalidate();
}
}
//设置背景
BOOL CGraphicView::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CBitmap bitmap;
bitmap.LoadBitmapW(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);
return TRUE;
//return CView::OnEraseBkgnd(pDC);
}
IMPLEMENT_DYNAMIC(CSetting, CDialogEx)
Setting类中代码:
//Setting窗口中构造函数中初始化一些变量
CSetting::CSetting(CWnd* pParent /*=NULL*/)
: CDialogEx(CSetting::IDD, pParent)
, m_nLineWidth(0)
, m_nLineStyle(0)
, m_clr(RGB(255,0,0))
{
m_brush.CreateSolidBrush(RGB(0,0,255));
m_font.CreatePointFont(200,_T("华文行楷"));
EnableAutomation();
}
void CSetting::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
UpdateData();
CPen pen(m_nLineStyle,m_nLineWidth,m_clr);
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.top+rect.Height()/2);
// 不为绘图消息调用 CDialogEx::OnPaint()
}
HBRUSH CSetting::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
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->SetBkMode(TRANSPARENT);
pDC->SetBkColor(RGB(0,0,255));
return m_brush;
}
if(pWnd->GetDlgCtrlID()==IDC_TEXT)
{
pDC->SelectObject(&m_font);
}
if(pWnd->GetDlgCtrlID()==IDOK)
{
pDC->SetTextColor(RGB(255,0,0));
return m_brush;
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
TestBtn类中代码:
void CTestBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
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);
}