2011-10-12 76 views
1

我想覆蓋datetimepicker對象刪除texte屬性_clearOnDisabled爲true時。當_readOnly屬性爲true時,我想將文本顯示爲黑色而不是灰色。覆蓋datetimepicker vb.net

所以我試着用WndProc,但我似乎每一個對象都經過我的函數不僅僅是我的datetimepicker。當我放置WM_PAINT消息時,我得到了100%的CPU。我也試着重寫的OnPaint但在其沒有得到

THX的幫助

Imports System.Drawing 
Imports System.Windows.Forms 
Imports DTP.WindowsMessages 

Public Class DTP 
    Inherits System.Windows.Forms.DateTimePicker 

    Private _readOnly As Boolean = False 
    Private _clearOnDisabled As Boolean = True 
    Private _backColorReadOnly As Color = MyBase.BackColor 

    Public Sub New() 
     MyBase.New() 
    End Sub 

    Public Overrides Property BackColor() As Color 
     Get 
      Return MyBase.BackColor 
     End Get 
     Set(ByVal Value As Color) 
      MyBase.BackColor = Value 
      If Not _readOnly Then 
       Me.Invalidate() 
      End If 
     End Set 
    End Property 

    Protected Overrides Sub WndProc(ByRef m As Message) 
     Select Case m.Msg 
      Case WM_ERASEBKGND 
       Dim g As Graphics = Graphics.FromHdc(m.WParam) 
       Dim backBrush As SolidBrush 

       If _readOnly Then 
        backBrush = New SolidBrush(_backColorReadOnly) 
        g.FillRectangle(backBrush, Me.ClientRectangle) 
       Else 
        backBrush = New SolidBrush(MyBase.BackColor) 
        g.FillRectangle(backBrush, Me.ClientRectangle) 
       End If 
       g.Dispose() 
      Case WM_LBUTTONDOWN, WM_KEYDOWN 
       If Not _readOnly Then 
        MyBase.WndProc(m) 
       End If 

       'Case WM_PAINT ', WM_NCPAINT, WM_DRAWITEM 
       ' If Not _clearOnDisabled Then 
       '  MyBase.WndProc(m) 
       ' End If 

      Case Else 
       MyBase.WndProc(m) 
     End Select 
    End Sub 

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) 
     If Not _clearOnDisabled Then 
      MyBase.OnPaint(e) 
     End If 
    End Sub 

    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs) 
     MyBase.OnPaintBackground(pevent) 
    End Sub 

    Public Property [ReadOnly]() As Boolean 
     Get 
      Return _readOnly 
     End Get 
     Set(ByVal Value As Boolean) 
      _readOnly = Value 
      Me.Invalidate() 
     End Set 
    End Property 

    Public Property BackColorReadOnly() As Color 
     Get 
      Return _backColorReadOnly 
     End Get 
     Set(ByVal Value As Color) 
      _backColorReadOnly = Value 
      If _readOnly Then 
       Me.Invalidate() 
      End If 
     End Set 
    End Property 

End Class 

回答

1

不要吃內線消息,之後卻油漆:

Case WM_PAINT 
    MyBase.WndProc(m) 
    If _clearOnDisabled Then 
    Dim dc As IntPtr = GetWindowDC(Me.Handle) 
    Using g As Graphics = Graphics.FromHdc(dc) 
     g.FillRectangle(SystemBrushes.Window, New Rectangle(SystemInformation.Border3DSize.Width, _ 
                  SystemInformation.Border3DSize.Height, _ 
                  Me.ClientSize.Width - SystemInformation.VerticalScrollBarWidth, _ 
                  Me.ClientSize.Height)) 
    End Using 
    ReleaseDC(Me.Handle, dc) 
    End If 

你可以擺脫你的OnPaint,OnPaintBackground覆蓋。

+0

嗨,這個工作太棒了!但是'Dim g As Graphics = Graphics.FromHdc(m.WParam)'有什麼區別。有沒有任何性能提升? – Naster

+0

@Naster WParam不起作用。它不用於[WM_PAINT消息](http://msdn.microsoft.com/en-us/library/dd145213(v = vs.85).aspx)。 – LarsTech