Resizing ToolStrip item's image

风流意气都作罢 提交于 2020-01-25 07:03:46

问题


Is there any way I can fit/resize the image of ToolStrip item? I already set the property AutoSize = False , ImageScaling = SizeToFit & Size = 40, 40 but only the item's boxes resizing (leave spaces around image) and the image size is still with it's default size.

I then came up with inserting the image using BackgroundImage instead of inserting it at Image. I can fit/resize the image but the problem is when I run my system and pointing the cursor on the ToolStrip item, the item's image disappear. I found this solution Resizing ToolStripButtons to fit complete BackGround image but I don't know how to apply it using vb.net

Kindly to help me. Thank you.


回答1:


If you want your images show in original size

  • Set AutoSize property of ToolStrip to true
  • Set ImageScaling property of ToolStripItem to None

If you want your images show in a specific size

  • Set AutoSize property of ToolStrip to true
  • Set ImageScalingSize property of ToolStrip to your specific size, for example 32,32
  • Set ImageScaling property of ToolStripItem to SizeToFit



回答2:


Hopefully this will help

Partial Public Class Form1
    Inherits Form
    Public Sub New()
        InitializeComponent()
        'using new render instead of def render
        toolStrip1.Renderer = New MyRenderer()
    End Sub
    Private Class MyRenderer
        'apply everything of default render
        Inherits ToolStripProfessionalRenderer

        'this will override the Render Butoon Bg event
        Protected Overrides Sub OnRenderButtonBackground(e As ToolStripItemRenderEventArgs)
            'if image is nothing then use the def render
            If e.Item.BackgroundImage Is Nothing Then
                MyBase.OnRenderButtonBackground(e)
            Else

                'redraw the image to fit the area
                Dim bounds As New Rectangle(Point.Empty, e.Item.Size)
                e.Graphics.DrawImage(e.Item.BackgroundImage, bounds)
                ' Something...
                If e.Item.Pressed Then
                    ' Something...
                ElseIf e.Item.Selected Then
                End If

                'draw the fit button here
                Using pen As New Pen(Color.Black)
                    e.Graphics.DrawRectangle(pen, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1)
                End Using
            End If
        End Sub
    End Class
End Class


来源:https://stackoverflow.com/questions/33518339/resizing-toolstrip-items-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!