问题
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 ofToolStrip
totrue
- Set
ImageScaling
property ofToolStripItem
toNone
If you want your images show in a specific size
- Set
AutoSize
property ofToolStrip
totrue
- Set
ImageScalingSize
property ofToolStrip
to your specific size, for example32,32
- Set
ImageScaling
property ofToolStripItem
toSizeToFit
回答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