extended picturebox
-
Hi, i have been trying to create my own version of the picture box, i did this by first creating a user control and then using the keyword
inherits System.Windows.Forms.PictureBox
What i really want to do is that i wanna be able to create a button that is 50% transparent when mouse is no over it, and it will become opaque when the mouse hovers over it. I made it a picturebox because it will load a gif image from the resx file. I have overrided the OnPaint function so that it will display the image in the control, the layout is as following. I put this control on top of another PictureBox, and when I run it, it gives me a while box instead of displaying the image. I debugged it and find that the displaying of the image is fine until this onPaint is over, (i.e. when the pointer steps out of this method, and that the PictureBox underneath is displayed, this control will turn into a white box) How can i fix this?!:confused::confused: Thx Code:Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs) MyBase.OnPaint(pe) Dim g As Graphics = Me.CreateGraphics() g.Clear(Me.BackColor) '-------------------call image from resources --------- Dim myAssembly As System.Reflection.Assembly myAssembly = Me.GetType.Assembly ' Creates the ResourceManager. Dim myManager As New _ System.Resources.ResourceManager("PictureResizer.Images", _ myAssembly) Dim myImage As System.Drawing.Image myImage = CType(myManager.GetObject("flipccw"), _ System.Drawing.Image) '---------------------------------------------- Dim bitmap As Bitmap = New Bitmap(myImage) Dim clrMatrix As ColorMatrix = _ New ColorMatrix(New Single()() _ {New Single() {1, 0, 0, 0, 0}, _ New Single() {0, 1, 0, 0, 0}, _ New Single() {0, 0, 1, 0, 0}, _ New Single() {0, 0, 0, 0.5F, 0}, _ New Single() {0, 0, 0, 0, 1}}) Dim imgAttributes As ImageAttributes = New ImageAttributes() imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap) g.DrawImage(bitmap, New Rectangle(0, 0, Me.Width, Me.Height), _ 0, 0, bitmap.Width, bitmap.Height, _ GraphicsUnit.Pi
-
Hi, i have been trying to create my own version of the picture box, i did this by first creating a user control and then using the keyword
inherits System.Windows.Forms.PictureBox
What i really want to do is that i wanna be able to create a button that is 50% transparent when mouse is no over it, and it will become opaque when the mouse hovers over it. I made it a picturebox because it will load a gif image from the resx file. I have overrided the OnPaint function so that it will display the image in the control, the layout is as following. I put this control on top of another PictureBox, and when I run it, it gives me a while box instead of displaying the image. I debugged it and find that the displaying of the image is fine until this onPaint is over, (i.e. when the pointer steps out of this method, and that the PictureBox underneath is displayed, this control will turn into a white box) How can i fix this?!:confused::confused: Thx Code:Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs) MyBase.OnPaint(pe) Dim g As Graphics = Me.CreateGraphics() g.Clear(Me.BackColor) '-------------------call image from resources --------- Dim myAssembly As System.Reflection.Assembly myAssembly = Me.GetType.Assembly ' Creates the ResourceManager. Dim myManager As New _ System.Resources.ResourceManager("PictureResizer.Images", _ myAssembly) Dim myImage As System.Drawing.Image myImage = CType(myManager.GetObject("flipccw"), _ System.Drawing.Image) '---------------------------------------------- Dim bitmap As Bitmap = New Bitmap(myImage) Dim clrMatrix As ColorMatrix = _ New ColorMatrix(New Single()() _ {New Single() {1, 0, 0, 0, 0}, _ New Single() {0, 1, 0, 0, 0}, _ New Single() {0, 0, 1, 0, 0}, _ New Single() {0, 0, 0, 0.5F, 0}, _ New Single() {0, 0, 0, 0, 1}}) Dim imgAttributes As ImageAttributes = New ImageAttributes() imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap) g.DrawImage(bitmap, New Rectangle(0, 0, Me.Width, Me.Height), _ 0, 0, bitmap.Width, bitmap.Height, _ GraphicsUnit.Pi
First, you don't need a UserControl. All you have to do is create a class file and inherit from PictureBox. Which is what you actually did in the first place. All you did was change the inheritance from UserControl to PictureBox. Next, it looks like all you're doing is replacing the PictureBox's painting code. To do that, get rid of the OnPaint function and move that code to the Paint event instead. You also won't need to call MyBase.OnPaint(). The OnPaint method is used to raise the Paint event, where the actualy painting occurs.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007