change cursor over a pictureBox - c#

i was trying to implemet an image button in winforms application as i can ...easy when using asp.net
the problem seem to be(i suspect) that when the mouse is over the image inside the picturebox
it is not responding or not triggering the mouseEnter event
it looks like if i had a picture that is smaller than the pictureBox Size it will accept the reason to trigger the event but over the image within the pictureBox it would Not ?
the trick was to set pictureBox to sizeMode=zoom. then do 2 things when the mouse is over the "imageButton" : change the size of PictureBox a little larger + change cursor to hand
so i will get a kind of mouse over effect as i could with asp.net
did anyone have that problem ?
at first i tried mouseHover, then i thought enter would do better as it only requiers the mouse to pass the borders of the picture box... both enter and hover events did not work for me ...
Edit :
the event does trigger , i can see that if i initially set sizemode to CenterImage and inside the event
i ask for sizemode=zoom, so the effect dose occur ..but cursor.current=Cursors.Hand will not change.

This should work
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Cursor = Cursors.Hand;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Cursor = Cursors.Default;
}

seem like i should have known better how to use Cursors class .
cursor=Cursors.hand;
rather than
cursor.current=Cursors.hand;
that was embarrassing ..

only add MouseMove event on pictureBox and set a Cursor for this
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Cursor = Cursors.Hand;
}

Related

How to get width of C# Form?

I'm making a text editor, and it will have the ability to be resized. However when it is resized the close button along with a few other buttons at the top are kinda stuck in the middle. I would like to do something like this.
Ex.
private void Size_Change(object sender, EventArgs e) // Event after maximize or restore
{
Close_Button.Location = Width - (width of button);
}
what is a method of doing getting the width of a form and then subtracting from that width?
Just use anchor points on the button. Like Sani Singh Huttunen said. It's located in the properties of the button.
That way it will be resized or relocated with the form
you need change "Location" to "Left" property:
private void Size_Change(object sender, EventArgs e)
{
Close_Button.Left = this.Width - Close_Button.Width;
}
or create new Point to Location property.

Hide cursor over image while using QueryCursor event in WPF

I have an image in my application and I need to draw a line according to the mouse position. What I tried to do is use QueryCursor event to get the mouse position and draw the line, which works as planned.
However, now I wish to hide the cursor while over the image so that only the line is visible. I tried to change the cursor to 'None' but then the event stopped working. What should I do?
You should be able to set the Cursor property to Cursors.None in the QueryCursor event handler and then set it back to null when the MouseLeave event fires.
This works fine for me:
<Image ... QueryCursor="Button_QueryCursor" MouseLeave="Button_MouseLeave" />
private void Image_QueryCursor(object sender, QueryCursorEventArgs e)
{
Cursor = Cursors.None;
//...
}
private void Image_MouseLeave(object sender, MouseEventArgs e)
{
Cursor = null;
}
Please set the Cursors.None to the Windows' Cursor property
i.e this.Cursor = Cursors.None;
Hope this helps.

image disappear when i minimize my form, scroll doesn't show

I have writen code below for drag and drop. when I drag an image into my panel it is added, but when i minimize the form it disappears.(it is a C# windows form application)
1st: What's the reason of that?
2nd: How can I fix it?
I set panel1 auto scroll property and allow drop to true, but when image is bigger than form size scroll doesn't appear.
3rd: How can i fix problem with scroll?
private void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
string[] imagePath =(string[]) e.Data.GetData(DataFormats.FileDrop);
Graphics g = panel1.CreateGraphics();
g.DrawImage(Image.FromFile(imagePath[0]), new Point(0, 0));
}
Probably the easiest way to display an image file is to load it into a PictureBox. That way you don't have to worry about persisting it by redrawing it.
To accomplish this you simply change your code to
string[] imagePath;
private void panel1_DragDrop(object sender, DragEventArgs e)
{
imagePath = (string[]) e.Data.GetData(DataFormats.FileDrop);
if (imagePath.Length > 0 ) pictureBox1.ImageLocation = imagePath[0];
}
You may want to add error handling to catch wrong file types.
To address scrolling you place the PictureBox inside the Panel and script the LoadCompleted event:
private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
pictureBox1.ClientSize = pictureBox1.Image.Size;
}
The Panel needs to have set AutoScroll to true, the PictureBox should sit at Location 0;0 and have SizeMode Normal. Do not Dock the PictureBox nor Anchor it to stretch! You can Dock or Anchor the Panel to your liking.

Drawing in C# winform is fairly slow

I'm creating a custom DataGridView, in which the CheckBox Shows a border when MouseHover is raised.
Here is what I've done so far.
void checkBox_MouseLeave(object sender, EventArgs e)
{
//showBorder defines whether the border is drawn.
this.showBorder = false;
this.DataGridView.InvalidateCell(this);
}
void CheckBoxMouseHover(object sender, EventArgs e)
{
this.showBorder = true;
this.CheckBox.BringToFront();
this.DataGridView.InvalidateCell(this);
}
protected override void Paint(...........)
{
..........
if (showBorder)
{
GraphicsPath border=new GraphicsPath();
border.AddRectangle(new Rectangle(checkBoxPosition.X-1,checkBoxPosition.Y-1,checkBoxSize.Width+1,checkBoxSize.Height+1));
graphics.DrawPath(new Pen(borderColor,1),border);
}
}
But is comes so slow that I have to wait for about half a second to see do border show.
Anyway, MouseLeave works fine.
So how can I improve the performance here?
In addition, how can I customize the checkbox? for example, the background color, etc.
You're using MouseHover event for the Mouse going over the control. Try MouseEnter instead. MouseHover is triggered after the mouse stays over the control for a little bit of time. MouseEnter is instant

WinForms cursor hidden only on one Form

I have a C# application with 2 simultaneous visible forms, and I need to hide mouse cursor when it is over only on one of them. If I use Cursor.Hide() it applies the change for both of them.
You need to implement this logic by using the MouseEnter and MouseLeave events one each form something like:
private void frm1_MouseEnter(object sender, EventArgs e)
{
Cursor.Hide();
}
private void frm1_MouseLeave(object sender, EventArgs e)
{
Cursor.Show();
}
do the abobe on the form that should hide the cursor and add this to the form that should make the cursor visible:
private void frm2_MouseEnter(object sender, EventArgs e)
{
Cursor.Show();
}
You can make a "blank" cursor, and set myForm.Cursor = blankCursor; This will make that specific form show a specific cursor, which could be completely transparent.
Did you try this.Cursor = Cursors.None, instead of Cursor.Hide()?
You could use the Control.MouseEnter and Control.MouseLeave events to trigger hiding or displaying the cursor
If you're hiding the cursor so that the user can't do anything on the form, consider using this.UseWaitCursor = true; instead.

Categories