处理VScrollbar Paint事件 [英] Handling VScrollbar Paint event
问题描述
我一直在尝试为表单上的控件处理Paint
事件.但是,该事件从未得到处理,而且我不确定自己在做什么错.我创建了一个非常简单的WinForms项目来演示这一点(我包括了生成的设计器代码,以表明那里什么都没有了):
I've been trying to handle the Paint
event for a control on a form. However the event never gets handled, and I'm not sure what I'm doing wrong. I've created a very simple WinForms project to demonstrate this (I've included the generated designer code to show that there's nothing else there):
Form1.vb
Public Class Form1
Private Sub Form1_Load( sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler VScrollBar1.Paint, AddressOf VScrollBar1_Paint
End Sub
Private Sub VScrollBar1_Paint (ByVal sender As Object, ByVal e As PaintEventArgs)
Dim str As String = "test"
System.Windows.Forms.MessageBox.Show(str)
End Sub
End Class
Form1.Designer.vb
Form1.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.VScrollBar1 = New System.Windows.Forms.VScrollBar()
Me.SuspendLayout
'
'VScrollBar1
'
Me.VScrollBar1.Location = New System.Drawing.Point(26, 56)
Me.VScrollBar1.Name = "VScrollBar1"
Me.VScrollBar1.Size = New System.Drawing.Size(17, 80)
Me.VScrollBar1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6!, 13!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 261)
Me.Controls.Add(Me.VScrollBar1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(false)
End Sub
Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
End Class
该事件永远不会被处理,但是控件是正确绘制的.我想念什么?
The event is never handled, but the control is drawn correctly. What am I missing?
推荐答案
Well, after spending a bit more time on this, I've discovered that all you need to do is call the SetStyle() method with the flag and value parameters set to ControlStyles.userPaint
and True
, respectively.
该方法不是公开的,因此需要反射才能调用它:
This method isn't public though, so reflection is needed in order to invoke it:
Dim methodInfo As System.Reflection.MethodInfo = VScrollBar1.GetType().GetMethod("SetStyle", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
methodInfo.Invoke(VScrollBar1, {ControlStyles.UserPaint, True})
我将上面的代码放在上面的Form1_Load方法中,恰好在AddHandler
行的上方,并且似乎可以正常工作(我可以处理该事件).
I put the above code into the Form1_Load method above, just above the AddHandler
line and it appears to work (I can handle the event).
这篇关于处理VScrollbar Paint事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!