2016-06-30 3 views
0

폼의 컨트롤에 대해 Paint 이벤트를 처리하려고했습니다. 그러나 이벤트는 결코 처리되지 않으며, 내가 뭘 잘못하고 있는지 확신하지 못합니다. 내가 이것을 입증하는 매우 간단한 윈폼 프로젝트를 만들었습니다 (나는 다른이 아무것도 없다는 것을 보여주기 위해 생성 된 디자이너 코드를 포함 시켰습니다) :VScrollbar Paint 이벤트 처리

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이

<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 

이벤트는 처리되지 않지만 컨트롤은 올바르게 그려집니다. 내가 뭘 놓치고 있니?

+0

스크롤 막대는 페인트 이벤트를 발생시키지 않습니다. 시스템에 의해 내부적으로 그려집니다. 소유자를 그리는 시설은 없습니다. 스크롤바를 요청하면 스크롤바가 생깁니다. 표준 시스템 모양을 무시할 이유가 없습니다. –

+0

컨트롤에 대한 이벤트 목록을 열면 페인트 이벤트가 표시되지 않습니다. 당신은 혼란스럽지 않은 것을 다루기 위해 노력하고 있습니다. – Plutonix

답변

-1

글쎄, 이것에 대해 좀 더 많은 시간을 보낸 후, 플래그와 값 매개 변수가 각각 ControlStyles.userPaintTrue으로 설정된 SetStyle() 메서드를 호출해야한다는 것을 발견했습니다.

이 방법은 그러나 공개되지 않으므로 반사가이를 호출하기 위해 필요한 :

Dim methodInfo As System.Reflection.MethodInfo = VScrollBar1.GetType().GetMethod("SetStyle", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic) 
methodInfo.Invoke(VScrollBar1, {ControlStyles.UserPaint, True}) 

가 난 그냥 AddHandler 선 위에 위를 Form1_Load 방법으로 위의 코드를 넣어이 일 것으로 보인다 (나는 사건을 처리 할 수있다).

+0

다른 사람들이 말한 것처럼 ... 그것은 이유 때문에 당신에게 숨겨져 있습니다. – DonBoitnott

+0

예, 이벤트를 처리 할 수 ​​있도록 보호 기능을 해킹 할 수 있습니다. 그러나 그것은 여전히 ​​당신의 문제를 해결하지 못합니다. 스크롤 막대 컨트롤을 owner-draw 할 수는 없으므로 Paint 이벤트를 완전히 무의미하게 처리 할 수 ​​있습니다. 스크롤바를 사용자 정의 할 수있는 유일한 방법은 WM_CTLCOLOR 메시지를 처리하여 배경색을 변경하는 것입니다. 그렇지 않으면 본질적으로 스크래치에서 직접 스크롤바 컨트롤을 만들어야하는데, 필자는 강력히 권고한다. (유용하거나 관련이 없기 때문에이 답변을 줄였습니다.) –

+0

'유용하지도 않고 관련성이있는'의견과 관련하여 제 질문과 직접적으로 관련이 있기 때문에 관련성이 있습니다. Paint 이벤트 처음부터 질문의 전제에 동의하지 않으므로 답이 아니라 질문을 downvote해야합니다. 처음에는이 이상한 질문에 대한 나의 이유는 내가 이것을 다루려고했기 때문이었습니다. http://stackoverflow.com/questions/38094955/is-there-any-way-to-remove-this-white-line- next-to-a-winforms-scrollbar – Interminable