What's the deal with the ToolStrip's RenderMode property?

你说的曾经没有我的故事 提交于 2019-12-03 12:25:49

问题


I never quite understood what this property was designed for. I can see that it visibly changes the design of the toolstrip and I find that setting it to System on Windows XP makes it fit much better with the WinForms style.

Is there some deeper meaning here? Does the way in which the control is rendered change at all and which mode would you recommend using?

Thanks.


回答1:


The RenderMode property allows the developer to precisely control the display of the ToolStrip (or ContextMenu). When you set the RenderMode to ManagerRenderMode, you can create a custom renderer that will allow you to customize the look of the ToolStrip. For example, the code below draws a gray gradient as the background of an the item in a ContextMenu that currently has the mouse over it.

Class CustomProfessionalRenderer
   Inherits ToolStripProfessionalRenderer

   Protected Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
      Dim r As Rectangle = e.Item.ContentRectangle

      If e.Item.Selected Then
         Dim b = New LinearGradientBrush(r, Color.FromArgb(255, 227, 224, 215), Color.White, LinearGradientMode.Vertical)
         Try
            e.Graphics.FillRectangle(b, e.Item.ContentRectangle)
         Finally
            b.Dispose()
         End Try
      End If
   End Sub

End Class

Just make sure that in your Form Load event, or some other area that is called before the toolstrip is used, you assign your custom renderer to your toolstrip:

  myToolStrip.Renderer = New CustomProfessionalRenderer()


来源:https://stackoverflow.com/questions/993061/whats-the-deal-with-the-toolstrips-rendermode-property

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