vc++中HBRUSH的几种用法

本文详细介绍在Windows编程中创建不同类型的画刷(hbr)和字体(CFont)的方法。包括使用CreateSolidBrush、CreatePatternBrush等函数创建画刷,以及通过CFont类的CreateFont、CreateFontIndirect等功能创建字体,提供了丰富的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HBRUSH hbr;

第一种: hbr= CreateSolidBrush(RGB(255,0,0));                       //创建单色的画刷

第二种: hbr= (HBRUSH)GetStockObject(BLACK_BRUSH);   //只能取特定颜色的画刷,如BLACK_BRUSH,GRAY_BRUSH刷

第三种: hbr= CreatePatternBrush(HBITMAP hbmp);               //得到位图画刷

第四种: hbr = CreateHatchBrush(int fnStyle,                             //创建一种带阴影的画刷
                              COLORREF clrref
                               )

第五种: hbr= CreateBrushIndirect(LOGBRUSH);                    //通过LOGBRUSH结构体来取画刷
  

 typedef struct tagLOGBRUSH { 
    UINT     lbStyle; //画刷类型
    COLORREF lbColor; //颜色
    LONG     lbHatch; //阴影
 } LOGBRUSH, *PLOGBRUSH;

第六种: hbr= HBRUSH CreateDIBPatternBrush(                   //通过与设备无关位图创建一个画刷
     HGLOBAL hglbDIBPacked,  // handle to DIB
     UINT fuColorSpec        // color table data
   );

 

附加:

RECT rc;

    GetWindowRect(&rc);
    ScreenToClient(&rc);

 HDC hdc;

primarysurface->GetDC(&hdc);//得到

FillRect(hdc,&rc,hbr);//填充一个指定的rc矩形区域

 

 

 

CFont用法 CFont 使用详解 .

 (2012-04-20 16:01:15)
sg_trans.gif转载
标签: 

it

 

原文:http://blog.youkuaiyun.com/noexcuse/article/details/1837888

 

CFont class encapsulates the functionalities needed to manipulate the Fonts in Windows programming. A font can be created in 4 ways with a CFont class using CreateFont, CreateFontIndirect, CreatePointFont, or CreatePointFontIndirect functions. This CFont Samples page tries to give a piece of sample code for all of the above functions.

CFont Sample for using CFont :: CreateFont:
   The following CFont sample illustrates how to create a font using CreateFont function of the CFont class.
 


   CClientDC dc(this);
   CFont l_font;
   l_font.CreateFont(14, 0, 0, 0, FW_NORMAL,
   FALSE, FALSE, FALSE, 0, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,   DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "Times New Roman");

   CFont* l_old_font = dc.SelectObject(&l_font); dc.TextOut(50, 50, "Hello World");    
   dc.SelectObject(l_old_font);
   // Delete the font object. 
   l_font.DeleteObject(); 

 

 

 


   In the above CFont Sample, the CreateFont function uses all default parameters (either 0 or Default constants), except for the height parameter. If CreateFont is called as above, the MFC framework will select the best fit parameters by itself and create a font accordingly. 

CFont Sample for using CFont :: CreateFontIndirect: 
   This part of CFont sample illustrates the usage of CreateFontIndirect. 


   CClientDC dc(this);
   CFont l_font;
   LOGFONT lf;
   lf.lfHeight = 12;
   strcpy(lf.lfFaceName, "Arial"); // Need a face name "Arial". 
   l_font.CreateFontIndirect(&lf); 
   CFont* l_old_font = dc.SelectObject(&l_font); 
   dc.TextOut(50, 50, "Hello World"); 
   dc.SelectObject(l_old_font); 
   // Delete the font object.
   l_font.DeleteObject(); 


The LOGFONT is a structure with all members required for the Font object. 

CFont Sample for using CFont :: CreatePointFont:
 This part of the sample illustrates the use of CreatePointFont for creating a font.


   CClientDC dc(this); 
   CFont l_font; 
   l_font.CreatePointFont(140,"Times New Roman"); 
   CFont* l_old_font = dc.SelectObject(&l_font);
   dc.TextOut(50, 50, "Hello World"); 
   dc.SelectObject(l_old_font); 
   // Delete the font object. 
   l_font.DeleteObject(); 


The first parameter of the CreatePointFont function is the height of the Font in tenths of a point. The return value of this function is non-zero value if successful. 

CFont Sample for using CFont :: CreatePointFontIndirect: 


   CClientDC dc(this); 
   CFont l_font; 
   LOGFONT lf;
   lf.lfHeight = 120; 
   strcpy(lf.lfFaceName, "Arial"); // Need a face name "Arial". 
   l_font.CreatePointFontIndirect(&lf); 
   CFont* l_old_font = dc.SelectObject(&l_font); 
   dc.TextOut(50, 60, "Hello World"); 
   dc.SelectObject(l_old_font); 
   //  Delete the font object. 
   l_font.DeleteObject(); 


   Usually it is better to use either CreatePointFont or CreatePointFontIndirect functions as they reduce the head-ache of thinking about the width, weight and such parameters.
   Also, in the above CFont sample the l_font is deleted in the end. It is a good programming practice as advised by Windows Programming manuals to delete the Objects after removing them from the current device context. 

分享:

转载于:https://my.oschina.net/u/3874841/blog/3072715

VC PICTURE控件的使用,如何加载背景图片2009年04月19日 星期日 15:02vc picture控件的分类总结: (一) 非动态显示图片(即图片先通过资源管理器载入,有一个固定ID) (二) 动态载入图片(即只需要在程序中指定图片的路径即可载入) 为方便说明,我们已经建好一个基于对话框的工程,名为Ttest. 对话框类为CTestDlg (一) vc picture控件非动态载入图片. 方法1.先从最简单的开始,用picture 控件来实现. 步骤: 先在资源里Import一张图片,ID为IDB_BITMAP2 然后在对话框上添加一个picture控件,右键点击打开属性, 将type下拉框选择BITMAP,紧跟着下面就出现一个Image下拉框, 拉开就会看到所有已经载入好的图片, 选择你要的图片.运行程序即可看到. 方法2vc picture控件.通过背景图 同样如上,先载入一张图片,ID为IDB_BITMAP2 TestDlg.h中 CBrush m_brBk;//在public中定义 TestDlg.cpp中 在初始化函数OnInitDialog()中加入: BOOL CTestDlg::OnInitDialog() { CDialog::OnInitDialog(); CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP2); m_brBk.CreatePatternBrush(&bmp); bmp.DeleteObject(); return TRUE; // return TRUE unless you set the focus to a control } 在打开类向导,找到WM_CTLCOLOR消息,重载得对应函数OnCtlColor(),添加如下: HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if (pWnd == this) { return m_brBk; } return hbr; } (二) vc picture控件动态载入图片. 方法3 图像控件(本例用KoDak 图像编辑控件) 1. 首先应该保证系统中有这个控件。注意,它不能单独使用,必须和其他几个控件(特别是Imgcmn.dll)一同使用。如果没有,从别的机器上copy过来即可。这几个文件是Imgadmin.ocx,Imgcmn.dll,Imgedit.ocx,Imgscan.ocx,Imgshl.dll,Imgthumb.ocx,Imgutil.dll,把它们copy到windows\system目录下,然后用regsvr32.exe将它们分别注册。 2. 打开工程,进入资源管理器,在对话框上单击右键,单击Insert Activex control… 选择Kodak图象编辑控件,大小任意。 3. 在对话框上选中该控件,为其添加变量:m_ctrlPicture。。 4. 在BOOL CTestDlg::OnInitDialog()添加如下: BOOL CTestDlg::OnInitDialog() { CDialog::OnInitDialog(); m_ctrlPicture.SetImage("aa.jpg"); //保证图像在工程目录下,也可以写绝对路径 m_ctrlPicture.Display(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } 编译运行就OK了,此种方法的好处就是可能针对多种图像格式. 方法4 vc picture控件通过CBitmap,HBITMAP,直接用OnPaint()绘制 首先在CTestDlg类中声明一个变量: CBitmap m_bmp; 然后我们在对话框中加入一个picture 标签,名为IDC_STATIC1 然后: BOOL CDisplayPic::OnInitDialog() { CDialog::OnInitDialog(); if( m_bmp.m_hObject != NULL )//判断 m_bmp.DeleteObject(); /////////载入图片 HBITMAP hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), "c:\\aaa.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE); if( hbmp == NULL ) return FALSE; ///////////////////////该断程序用来取得加载的BMP的信息//////////////////////// m_bmp.Attach( hbmp ); DIBSECTION ds; BITMAPINFOHEADER &bminfo = ds.dsBmih; m_bmp.GetObject( sizeof(ds), &ds ); int cx=bminfo.biWidth; //得到图像宽度 int cy=bminfo.biHeight; //得到图像高度 /////////////////// //////////////////////////////// /////////////得到了图像的宽度和高度后,我们就可以对图像大小进行适应,即调整控件的大小,让它正好显示一张图片/////////////////////////// CRect rect; GetDlgItem(IDC_STATIC1)->GetWindowRect(&rect); ScreenToClient(&rect); GetDlgItem(IDC_STATIC1)->MoveWindow(rect.left,rect.top,cx,cy,true);//调整大小 return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } 图片加载成功了,标签大小也适应了,下面就是绘制绘制图像了,打开类向导,重载WM_PAINT消息 void CDisplayPic::OnPaint() { //////////////以下三种情况任选一种会是不同效果(只能一种存在)/////////// //CPaintDC dc(this); //若用此句,得到的是对话框的DC,图片将被绘制在对话框上. CPaintDC dc(GetDlgItem(IDC_STATIC1)); //用此句,得到picture控件的DC,图像将被绘制在控件上 // CDC dc; // dc.m_hDC=::GetDC(NULL); //若用此两句,得到的是屏幕的DC,图片将被绘制在屏幕上/////////////////////////////////////////////////////// CRect rcclient; GetDlgItem(IDC_STATIC1)->GetClientRect(&rcclient); CDC memdc; memdc.CreateCompatibleDC(&dc); CBitmap bitmap; bitmap.CreateCompatibleBitmap(&dc, rcclient.Width(), rcclient.Height()); memdc.SelectObject( &bitmap ); CWnd::DefWindowProc(WM_PAINT, (WPARAM)memdc.m_hDC , 0); CDC maskdc; maskdc.CreateCompatibleDC(&dc); CBitmap maskbitmap; maskbitmap.CreateBitmap(rcclient.Width(), rcclient.Height(), 1, 1, NULL); maskdc.SelectObject( &maskbitmap ); maskdc.BitBlt( 0, 0, rcclient.Width(), rcclient.Height(), &memdc, rcclient.left, rcclient.top, SRCCOPY); CBrush brush; brush.CreatePatternBrush(&m_bmp); dc.FillRect(rcclient, &brush); dc.BitBlt(rcclient.left, rcclient.top, rcclient.Width(), rcclient.Height(), &memdc, rcclient.left, rcclient.top,SRCPAINT); brush.DeleteObject(); // Do not call CDialog::OnPaint() for painting messages }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值