更改按钮文本的边距/填充
我目前正在为我的“大”WinForms 项目编写一个小助手应用程序。在这个小应用程序中,我显示了一个带有几个按钮的用户控件。现在有那些标准的文本到按钮边距。
我的意思是,按钮看起来像这样:
~~~~~~~~~~~~~
~ ~
~ 按钮文本 ~
~ ~
~~~~~~~~~~~~~
但我希望 ButtonText 几乎“触及”按钮边界。换句话说:ButtonText 和 Buttonbounds 之间的标准空间对于我的需求来说太大了。是否可以改变这种行为?
I am currently writing a little helper-app for my "big" WinForms-Project. In this little app I am showing a UserControl with a few Buttons on it. Now there are those standard-text-to-button-margins.
What I mean is, that Buttons look like this:
~~~~~~~~~~~~~
~ ~
~ ButtonText ~
~ ~
~~~~~~~~~~~~~
But I want that the ButtonText nearly "touches" the buttonbounds. In other words: The standard space between ButtonText and Buttonbounds is too big for my needs. Is it possible to change this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以重写按钮上的绘制事件,并使用
ControlPaint.DrawButton(...)
以及常规字符串绘图来根据您的需要控制按钮布局。您必须挂钩 MoseDown、MouseUp 等才能将按钮绘制为正确的状态(按下、热等)。You can override the paint event on the button and use
ControlPaint.DrawButton(...)
along with regular string drawing to control the button layout to your needs. You will have to hook MoseDown, MouseUp, etc. to get the button drawn the correct state (depressed, hot, etc.).为此,您可以创建一个
UserControl< /code>
包含一个停靠按钮,那么您可以覆盖
UserControl
这样,当它自动调整大小时,它的大小会小于按钮通常需要的大小,从而迫使按钮文本更靠近边缘。但是,您需要进行一些尝试,以确保所有 DPI 设置。您还可以考虑使用
ToolStripButton
s 相反,使用适当的渲染器使它们看起来像常规按钮。这些通常尺寸较小,您应该对按钮内文本的填充和边距有更多的控制,具体取决于您提供的自定义程度。To achieve this, you could create a
UserControl
that contains a docked button, then you can override the behaviour of yourUserControl
so that when it auto-sizes, it sizes smaller than the button would normally require and thus forces the button text to sit closer to the edge. However, you'll need to use some trial an error to ensure that the text is visible for all DPI settings.You could also consider using
ToolStripButton
s instead, with an appropriate renderer that makes them look like regular buttons. These often size smaller and you should have more control over the padding and margins of the text within the button, depending on how much customization you provide.