先看程序:
void CMainWindow::OnPaint ()
{
//在OnPaint函数中,只能使用CPaintDC
CPaintDC dc (this);
CRect rect;
GetClientRect (&rect);
dc.MoveTo(rect.left,rect.top);
dc.LineTo(rect.right,rect.bottom);
dc.MoveTo(rect.left,rect.bottom);
dc.LineTo(rect.right,rect.top);
dc.SetBkColor(RGB(192,192,192));//文字背景色
CString str = "Hello, MFC";
dc.DrawText (str, -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
{
//调试时非常方便
// TRACE("当前坐标:x = %d, y = %d\n",point.x,point.y);
CRect rect;
GetClientRect(&rect);
//画图需要拿到设备描述表
CClientDC dc(this);
//可以在构造函数中设定笔的类型
CPen pen/*(PS_SOLID,100,RGB(255,0,0))*/;
//也可以调用CreatePen设定
pen.CreatePen(PS_SOLID,10,RGB(255,0,0));
CPen* pOldPen= dc.SelectObject(&pen);
//透明画刷
dc.SelectStockObject(NULL_BRUSH);
//设置映射模式
//不考虑横纵坐标比
dc.SetMapMode(MM_ANISOTROPIC);
//逻辑坐标最大就