如何在对话框窗口界面上画图和写字:
实例如下:
首先创建一个基本的MFC对话框工程,其次打开类视图,点击主窗口的OnPaint()函数进行编辑如下:
void CCRgnDlg::OnPaint()
{
if (IsIconic()) //如果窗口最小化
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else //在窗口中绘图和写字。
{
CPaintDC dc(this);
CRgn rgn1,rgn2,rgn3;
CBrush br;
br.CreateSolidBrush(RGB(255,0,0));
rgn1.CreateRectRgn(50,50,150,150);
rgn2.CreateEllipticRgn(50,50,250,250);
rgn2.CombineRgn(&rgn2,&rgn1,RGN_XOR);
dc.FillRgn(&rgn2,&br);
//========================
//=====创建字体并写字=====
CFont font,*oldfont;
font.CreateFont(12,10,10,10,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"");
oldfont=dc.SelectObject(&font); //往后对font的改变就是对oldfont的改变。
dc.TextOut(10,10,"CreateFont创建的字体");
m_button1.SetFont(&font); //设置按钮字体
dc.SelectObject(oldfont);
font.DeleteObject();
font.CreatePointFont(90,"宋体");
dc.SelectObject(&font);
dc.TextOut(35,35,"CreatePointFont创建的字体");
dc.SelectObject(oldfont);
font.DeleteObject();
LOGFONT log;
log.lfCharSet=134;
log.lfHeight=-25; //高25像素
log.lfWidth=-25; //宽25像素
log.lfWeight=250; //字体颜色的深度
log.lfEscapement=-20; //倾斜-20度。
strcpy(log.lfFaceName,"宋体");
font.CreateFontIndirect(&log);
dc.SelectObject(&font);
dc.TextOut(50,50,"LOGFONT创建的倾斜加粗字体");
dc.SelectObject(oldfont);
font.DeleteObject();
CDialog::OnPaint();
}
}