winapi – win32 – 如何在文本字符串周围绘制一个矩形?
发布时间:2020-12-14 02:09:54 所属栏目:Windows 来源:网络整理
导读:我是Win32的新手并尝试在C中获取基于GDI的代码(出于技术原因不想使用GDI) 编辑:简化问题: 我需要在窗口中间绘制的文本周围绘制一个矩形. – 如何填充矩形坐标? – 任何人都可以帮助线 – 矩形(x1,y1,x2,y2)? – 如何计算这些(x1,y1) (x2,y2)值? 谢谢..
我是Win32的新手并尝试在C中获取基于GDI的代码(出于技术原因不想使用GDI)
编辑:简化问题: 我需要在窗口中间绘制的文本周围绘制一个矩形. 谢谢.. hdc = BeginPaint(hWnd,&ps); GetClientRect(hWnd,&rcClient); SelectObject(hdc,GetStockObject(DEFAULT_GUI_FONT)); SetTextColor(hdc,RGB(255,0)); DrawText(hdc,wstring(s.begin(),s.end()).c_str(),-1,&rectResult,DT_SINGLELINE | DT_CALCRECT); DrawText(hdc,&rcClient,DT_SINGLELINE | DT_CENTER | DT_VCENTER); // Here I need help - How to I place the rectangle around the Text - which is drawn in the middle of the window? // It looks like need to use - rectResult.bottom/top/left/right - but don't know how.. Rectangle(hdc,100,100); 解决方法
您实际上不必自己将文本居中.如果传递适当的标志,GDI文本输出函数可以为您完成.
例如,如果调用 假设您只有一行文本(听起来像这样),您可以通过传递DT_SINGLELINE和DT_VCENTER标志来自动垂直居中文本. 所以你真正需要做的就是编写代码将your window’s client area分成4个相等的部分,然后将这些矩形传递给DrawText函数.这不是非常困难.如果你无法在头脑中看到它,请将铅笔和纸放在上面. void PaintWindow(HWND hWnd) { // Set up the device context for drawing. PAINTSTRUCT ps; HDC hDC = BeginPaint(hWnd,&ps); HPEN hpenOld = static_cast<HPEN>(SelectObject(hDC,GetStockObject(DC_PEN))); HBRUSH hbrushOld = static_cast<HBRUSH>(SelectObject(hDC,GetStockObject(NULL_BRUSH))); // Calculate the dimensions of the 4 equal rectangles. RECT rcWindow; GetClientRect(hWnd,&rcWindow); RECT rc1,rc2,rc3,rc4; rc1 = rc2 = rc3 = rc4 = rcWindow; rc1.right -= (rcWindow.right - rcWindow.left) / 2; rc1.bottom -= (rcWindow.bottom - rcWindow.top) / 2; rc2.left = rc1.right; rc2.bottom = rc1.bottom; rc3.top = rc1.bottom; rc3.right = rc1.right; rc4.top = rc1.bottom; rc4.left = rc1.right; // Optionally,deflate each of the rectangles by an arbitrary amount so that // they don't butt up right next to each other and we can distinguish them. InflateRect(&rc1,-5,-5); InflateRect(&rc2,-5); InflateRect(&rc3,-5); InflateRect(&rc4,-5); // Draw (differently-colored) borders around these rectangles. SetDCPenColor(hDC,0)); // red Rectangle(hDC,rc1.left,rc1.top,rc1.right,rc1.bottom); SetDCPenColor(hDC,RGB(0,255,0)); // green Rectangle(hDC,rc2.left,rc2.top,rc2.right,rc2.bottom); SetDCPenColor(hDC,255)); // blue Rectangle(hDC,rc3.left,rc3.top,rc3.right,rc3.bottom); SetDCPenColor(hDC,128,0)); // orange Rectangle(hDC,rc4.left,rc4.top,rc4.right,rc4.bottom); // Draw the text into the center of each of the rectangles. SetBkMode(hDC,TRANSPARENT); SetBkColor(hDC,0)); // black // TODO: Optionally,set a nicer font than the default. DrawText(hDC,TEXT("Hello World!"),&rc1,DT_CENTER | DT_SINGLELINE | DT_VCENTER); DrawText(hDC,&rc2,&rc3,&rc4,DT_CENTER | DT_SINGLELINE | DT_VCENTER); // Clean up after ourselves. SelectObject(hDC,hpenOld); SelectObject(hDC,hbrushOld); EndPaint(hWnd,&ps); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ms-access – 当更新Microsoft Access数据库中的特定表和字
- Windows批处理文件 – 如果窗口标题包含文本,则为taskkill
- windows – 添加到通知托盘的图标在鼠标悬停时消失
- windows – 在我的应用程序激活之前检测哪些窗口处于活动状
- windows-server-2012-r2 – CSV上Hyper-V存储的可悲性能
- 如何排查Windows操作系统注销关机重启超慢故障
- windows – 广泛使用的负载平衡解决方案?
- NSIS:无法从Windows 7 64位的注册表中获取JRE版本
- Windows SVN浏览器没有shell集成?
- 为什么RMI注册表忽略了java.rmi.server.codebase属性
推荐文章
站长推荐
- Windows server 2016 搭建IIS(web)服务
- 使用System.Windows.Forms破坏Mono C#代码
- Windows Powershell是否具有Try/Catch或其他错误
- windows – 如何使用Write-EventLog在事件日志中
- 在Microsoft Sql Server Management Studio中测试
- Windows Azure:删除附加到不存在的VM的磁盘
- windows-phone-7 – 在锁定屏幕下跟踪加速度计
- emacs – 学习Windows / C程序员的常见Lisp技巧
- windows – 使用复制粘贴移动hg repo文件夹?
- 使用Windows Azure进行GeoIP路由
热点阅读