如何使用 Windows 窗体在窗口标题栏中绘制自定义按钮?
如何在窗体标题栏中的最小化、最大化和关闭按钮旁边绘制自定义按钮?
我知道您需要使用 Win32 API 调用并覆盖 WndProc 过程,但我一直无法找到正确的解决方案。
有谁知道如何做到这一点? 更具体地说,有谁知道在 Vista 中可行的方法吗?
How do you draw a custom button next to the minimize, maximize and close buttons within the Titlebar of the Form?
I know you need to use Win32 API calls and override the WndProc procedure, but I haven't been able to figure out a solution that works right.
Does anyone know how to do this? More specifically, does anyone know a way to do this that works in Vista?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下内容将在 XP 中运行,我没有方便的 Vista 机器来测试它,但我认为你的问题是由于某种不正确的 hWnd 引起的。 无论如何,继续处理注释很差的代码。
The following will work in XP, I have no Vista machine handy to test it, but I think your issues are steming from an incorrect hWnd somehow. Anyway, on with the poorly commented code.
我知道距离上次回答已经很久了,但这最近确实对我有帮助,我喜欢用我的评论和修改来更新克里斯提供的代码。
该版本在 Win XP 和 Win 2003 上完美运行。在 Win 2008 上,调整窗口大小时有一个我无法识别的小错误。 也适用于 Vista(无 Aero),但请注意,标题栏按钮不是方形的,按钮尺寸应考虑到这一点。
该代码演示了需要处理的消息以及如何处理它们。
该代码使用自定义 TitleBarButton 对象的集合。 该类太大,无法包含在此处,但如果需要,我可以提供它以及示例。
I know it's been long since the last answer but this really helped me recently and I like to update the code provided by Chris with my comments and modifications.
The version runs perfectly on Win XP and Win 2003. On Win 2008 ot has a small bug that I was not able to identify, when resizing windows. Works on Vista too (no-Aero) but note that the title bar buttons are not square and button dimensions should take that into account.
The code demonstrates the messages that heve to be treated and how to treat them.
The code uses a collection of custom TitleBarButton objets. That class is too big to be included here but I can provide it if needed along with an example.
绘图似乎是简单的部分,以下内容将做到这一点:
[编辑:代码已删除,请参阅我的其他答案]
真正的问题是更改状态并检测按钮上的点击...为此,您需要挂钩作为程序的全局消息处理程序,.NET 似乎隐藏了不在实际容器区域中的表单的鼠标事件(即鼠标移动并单击标题栏)。 我正在寻找相关信息,现在找到了,我正在研究它,应该不会太难......如果我们能弄清楚这些消息实际上传递的是什么。
Drawing seems to be the easy part, the following will do that:
[Edit: Code removed, see my other answer]
The real problem is changing the state and detecting clicks on the button... for that you'll need to hook into the global message handler for the program, .NET seems to hide the mouse events for a form while not in the actual container areas (ie. mouse moves and clicks on the title bar). I'm looking for info on that, found it now, I'm working on it, shouldn't be too hard... If we can figure out what these messages are actually passing.