Events outside the form

S

sajin

Hi all,

I implemented the color picker in vb .net 2005 , but i can't select the
color . when ever i move the mose over the sreen or desktop i show the
color below the cusor in the picturebox which is there in the form

But i need is as follows
I will have a pictureBox (like image in the color picker (pen)) i need
to have a drag event , meaning when i drag this image to anywhere and
leave then i should be able to get the color . getting the color is not
a problem , but how to get the event

I am a bigginer
my code is as follows

Public Class Form1

Public Declare Function GetPixel Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nXPos As Int32, _
ByVal nYPos As Int32 _
) As Int32
Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr _
) As IntPtr


Private Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal hdc As IntPtr _
) As Int32
Private m_hdc As IntPtr
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
m_hdc = GetDC(IntPtr.Zero)
Me.Timer1.Enabled = True
pb.AllowDrop = True
End Sub
Private Sub Form1_Closed( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Closed
ReleaseDC(IntPtr.Zero, m_hdc)
End Sub




Private Sub Timer1_Tick( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Timer1.Tick
Me.PictureBox1.BackColor = _
ColorTranslator.FromWin32( _
GetPixel(m_hdc, Windows.Forms.Cursor.Position.X,
Windows.Forms.Cursor.Position.Y) _
)
End Sub
End Class


I tried with drag events...but not working , i am not sure how to use
that

Thanks
Sajin
 
S

Stoitcho Goutsev \(100\)

sajin,

The user needs to press the mouse button, hold it down and release it over
some part of the screen. Only in this case you are going to get MouseUp
event.
Windows sends mouse events to the window under the mouse cursor. However a
window can capture the mouse meaning that it will receive all mouse events
regardless of where the mouse is. Unfortunately the mouse capture works only
if the left mouse button is down. For more information read MSDN regarding
SetCapture API method.

Default behavior for windows forms is to capture the mouse as soon a mouse
button is pressed and release it as soon as either of the mouse buttons is
released. That means you can start with the left mouse button down then at
some point you can press the right and release the right this will kill the
capture also. Because of that you should consider operation done when
MouseUp event comes for any mouse button.

Again read MSDN about SetCapture API there are some details that you should
know when using this feature.
 
S

sajin

Thanx for ur help , this lead me to do it in another way , i did like
this , when the user click the pictureBox i changed the mouse pointer
as (custom image-pen) , after leaving the mouse click i got the color
under the cursor

Public Class Form1

Public Declare Function GetPixel Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nXPos As Int32, _
ByVal nYPos As Int32 _
) As Int32
Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr _
) As IntPtr
Private Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal hdc As IntPtr _
) As Int32
Private m_hdc As IntPtr
Dim condition As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
m_hdc = GetDC(IntPtr.Zero)
pb.AllowDrop = True
End Sub
Private Sub Form1_Closed( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Closed
ReleaseDC(IntPtr.Zero, m_hdc)
End Sub
Private Sub Timer1_Tick( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Timer1.Tick
Me.pb2.BackColor = _
ColorTranslator.FromWin32( _
GetPixel(m_hdc, Windows.Forms.Cursor.Position.X,
Windows.Forms.Cursor.Position.Y) _
)
End Sub

Private Sub pb_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles pb.MouseDown
Me.Timer1.Enabled = True
pb.BringToFront()
Dim img As Bitmap = Image.FromFile("ico.png")
Dim HIcon As IntPtr = img.GetHicon()
Me.Cursor = New System.Windows.Forms.Cursor(HIcon)


End Sub


Private Sub pb_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles pb.MouseUp

Me.PictureBox1.BackColor = _
ColorTranslator.FromWin32( _
GetPixel(m_hdc, Windows.Forms.Cursor.Position.X,
Windows.Forms.Cursor.Position.Y) _
)
Me.Timer1.Enabled = False
Me.Cursor = Cursors.Default
End Sub
End Class


Here pb is my pictureBox which user will drag to any point to get the
color,
PictureBox1 is the pictureBox where i show the selected color

Thanks
Sajin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top