Change mouse pointer on component border - c#

I'm trying to change the mouse pointer as it moves over the outer borders of a component. Sort of the same way it changes when moved over the borders of a resizable form. Any ideas?

Controls have events like
MouseEnter
MouseLeave
MouseHover
and so on. Use one of them

Related

Flickering while changing the cursor on the mouse move event of custom control

I have created a custom control.
I have drawn a text inside the control.
In that, I'm trying to change the default cursor to Hand cursor when moving the mouse pointer on a particular region(Region where the text drawn inside the control).
For that, I have changed the cursor on the MouseMove event of the control.
if (textRect.Contains(e.Location))
{
Cursor.Current = Cursors.Hand;
}
I'm getting a flickering effect when moving the mouse over the control.
That is, the cursor is changed to hand cursor and back to default cursor.
I can't able to figure out why it is happening.
Provide your valuable suggestions to resolve this.
Thanks in advance.

Events for the border of a form?

I want to know when the user has the mouse over the border of a form. I also want to be able to know if its the top/bottom or left/right border.
There does not seem to be any events for this. I have tried using MouseMove on the form but it only fires when it is "inside" the form.
I have tried looking at the Bounds property but that does not have any events.
What can I do to solve this?
You have to override the WNDPROC method and look for WM_NC* messages.
This link should get you started: Mouse events from a non-client area of a control

How to start Picturebox events from button

I have button named "Paint" which should allow drawing rectangle on my picturebox after a click, i.e. it acts like a switch to allow drawing(on/off).
I've drawn rectangle using mouse positions as explained here: How Can I Capture Mouse Coordinate on PictureBox? . But rectangle is drawn whenever I move over PictureBox.
How can I implement the functionality where drawing must be implemented only when "Paint" is 'on'
I've tried starting implementation from events of Picturebox: Paint, MouseDown, MoseMove, Mouse Up...
set a flag(bool) in your application telling you the mode you are in whether drawing or not(can be activated from the button you are telling about).
in mouse down take the start point(e.x, e.y) from the mouse event handler.
now you have the top left point of the rectangle.
3.while mouse move take e.x and e.y and which is the bottom right point and draw your rectangle. put the drawing code in mouse move so that the it draws like the "Paint" Program(do this if the draw flag is true).
in mouse up reset the drawing flag
5.in the paint event of the picturebox draw the all the shapes you have so that if you minimized your application windows and then maximized it you will find your shapes drawn this can be achieved by making the rectangle is a class and make some instances of it(for loop over your shapes and draw it).
Simplest solution is to add a boolean bDraw variable, which becomes TRUE only on button click. All other drawing methods do not do anything if this variable is FALSE.
Other solution could be simply to subscribe to
Mouse events inside button click event handler. So if button is NOT clicked, no event raise happen.

Container Control is losing focus

I'm trying to create User Control which will work like a rich button.
It's supposed to have an effect on hover - I turn border on on MouseEnter and off again at MouseLeave.
BUT, when I hover over label in my control, it fires Control.MouseLeave.
Is there any way to prevent this?
First, focus Enter and Leave are different events than MouseEnter and MouseLeave. Focus deals with keyboard input. MouseEnter and MouseLeave deals with where the mouse is right now.
Entering a nested control fires MouseLeave on the parent control. You can capture the mouse by setting the control's Capture property to true, but you may find that doesn't behave like you'd expect.
You might look at my post here. I had the same issue with nested controls. I opted to create a .NET equivalent of a mouse hook by calling Application.AddMessageFilter.
Another option would be to remove the inner label control and draw the text manually it in the button's OnPaint.

WinForm: Selection box flickers when dragging the mouse

I made a click-drag selection box in a picture box. In the picturebox Paint event hander I use
e.Graphics.DrawRectangle(pen, rectangle);
and update the rectangle and refreshe the picturebox in the mouse move event handler.
The selection box looks smooth as long as the mouse remains at the bottom-right corner (i.e. drag to right/bottom). However if I want to drag the mouse to the left or up, rectangle.X/rectangle.Y has to be re-set constantly and the box flickers very noticeably.
Is there a better/more efficient way to do the drawing? Much appreciated!
another thing to take into account is DoubleBuffering
How do I enable double-buffering of a control using C# (Windows forms)?
have a look at the excepted answer here for the correct double buffering code.
I just found the solution: Replacing pictureBox.Refresh() with pictureBox.Invalidate() would make the redrawing smooth at all times. It seems Refresh() adds huge overhead in this case, that even setting the main Form or the PictureBox's DoubleBuffered property to true will not help.

Categories