MFC第二十三天 HBrush对闭合图形的填充、CPen、CFont类常用功能与LOGFONT和LOGPEN结构体

文章目录

HBrush对闭合图形的填充

HBRUSH创建:

a)实色填充:

cpp 复制代码
HBRUSH CreateSolidBrush( COLORREF color);

b)栅格线填充:

cpp 复制代码
HBRUSH CreateHatchBrush( int iHatch, COLORREF color);

c)平铺图填充:

cpp 复制代码
HBRUSH CreatePatternBrush( HBITMAP hbm);
HBITMAP hBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_LOGO));

d)空填充:可以创建前三种任何一种,也可以创建第四种(空中)

cpp 复制代码
HBRUSH CreateBrushIndirect(  [in] const LOGBRUSH *plbrush);

对应的CBrush

cpp 复制代码
CBrush::CreateBrushIndirect
 Initializes a brush with the style, color, and pattern specified in a LOGBRUSH structure.
 
CBrush::CreateDIBPatternBrush
 Initializes a brush with a pattern specified by a device-independent bitmap (DIB).
 
CBrush::CreateHatchBrush //Initializes a brush with the specified hatched pattern and color.
CBrush::CreatePatternBrush	//Initializes a brush with a pattern specified by a bitmap 图片
CBrush::CreateSolidBrush	// Initializes a brush with the specified solid color. 实色
CBrush::CreateSysColorBrush		// Creates a brush that is the default system color.
 
CBrush::FromHandle
 Returns a pointer to a CBrush object when given a handle to a Windows HBRUSH object.
 
CBrush::GetLogBrush		// Gets a LOGBRUSH structure.
cpp 复制代码
 /* Hatch Styles */  填充风格
#define HS_HORIZONTAL       0       /* ----- */
#define HS_VERTICAL         1       /* ||||| */
#define HS_FDIAGONAL        2       /* \\\\\ */
#define HS_BDIAGONAL        3       /* / */
#define HS_CROSS            4       /* +++++ */
#define HS_DIAGCROSS        5       /* xxxxx */
#define HS_API_MAX          12
cpp 复制代码
typedef struct tagLOGBRUSH {
  UINT      lbStyle;
  COLORREF  lbColor;
  ULONG_PTR lbHatch;
} LOGBRUSH, *PLOGBRUSH, *NPLOGBRUSH, *LPLOGBRUSH;
cpp 复制代码
	CBrush br1;
	br1.CreateSolidBrush(0xffff00); //纯色填充
	dc.SelectObject(&br1);
	dc.SelectObject(&pOldPen); //默认pen 接近0的黑色
	dc.Pie(296, 80, 800, 400, 600, 134, 600, 400);

	LOGBRUSH lb{ BS_NULL };
	CBrush br3;
	br3.CreateBrushIndirect(&lb);
	dc.SelectObject(&br3);
	POINT pts[] = { {40,200},{130,30},{300,250},{150,300} };
	dc.Polygon(pts, _countof(pts));

HBITMAP位图资源的加载和平铺填充

cpp 复制代码
	CBitmap bitmap;
	bitmap.LoadBitmap(IDB_LOGO);
	CBrush br;
	br.CreatePatternBrush(&bitmap);   
	dc.SelectObject(&br);
	dc.Rectangle(rect.left, rect.top, rect.right, rect.bottom);

CFont类常用功能与LOGFONT结构体

CFont类的主要函数:

a)CFont::CreateFont:参数太多没法用

b)使用结构体创建比较方便:

cpp 复制代码
BOOL CreateFontIndirect( const LOGFONT* lpLogFont );

c)GetLogFont:获取字体信息,是以上函数的反函数。

d)简易创建函数:他的大小是以上函数的十分之一,所以调用时要乘以10。

cpp 复制代码
CreateFont和CreateFontIndirect中间加Point。

Requested font height in tenths of a point. 
	(For instance, pass 120 to request a 12-point font.)
cpp 复制代码
typedef struct tagLOGFONT { 
  LONG lfHeight;   LONG lfWidth; 
  LONG lfEscapement;  //书写角度
  LONG lfOrientation; 	//基线角度
  LONG lfWeight; 
  BYTE lfItalic; 
  BYTE lfUnderline; 
  BYTE lfStrikeOut;  //删除线
  BYTE lfCharSet;  //字符集表示 
  BYTE lfOutPrecision; //输出精度
  BYTE lfClipPrecision; //剪辑精度
  BYTE lfQuality; //字体质量
  BYTE lfPitchAndFamily; //字体的字符间距和族标识
  TCHAR lfFaceName[LF_FACESIZE]; //字体名称
} LOGFONT;
cpp 复制代码
	CFont font;
	//font.CreatePointFont(100 * 3 / 2, _T("黑体"));  //简易函数
	LOGFONT lf{ 15 * 3 / 2 };
	lf.lfItalic = TRUE;
	lf.lfWeight = 700;
	lf.lfCharSet = GB2312_CHARSET;
	lf.lfEscapement = 200;
	_tcscpy_s(lf.lfFaceName, LF_FACESIZE, _T("黑体"));
	font.CreateFontIndirect(&lf);
	
	auto pOldFont = dc.SelectObject(&font);
	//dc.SetBkMode(TRANSPARENT); //设置文字的透明色 //真透明 	//dc.SetTextColor(0xff);
	dc.SetBkColor(0xffff00); //假透明
	dc.DrawText(_T("这世间本没有佛"), rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
	pOldFont->GetLogFont(&lf);

CPen类简介

cpp 复制代码
CPen::CreatePen
Creates a logical cosmetic or geometric pen with the specified style, width,
 and brush attributes, and attaches it to the CPen object.
 
CPen::CreatePenIndirect
 Creates a pen with the style, width, and color given in a LOGPEN structure,
  and attaches it to the CPen object.
 
CPen::FromHandle    //Returns a pointer to a CPen object when given a Windows HPEN.
CPen::GetExtLogPen	//Gets an EXTLOGPEN underlying structure.
CPen::GetLogPen		//Gets a LOGPEN underlying structure. 
CPen::operator HPEN		//Returns the Windows handle attached to the CPen object.
cpp 复制代码
/* Pen Styles */
#define PS_SOLID            0						实线
#define PS_DASH             1       /* -------  */  破折线	
#define PS_DOT              2       /* .......  */ 点
#define PS_DASHDOT          3       /* _._._._  */ 点划线
#define PS_DASHDOTDOT       4       /* _.._.._  */双点划线
#define PS_NULL             5					  无线条
cpp 复制代码
typedef struct tagLOGPEN {
  UINT  lopnStyle;   // 线条样式
  POINT lopnWidth;   // 线条宽度
  COLORREF lopnColor;   // 线条颜色
} LOGPEN, *PLOGPEN
cpp 复制代码
	CRect rect;
	GetClientRect(&rect);
	CPen pen;  //用来绘制边框的
	pen.CreatePen(PS_SOLID, 5, RGB(0, 255, 255));
	auto pOldPen = dc.SelectObject(&pen); //返回之前选入的画笔的句柄
	//绘图操作结束后,会使用SelectObject函数将之前的画笔 重新选入 DC,以保证 DC 的状态不受影响
	LOGPEN logPen;
	GetObject(pOldPen->GetSafeHandle(), sizeof(logPen), &logPen);
	
	CPen p2;
	p2.CreatePen(PS_DOT, 1, 0xff00);
	dc.SelectObject(&p2); 
	dc.MoveTo(594, 31);
	dc.LineTo((196 + 710) / 2, (72 + 301) / 2);
	dc.LineTo(517, 414);
相关推荐
人间乄惊鸿客11 小时前
c++ 右值
c++·算法
ShineWinsu13 小时前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
瑶池酒剑仙14 小时前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
郝学胜-神的一滴1 天前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光1 天前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
青少儿编程课堂2 天前
蓝桥杯真题解析:手链配色计数与波利亚定理(全国青少年信息素养大赛与电子学会软件编程考级)
c++·python·算法·bfs·信息学竞赛
艾莉丝努力练剑2 天前
【AI大模型接入SDK】项目总结
java·开发语言·前端·c++·人工智能·学习·架构
旖旎夜光2 天前
面试题 01.02: 判定是否互为字符重排 (哈希表) —— 题解
c++·笔记·学习·算法·哈希表
白小飞不会飞2 天前
【AUTOSAR】CP SOME/IP-TP
c++·嵌入式硬件·网络协议·车载系统·autosar
ShineWinsu2 天前
对于Qt:信号量(QSemaphore)的解析
c++·qt·面试·笔试·客户端·线程安全·信号量