简体   繁体   English

在启动时通过 toolstrip.ImageScalingSize 以编程方式调整 ToolStrip 按钮图像的大小

[英]Resize ToolStrip Button Image programatically at startup via toolstrip.ImageScalingSize

So I've read around this and will provide relevant properties at the end.所以我已经阅读了这个并将在最后提供相关的属性。

I'm looking to store a custom ToolStrip button image size in my.settings and load them at startup, changing them to a user set size.. The code I run at startup is:我希望在 my.settings 中存储自定义 ToolStrip 按钮图像大小并在启动时加载它们,将它们更改为用户设置大小..我在启动时运行的代码是:

    Dim tss As New List(Of ToolStrip)
    tss = GetAllControls(Me).OfType(Of ToolStrip)().ToList

    For Each ts In tss
        ts.BackColor = My.Settings.ToolStripBGColor
        ts.ImageScalingSize = New Size(My.Settings.ToolStripImgScalingSize, My.Settings.ToolStripImgScalingSize)
        ts.ResumeLayout()
        ts.Invalidate()
        ts.Refresh()
    Next

    ToolStripContainer.Invalidate()
    ToolStripContainer.Refresh()

This does change the properties of all of the ToolStips.这确实会更改所有 ToolStips 的属性。 However, the images initially display at the default 16x16 UNTIL I drag them into another area of the ToolStripContainer.但是,图像最初以默认的 16x16 显示,直到我将它们拖到 ToolStripContainer 的另一个区域中。 It then resizes correctly.然后它会正确调整大小。 This tends to imply to me that it's something to so with the draw of these containers/controls (hence the blanket bombing of .invalidate, .resumelayout and .refresh!)这对我来说往往暗示着这些容器/控件的绘制也是如此(因此对 .invalidate、.resumelayout 和 .refresh 进行全面轰炸!)

Regarding proprieties, the relevant ones within designer view:关于礼节,设计师眼中的相关礼节:

ToolStripButton工具条按钮

.autosize = true .autosize = 真

.imagescaling = SizeToFit .imagescaling = SizeToFit

ToolStrip工具条

.autosize = true .autosize = 真

.imagesclaing = 16,16 (later modified by code) .imagesclaing = 16,16(后来被代码修改)

ToolStripContainer工具条容器

  • couldn't see any that would effect this!??看不到任何会影响这个的!??

This is one of those where you go round in circles for half a day over what essentially could be due to a janky aspect of .net!这是您绕着圈子转了半天,本质上可能是由于 .net 的一个笨拙方面造成的问题之一! Could be me though...虽然可能是我...

Getting this to work with AutoSize=True is always a bit confusing. AutoSize=TrueAutoSize=True一起工作总是有点令人困惑。 I've found that if you set it to False with layout suspended and then set it to True with layout enabled, that you can get the desired effect.我发现如果在布局暂停的情况下将其设置为False ,然后在启用布局的情况下将其设置为True ,则可以获得所需的效果。

That description is probably clear as mud, so here is the code pattern.那个描述可能像泥巴一样清晰,所以这里是代码模式。

    With ToolStrip1
        .SuspendLayout()
        .AutoSize = False
        .ImageScalingSize = New Size(40, 40)
        .ResumeLayout()
        .AutoSize = True
    End With

Imports System.Drawing : Imports Microsoft.VisualBasic导入 System.Drawing:导入 Microsoft.VisualBasic

Imports Microsoft.Win32 : Imports System导入 Microsoft.Win32:导入系统

Imports System.IO : Imports System.Windows.Forms导入 System.IO :导入 System.Windows.Forms

Public Class Form1

    Inherits Form
    

Private toolStripItem1 As ToolStripButton私有 toolStripItem1 作为 ToolStripButton

Private toolStrip1 As ToolStrip私有 toolStrip1 作为 ToolStrip

Public Sub New()公共子新建()

toolStrip1 = New System.Windows.Forms.ToolStrip()

toolStrip1.Size = New System.Drawing.Size(580,40)

toolStrip1.BackColor = System.Drawing.Color.MistyRose

toolStrip1.AutoSize = True

toolStripItem1 = New System.Windows.Forms.ToolStripButton()    
    
toolStrip1.SuspendLayout()

Me.SuspendLayout()

toolStrip1.Items.AddRange(New System.Windows.Forms.ToolStripButton() _

{toolStripItem1})

toolStrip1.Location = New System.Drawing.Point(0, 0)

toolStrip1.Name = "toolStrip1"

toolStripItem1.AutoSize = False

toolStripItem1.Size = New System.Drawing.Size(110,95)

toolStripItem1.BackgroundImage = Image.FromFile("D:\Book4\Resources\icos\CUT.png")

toolStripItem1.Name = "toolStripItem1"

toolStripItem1.Text = "Cut"

toolStripItem1.Font = New System.Drawing.Font("Segoe UI", 16.0!, _

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, _

                             CType(0, Byte))

toolStripItem1.TextAlign = System.Drawing.ContentAlignment.TopCenter

AddHandler Me.toolStripItem1.Click, New System.EventHandler _

                                 (AddressOf Me.toolStripItem1_Click)

Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)

Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(1500,900)

Me.BackColor = ColorTranslator.FromHtml("#808080")

Me.Controls.Add(Me.toolStrip1)

Me.Name = "Form1"

toolStrip1.ResumeLayout(False)

Me.ResumeLayout(False)

Me.PerformLayout()

End Sub结束子

Public Sub Form1_Loaded(sender As Object, e As EventArgs) _ Public Sub Form1_Loaded(sender As Object, e As EventArgs) _

                       Handles MyBase.Load

Try尝试

  Dim ico As New System.Drawing.Icon("D:\Resources\icos\kvr.ico")

  Me.Icon = ico      

Catch ex As Exception Catch ex 作为例外

End Try结束尝试

End Sub结束子

Public Shared Sub Main()公共共享子主()

Dim form1 As Form1 = New Form1()

form1.ShowDialog()

End Sub结束子

Private Sub toolStripItem1_Click(ByVal sender As Object,ByVal e As EventArgs) Private Sub toolStripItem1_Click(ByVal sender As Object,ByVal e As EventArgs)

System.Windows.Forms.MessageBox.Show("Successfully enlarged ToolStripButtonImage size")

End Sub结束子

End Class结束类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM