【问题标题】:WinForms: Area of a button where an image can be drawn onWinForms:可以在其上绘制图像的按钮区域
【发布时间】:2013-05-20 15:42:55
【问题描述】:

好的,我有一个按钮,它以不同的大小显示,这取决于某些设置。

我现在想在按钮上显示图像。如何获得可以绘制的矩形?

如果我查询button.DisplayRectanglebutton.ClientRectangle,我会得到返回按钮的确切大小。但是由于按钮有边框等,我无法绘制,这是错误的。

据我从文档中了解到,DisplayRectangle 应该只返回我可以绘制的矩形,不包括边框和边距之类的东西?

有没有办法得到我想要的?

更新:

Douglas Barbin 的回答让我看到了他的 SystemInformation 类,它实际上具有 Border3DSize 属性。

但是,如果我计算一下

Dim size = New Size(ctrl.Width - SystemInformation.Border3DSize.Width * 2, _
    ctrl.Height - SystemInformation.Border3DSize.Height * 2)

在我的显示器上仍有一些(确切地说是两个)像素缺失,无法完全适合图像。

【问题讨论】:

    标签: vb.net image button .net-4.0


    【解决方案1】:

    这只有在按钮的 FlatAppearance 设置为 flat 时才有效。

    Dim mySize as Size = New Size With 
    {.Height = button.Height - (button.FlatAppearance.BorderSize * 2), 
     .Width = button.Width - (button.FlatAppearance.BorderSize * 2) }
    

    对于所有其他(非平面)边框样式,边框取决于操作系统和任何已安装的主题,因此我不确定是否有一种简单的方法可以从 Visual Studio 中获取它。通过 Windows API 调用或许可以实现,但代码会非常难看。

    【讨论】:

    • 这是个好主意。不幸的是,我需要这个来实现 3D 边框外观。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多