您的位置:首页 > 其它

孙鑫VC视频教程笔记之第十课“绘图”

2008-09-15 15:47 375 查看
1. 画图:
CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(255,0,0));
dc.SelceObject(&pen);
dc.SetPixel(point,RGB(255,0,0)); //画点
dc.MoveTo();dc.LineTo(); //画线
dc.Rectangle(); //画矩形

2. 透明画刷:
CBrush *pBrush=CBrush::FromHandle(GetStockObject(NULL_BRUSH));
dc.SelctObject(pBrush);

3. 颜色对话框:
COLORREF m_selColor; //声明为类的成员变量
CColorDialog dlg;
dlg.m_cc[/b].rgbResult=m_selColor; //将上次选择的颜色重新设置回去
dlg.m_cc.Flags|=CC_RGBINIT; // CC_RGBINIT Causes the dialog box to use the color specified in the rgbResult member as the initial color selection. Or the dialog will use the black color as the initial color.
if(IDOK==dlg.DoModal())
{
m_selColor=dlg.GetColor(); //得到用户选择的颜色
}

4. 字体对话框:
CFontDialog dlg;
CFont m_font;
if(IDOK==dlg.DoModal())
{
if(!m_font.m_hObject) //字体是否和字体资源相关联了
{
m_font.CreateFontIndirect(dlg.m_cf[/b].lpLogFont); //用选定的字体初始化一种字体
}
else
{
m_font.DeleteObject();
}
m_strFontName=dlg.m_cf.lpLogFont->lfFaceName; //获取字体的名字
}

5. 设置对话框和控件的背景色:
在对话框中添加WM_CTLCOLOR消息,在消息响应函数OnCtlColor函数中添加:
OnCtlColor的函数原型为:OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor),
该函数在MSDN中的解释是这样的:The framework calls this member function when a child control is about to be drawn. Most controls send this message to their parent (usually a dialog box) to prepare the pDC for drawing the control using the correct colors.

设置对话框和上面所有控件的背景色:

但两个button按钮的颜色没有改变
HBRUSH m_brush;
m_brush.CreateSolidBrush(RGB(255,0,0));
return m_brush;

静态框,组框(Group Box)的颜色设置:

if(pWnd->GetDlgCtrID()==IDC_STATIC1)
{
pDC->SetTextColor(RGB(255,0,0)); //设置字体颜色
pDC->SetBkMode(TRANSPARENT);//设置背景色为透明的
}

单行文本框的设置:

if(pWnd->GetDlgCtrID()==IDC_EDIT1)
{
pDC->SetTextColor(RGB(255,0,0)); //设置字体颜色
pDC->SetBkColor(RGB(255,0,0));//设置文本框的背景色
}

为静态框设置字体(以下代码):

CFont m_font; //放在CDialog的构造函数中
m_font.CreatePointFont(200,"华文行楷");
if(pWnd->GetDlgCtrID()==IDC_STATIC1) //还是放在OnCtlColor函数中
pDC->SelectObject(&m_font);

改变窗口上Button的字体颜色:

Button是自绘制的,所以需要自己创建一个CButton类,然后在该类中覆盖DrawItem方法,在该方法中设置对Button的字体颜色的改变,然后对对话框上的按钮设置变量,类型选为新建的类。
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);
}
最后将Button的styles去掉Default选项,选中owner draw。
更多Button的功能设置,参看源码中的CButtonST这个类(这个是国外的一个作者写的)。

6. 在窗口上显示一个位图:
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);

在CView类中添加WM_ERASEBKGND消息,在该消息所对应的消息响应函数OnEraseBkgnd中添加代码:
BOOL CGraphicView::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);

CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);

dcCompatible.SelectObject(&bitmap);

CRect rect;
GetClientRect(&rect);
//方法1:位图原始大小显示,一比一的显示
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);
//方法2:拉伸或压缩位图使得位图能够随窗口的大小改变而改变
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible, 0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
return TRUE;
//return CView::OnEraseBkgnd(pDC); //该行必须注释掉,否则被调用的话,又会擦除掉了
}
上述方法OnEraseBkgnd中的代码也可以放在OnDraw函数中,但有一个问题,就是在改变窗口大小的时候,窗口上的位图会闪烁一下,这是因为OnDraw先将背景擦除后在粘贴上一张位图,而OnEraseBkgnd是世界将位图粘在上面,没有擦除这样的动作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: