Rect & RECT & CRect & RectF

本文介绍了不同类型的矩形对象创建方法,包括整型和浮点数类型,涉及多种构造函数,如默认构造函数、使用点和尺寸对象构造等。

1) Rect
Rect::Rect()
Creates a Rect object whose x-coordinate, y-coordinate, width, and height are all zero. This is the default constructor. 
Rect::Rect(Point&,Size&)
Creates a Rect object by using a Point object to initialize the X and Y data members and a Size object to initialize the Width and Height data members.
Rect::Rect(INT,INT,INT,INT)
Creates a Rect object by using four integers to initialize the X, Y, Width, and Height data members.


2) RectF
RectF::RectF(PointF&,SizeF&) Creates a RectF object by using a PointF object to initialize the X and Y data members and uses a SizeF object to initialize the Width and Height data members of this rectangle.
RectF::RectF() Creates a RectF object and initializes the X and Y data members to zero. This is the default constructor.
RectF::RectF(REAL,REAL,REAL,REAL) Creates a RectF object by using four integers to initialize the X, Y, Width, and Height data members.

 

3) RECT
typedef struct tagRECT
{
    LONG    left;
    LONG    top;
    LONG    right;
    LONG    bottom;
} RECT, *PRECT, NEAR *NPRECT, FAR *LPRECT;

 

4) CRect

 

CRect( ) throw( ); 
CRect(int l,int t, int r, int b) throw( );
CRect(const RECT& srcRect) throw( );
CRect(LPCRECT lpSrcRect) throw( );
CRect(POINT point,SIZE size) throw( );
CRect(POINT topLeft,POINT bottomRight) throw( );

// MyButton.cpp; #include "pch.h" #include "MyButton.h" MyButton::MyButton() : m_bgColor(RGB(200, 200, 200)) {} MyButton::~MyButton() {} void MyButton::SetBackgroundColor(COLORREF color) { m_bgColor = color; Invalidate(); } void MyButton::AddRoundRect(GraphicsPath* path, const Rect& rect, int cornerRadius) { int diameter = cornerRadius * 2; path->AddArc(rect.X, rect.Y, diameter, diameter, 180, 90); path->AddArc(rect.X + rect.Width - diameter, rect.Y, diameter, diameter, 270, 90); path->AddArc(rect.X + rect.Width - diameter, rect.Y + rect.Height - diameter, diameter, diameter, 0, 90); path->AddArc(rect.X, rect.Y + rect.Height - diameter, diameter, diameter, 90, 90); path->CloseFigure(); } BEGIN_MESSAGE_MAP(MyButton, CButton) ON_WM_PAINT() END_MESSAGE_MAP() void MyButton::OnPaint() { CPaintDC dc(this); CRect rect; GetClientRect(&rect); // 双缓冲初始化 Bitmap buffer(rect.Width(), rect.Height()); Graphics graphics(&buffer); graphics.SetSmoothingMode(SmoothingModeAntiAlias); // 绘制背景(圆角矩形) Color bgColor; bgColor.SetFromCOLORREF(m_bgColor); SolidBrush brush(bgColor); GraphicsPath path; AddRoundRect(&path, Rect(0, 0, rect.Width(), rect.Height()), 5); graphics.FillPath(&brush, &path); // 处理文本 CStringW strText; GetWindowText(strText); if (!strText.IsEmpty()) { const wchar_t* text = strText.GetString(); Gdiplus::Font font(L"Arial", 12); Gdiplus::SolidBrush textBrush(Color::White); // 关键修正:正确配置 StringFormat Gdiplus::StringFormat format; format.SetAlignment(Gdiplus::StringAlignmentCenter); // 水平居中 format.SetLineAlignment(Gdiplus::StringAlignmentCenter); // 垂直居中 format.SetFormatFlags( Gdiplus::StringFormatFlagsNoWrap | Gdiplus::StringFormatFlagsLineLimit ); // 禁用自动换行(如需换行则移除 NoWrap) // 定义布局区域(必须与按钮同宽高) Gdiplus::RectF layoutRect(0, 0, rect.Width(), rect.Height()); // 测量文本尺寸(验证用,可选) Gdiplus::RectF boundingBox; graphics.MeasureString(text, -1, &font, layoutRect, &format, &boundingBox); // 修正绘制方式:直接传入 layoutRect,由 StringFormat 控制居中 graphics.DrawString( text, -1, &font, layoutRect, // 关键:传入整个布局区域 &format, &textBrush // 由 format 自动处理居中 ); } // 输出到屏幕 Graphics g(dc.GetSafeHdc()); g.DrawImage(&buffer, 0, 0); } 这段代码是不是有问题,执行后显示4个圆角不一致,是计算错误?还是生成后又修剪了?
07-09
“void MyButton::AddRoundRect(Gdiplus::GraphicsPath *,const Gdiplus::Rect &,int)”: 无法将参数 2 从“Gdiplus::RectF”转换为“const Gdiplus::Rect &” 声明与 "void MyButton::AddRoundRect(Gdiplus::GraphicsPath *path, const Gdiplus::Rect &rect, int cornerRadius)" (已声明 所在行数:19,所属文件:"C:\Users\Li\source\repos\MyButtonTest2\MyButton.h") 不兼容 不存在用户定义的从 "Gdiplus::RectF" 到 "const Gdiplus::Rect" 的适当转换 拼写错误 - Button 不是单词 浏览此宏周围的操作可能会失败。请考虑将其添加到提示文件。 拼写错误 - Round 不是单词 拼写错误 - path 不是单词 拼写错误 - corner 不是单词 拼写错误 - Radius 不是单词 拼写错误 - diameter 不是单词 拼写错误 - Paint 不是单词 拼写错误 - client 不是单词 拼写错误 - buffer 不是单词 拼写错误 - graphics 不是单词 拼写错误 - Color 不是单词 拼写错误 - brush 不是单词 拼写错误 - path 不是单词 拼写错误 - Text 不是单词 拼写错误 - font 不是单词 拼写错误 - text 不是单词 拼写错误 - Brush 不是单词 拼写错误 - format 不是单词 拼写错误 - layout 不是单词 “void MyButton::AddRoundRect(Gdiplus::GraphicsPath *,const Gdiplus::RectF &,Gdiplus::REAL)”:“MyButton”中没有找到重载的成员函数 “参数”: 从“Gdiplus::REAL”转换到“INT”,可能丢失数据 “参数”: 从“Gdiplus::REAL”转换到“INT”,可能丢失数据 未找到"AddRoundRect"的函数定义。 可以将成员函数设为常量。 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 拼写错误 - Dialog 不是单词 拼写错误 - gdiplus 不是单词 拼写错误 - Startup 不是单词 拼写错误 - Input 不是单词 拼写错误 - Paint 不是单词 拼写错误 - Icon 不是单词 拼写错误 - Icon 不是单词 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 拼写错误 - virtual 不是单词 浏览此宏周围的操作可能会失败。请考虑将其添加到提示文件。 拼写错误 - Data 不是单词 拼写错误 - Exchange 不是单词 拼写错误 - Icon 不是单词 拼写错误 - Dialog 不是单词 拼写错误 - Paint 不是单词 浏览此宏周围的操作可能会失败。请考虑将其添加到提示文件。 拼写错误 - Query 不是单词 拼写错误 - Drag 不是单词 拼写错误 - Icon 不是单词 浏览此宏周围的操作可能会失败。请考虑将其添加到提示文件。 拼写错误 - Color 不是单词 浏览此宏周围的操作可能会失败。请考虑将其添加到提示文件。 拼写错误 - Button 不是单词 拼写错误 - Button 不是单词 拼写错误 - Button 不是单词 拼写错误 - gdiplus 不是单词 拼写错误 - Token 不是单词 拼写错误 - gdiplus 不是单词 拼写错误 - Initialized 不是单词 拼写错误 - Microsoft 不是单词 拼写错误 - Visual 不是单词 拼写错误 - Button 不是单词 拼写错误 - Test 不是单词 宏可以转换为 constexpr 宏可以转换为 constexpr 宏可以转换为 constexpr 宏可以转换为 constexpr 宏可以转换为 constexpr 拼写错误 - Next 不是单词 拼写错误 - default 不是单词 拼写错误 - values 不是单词 拼写错误 - objects 不是单词
07-09
void MyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { if (!lpDrawItemStruct) return; CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); CRect rect = lpDrawItemStruct->rcItem; UINT state = lpDrawItemStruct->itemState; // 创建 GDI+ Graphics 对象 Graphics graphics(pDC->GetSafeHdc()); // ✅ 启用抗锯齿渲染 //graphics.SetSmoothingMode(SmoothingModeAntiAlias); graphics.SetSmoothingMode(SmoothingModeHighQuality); graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit); SolidBrush brush(m_colorBackground); // 绘制圆角矩形路径 GraphicsPath path; int diameter = m_radius * 2; RectF rectF(rect.left, rect.top, rect.Width(), rect.Height()); // 右下角圆弧 RectF arcRect = RectF(rectF.GetRight() - diameter, rectF.GetBottom() - diameter, diameter, diameter); path.AddArc(arcRect, 0, 90); // 左下角圆弧 arcRect = RectF(rectF.GetLeft(), rectF.GetBottom() - diameter, diameter, diameter); path.AddArc(arcRect, 90, 90); // 左上角圆弧 arcRect = RectF(rectF.GetLeft(), rectF.GetTop(), diameter, diameter); path.AddArc(arcRect, 180, 90); // 右上角圆弧 arcRect = RectF(rectF.GetRight() - diameter, rectF.GetTop(), diameter, diameter); path.AddArc(arcRect, 270, 90); path.CloseFigure(); //填充圆角矩形 graphics.FillPath(&brush, &path); // 获取按钮文本 CString text; GetWindowText(text); //// 获取当前字体 //CFont* pFont = GetFont(); //LOGFONT lf; //pFont->GetLogFont(&lf); // 使用 GDI+ 创建字体 FontFamily fontFamily(m_fontName); // 使用自定义字体名 Gdiplus::Font font(&fontFamily, m_fontSize, m_fontWeight, UnitPixel); SolidBrush textBrush(m_fontColor); // 白色字体 // 创建 GDI+ 字体对象 //Graphics graphics(pDC->GetSafeHdc()); //Gdiplus::Font font(pDC->GetSafeHdc(), (const LOGFONTW*)&lf); // 创建画刷 //SolidBrush textBrush(Color::Black); // 设置绘制区域和格式 RectF layoutRect(rect.left, rect.top, rect.Width(), rect.Height()); StringFormat format; format.SetAlignment(StringAlignmentCenter); format.SetLineAlignment(StringAlignmentCenter); // 绘制文字 // graphics.DrawString(text, -1, &font, layoutRect, &format, &textBrush); graphics.DrawString(text, text.GetLength(), &font, layoutRect, &format, &textBrush); } 为啥按钮文字没有加粗,是字体上价格横线
最新发布
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值