2016-08-16 59 views
0

基本上我試圖創建一個繼承PictureBox的用戶控件。我想對新的圖片庫做出的改變之一是添加一個Opacity屬性。運行時代碼無法訪問用戶控件屬性的設計時更改

似乎一切正常,除非在設計時更改不透明度值時,該更改不會影響運行時屬性的實際值!

這裏是我的代碼:

Private _opacity As Integer 
Public Property Opacity() As Integer 
    Get 
     Return _opacity 
    End Get 
    Set(ByVal value As Integer) 
     _opacity = value 
     If Image IsNot Nothing Then 
      MyBase.Image = ChangeOpacity(_image, value) 
     End If 
    End Set 
End Property 

Private _image As Image 
Public Shadows Property Image() As Image 
    Get 
     Return _image 
    End Get 
    Set(ByVal value As Image) 
     Dim bmp As Bitmap = value 

     If bmp IsNot Nothing Then 
      _image = ChangeOpacity(bmp, Opacity) 
     Else 
      _image = bmp 
     End If 
     MyBase.Image = _image 
    End Set 
End Property 

好吧,我只是用不同的方式來避免問題。我忽略OnPaint方法,將ChangeOpacity方法的代碼移到它,並刪除了陰影屬性:Image,因爲我不再需要它了。現在問題沒有了。 但是我仍然好奇爲什麼設計時更改爲Opacity屬性沒有保存,爲什麼現在保存?!

這是我的新工作的代碼。它可能會幫助某人:

Public Class Pic 
    Inherits PictureBox 

    Private _opacity As Integer 
    Public Property Opacity() As Integer 
     Get 
      Return _opacity 
     End Get 
     Set(ByVal value As Integer) 
      _opacity = value 
     End Set 
    End Property 

    Protected Overrides Sub OnPaint(pe As PaintEventArgs) 
     If Image IsNot Nothing Then 
      Dim Gr As Graphics = pe.Graphics 
      Dim colormatrix As New ColorMatrix 
      colormatrix.Matrix33 = Opacity/100 
      Dim imgAttribute As New ImageAttributes 
      imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap) 
      Gr.DrawImage(Image, New Rectangle(0, 0, Image.Width, Image.Height), 0, 0, Image.Width, Image.Height, 
      GraphicsUnit.Pixel, imgAttribute) 
     Else 
      MyBase.OnPaint(pe) 
     End If 
    End Sub 

End Class 
+0

也許有一個影像映射變量的問題,我猜基礎映像變量被調用和使用,你能檢查你的映像設置器是否在運行時被調用嗎? – Max

+0

可能是您的不透明屬性保存到MyBase Image屬性,而不是您的_Image值。 PictureBox控件已經被雙緩衝。你在構造函數中做的不透明度範圍檢查應該發生在不透明度屬性的Set塊中。賦予_Opacity 10的缺省值。 – LarsTech

+0

@Max不管設計時間值如何,它都會被調用並使用,但使用默認的「不透明度」值! –

回答

0

Yes it's called and used but with the default Opacity value no matter what the design time value is!

變量會在設計人員定義的單一方法中分配其設計時間值。設計者可能首先初始化Image變量。