Hello everyone.
First of all, sorry for my bad english.
Using:
Visual Studio 2005
Visual Basic.NET
I'm developing a small Media Player, an application which can play most popular music formats, and can view some image formats.
I have my custom control (called BtnEx) that inherits System.Windows.Forms.Control
I want to solve the next problem:
ArgumentException : Parameter is not valid
This error occurs in the next line of my code:
Dim CImg as New Bitmap (PImg, e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 2)
But only in Design Mode, not when I run my program (by pressing the F5 button).
When i build my project all works fine until I press my custom control (For dragging and selecting). Thats all happening in Design Mode (IDE)
A "X" image is appearing instead of Control's Image, and Visual Studio shows MessageBox with error description. In that description I can read in which line of code error occurs. (ArgumentException occured in file "C:\ ... \BtnEx.vb" line: 67.
When I Build my project (again) the red "X" image dissapears and all loks fine, but as I said, when i click on control that exception occurs.
I'm calling this procedure in
OnPaint Sub.
This is the OnPaint Sub:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
If PImg Is Nothing = False Then
Dim CImg As New Bitmap(PImg, e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 2)
If Me.Enabled = True Then
Select Case MS
Case EMouseState.MouseHover
IMGA.SetGamma(0.57#)
e.Graphics.DrawImage(CImg, CRect, 0, 0, CRect.Width, CRect.Height, GraphicsUnit.Pixel, IMGA)
Case EMouseState.MouseDown
IMGA.SetGamma(1.77#)
e.Graphics.DrawImage(CImg, CRect, 0, 0, CRect.Width, CRect.Height, GraphicsUnit.Pixel, IMGA)
Case EMouseState.MouseUp
If InB = True Then
IMGA.SetGamma(0.57#)
e.Graphics.DrawImage(CImg, CRect, 0, 0, CRect.Width, CRect.Height, GraphicsUnit.Pixel, IMGA)
Else
e.Graphics.DrawImage(CImg, CRect)
End If
Case EMouseState.MouseLeave
e.Graphics.DrawImage(CImg, CRect)
End Select
Else
ControlPaint.DrawImageDisabled(e.Graphics, CImg, 1, 1, Color.Transparent)
End If
End If
End Sub
When I use numbers instead e.ClipRectangle.Width or e.ClipRectangle.Height it runs fine.
Example :
Dim InitWidth As Integer = 42
Dim InitHeight As Integer = 42
Dim CImg As New Bitmap (PImg, InitWidth, InitHeight)
Dim CImg As New Bitmap (PImg, 42, 42)
I have tested e.ClipRectangle.Width with MsgBox:
MsgBox (e.ClipRectangle.Width.Tostring)
I know I can do this in other ways, but I want to know why is that happening ?
One of the other ways is to use
Me.Width and
Me.Height properties of control, but what when i want to use only part of control to draw on it ??
Example using Me.Width and Me.Height properties that works.
Dim CImg As New Bitmap (PImg, Me.Width - 2, Me.Height - 2)
This is the whole Class
Public Class BtnEx
Inherits Control
Private PImg As Image = Nothing
Public Property Image() As Image
Get
Return PImg
End Get
Set(ByVal Value As Image)
PImg = Value
Me.Refresh()
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
If PImg Is Nothing = False Then
Dim CRect As New Rectangle(1, 1, e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 2)
Dim IMGA As New Drawing.Imaging.ImageAttributes
Dim CImg As New Bitmap(PImg, e.ClipRectangle.Width - 2, e.CLipRectangle.Height - 2)
If Me.Enabled = True Then
Select Case MS
Case EMouseState.MouseHover
IMGA.SetGamma(0.57#)
e.Graphics.DrawImage(CImg, CRect, 0, 0, CRect.Width, CRect.Height, GraphicsUnit.Pixel, IMGA)
Case EMouseState.MouseDown
IMGA.SetGamma(1.77#)
e.Graphics.DrawImage(CImg, CRect, 0, 0, CRect.Width, CRect.Height, GraphicsUnit.Pixel, IMGA)
Case EMouseState.MouseUp
If InB = True Then
IMGA.SetGamma(0.57#)
e.Graphics.DrawImage(CImg, CRect, 0, 0, CRect.Width, CRect.Height, GraphicsUnit.Pixel, IMGA)
Else
e.Graphics.DrawImage(CImg, CRect)
End If
Case EMouseState.MouseLeave
e.Graphics.DrawImage(CImg, CRect)
End Select
Else
ControlPaint.DrawImageDisabled(e.Graphics, CImg, 1, 1, Color.Transparent)
End If
End If
End Sub
Protected Overrides Sub InitLayout()
MyBase.InitLayout()
Me.Refresh
End Sub
Private Sub II()
Me.Refresh()
End Sub
Dim InB As Boolean
Private Enum EMouseState
MouseUp = 0
MouseHover = 1
MouseDown = 2
MouseLeave = 3
End Enum
Dim MS As EMouseState
Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
InB = True
MyBase.OnMouseEnter(e)
MS = EMouseState.MouseHover
II()
End Sub
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
InB = False
MS = EMouseState.MouseLeave
II()
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
If e.Button = Windows.Forms.MouseButtons.Left = True Then
MS = EMouseState.MouseDown
II()
End If
End Sub
Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
MyBase.OnEnabledChanged(e)
Me.Refresh()
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
MS = EMouseState.MouseUp
II()
End Sub
Public Sub New()
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.Selectable Or ControlStyles.SupportsTransparentBackColor Or ControlStyles.UserPaint Or ControlStyles.StandardClick, True)
Me.DoubleBuffered = True
MS = EMouseState.MouseLeave
Me.Refresh()
End Sub
End Class
And on the end, sorry for big question, and Thanks in Advance.