![]() ![]() Is not perfect, it generates a very annoying effect 'cause I get a lot of flickering when drawing the rectangle (and yes I have the Form doubleBuffered and also I'm using the CreateParams trick to try to avoid flickering, but nothing). Then I've discover a weird trick to solve the rectangle issue by changing the opacity of the Form before drawing the rectangle in the new coordinates: Me.Opacity = 0.0R 'cause as I've said the Form is semi-transparent so if I draw a rectangle in the Form it will be semi-transparent too (or at least I don't know a way to avoid that). I've translated all the code to use it as a Form dialog to have more flexibility when selecting a region, I've replaced the entire code above to update my question, the code does not change too much just instead using a LL Hook to capture the mouse events I'm handling the mouse events of a semi-transparent maximized Form, I still drawing the rectangle on the Desktop Screen Graphics (not on the OnPaint Form event) that part of the code is the same as you can see in the code above: Private ScreenGraphic As Graphics = Graphics.FromHwnd(IntPtr.Zero) ScreenGraphic.DrawRectangle(pen, Me.SelectionRectangle) ' Using g As Graphics = Graphics.FromHwnd(IntPtr.Zero) ' Just a weird trick to refresh the painting. Me.SelectionRectangle = New Rectangle(Me.InitialLocation.X, Me.SelectionRectangle = New Rectangle(e.Location.X,ĮlseIf (e.Location.X > Me.InitialLocation.X) _ĪndAlso (e.Location.Y > Me.InitialLocation.Y) Then ' Bottom-Right If (e.Location.X Me.InitialLocation.X) _ĪndAlso (e.Location.Y Me.InitialLocation.Y) Then ' Bottom-Left Protected Overrides Sub OnMouseMove(e As MouseEventArgs) ' Return the selected rectangle area and save it. ' Make the Form transparent to take the region screenshot. Protected Overrides Sub OnMouseUp(e As MouseEventArgs) SelectionRectangle = New Rectangle(InitialLocation.X, InitialLocation.Y, 0, 0) Protected Overrides Sub OnMouseDown(e As MouseEventArgs) Public Sub New(ByVal BorderSize As Integer, ByVal BorderColor As Color) ![]() ''' Indicates the border color of the range selector. ''' Indicates the border size of the range selector. ![]() ''' Initializes a new instance of the class. Private ScreenGraphic As Graphics = Graphics.FromHwnd(IntPtr.Zero) Private SelectionRectangle As Rectangle = Rectangle.Empty ''' Indicates the rectangle that contains the selected area. Private InitialLocation As Point = Point.Empty ''' Indicates the initial location when the mouse left button is clicked. Public Property BorderColor As Color = Color.Red ''' Gets or sets the border color of the range selector. Public Property BorderSize As Integer = 2 ''' Gets or sets the border size of the range selector. Public Class RangeSelector : Inherits Form NOTE: I've updated the code that I'm using now in my last question edit. ScreenGraphic.DrawRectangle(pen, SelectionRectangle)Īnd here is the full code if someone need to inspectionate it better: Using pen As New Pen(Me.BorderColor, Me.BorderSize) ' Call the "EraseRectanglehere" method here before re-drawing ? ''' Draws the rectangle on the selected area. ' If left mouse button is hold then set the rectangle area. Private Sub MouseEvents_MouseMove(ByVal MouseLocation As Point) Handles MouseEvents.MouseMove ''' The Graphics object to draw on the screen.ĭim ScreenGraphic As Graphics = Graphics.FromHwnd(IntPtr.Zero) This is the relevant part of the code that I'm using: ''' I've tried to dispose, clear, and re-instance the Graphics object without luck, also I've seen this S.O. I just would like to update the drawn rectangle when I move the mouse to the new mouse location to expect a result like this else: ![]() I'm using a semi-transparent Form to capture the mouse events such as LeftButtonDown, LeftButtonUp and MouseMove to be able to select an area on the screen to draw a rectangle on that area, the problem is that a new rectangle is drawn every time that I move the mouse producing an annoying result like this: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |