I have a winforms application with a parent panel contains PictureBox and Label.
The application has an event on mouse enter on the parent panel to do some animation that hides the picturebox and shows the label.
It also has another event on mouse leave to handle the animation that hides the label and shows picture box.
What's happening is when mouse enters panel, animation kicks in and does what it does, but when the mouse enters label it practically leaves panel and the reverse animation kicks in.
My question is -
how can I prevent mouse leave event when my mouse enters child control of the panel?
Check if the mouse is still in the panel
private void Panel1_MouseLeave(object sender, EventArgs e)
{
if (!(sender as Panel).ClientRectangle.Contains(PointToClient(Control.MousePosition)))
{
//do animation
}
}
Related
How to remove that picturebox background after picture box?
I want to make when a mouse hover some picturebox slot, it will show tooltip text. But it was very disturbing picture box slot and must be made invisible.
Look here
Thank you :)
Controls have a Visbile property, if you set it to fase, the control will not be visibe anymore.
Or is your issue when to make it invisible? You may need to handle the MouseLeave event, and place this code there:
private void OnMouseLeavePicturebox(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
In the UserControl I made it is possible to add Rectangles by clicking. Those rectangles are saved in a List. Now I want to make it possible for the User to move those Rectangles that are drawn.
First I tried to add the MouseDown, MouseMove and MouseUp event to the Rectangle but that doesn't work because Drawing Rectangle is a Struct and not a control. I already did a Testproject and accomplished to move a button I put in in the UserControl by Designer. I tried with the code I got from Drag and Drop Function with Drawing Rectangle C# .net - Forms but this example isn't about Rectangles. It is about Controls and I don't know how to use this idea for Rectangles because
rectangle.MouseDown += delegate(object sender, MouseEventArgs e)
{
//do something
}
doesn't work. Any Ideas how to drag and drop Rectangles that are added dynamically?
You should add the MouseDown event to the UserControl. Then loop through the list and make a method to see if the coordinates are inside of a Rectangle. If it is, then hook and move it.
So you should add a bool value to, which is set to true inside the MouseDown event and set to false inside the MouseUp event. Then in the MouseMove event, check if the mouseDown value is true. If it is and a Rectangle was found, move it's position.
In my winform program I need to detect when the form is resized: but the ResizeEndmethod is also called when the form is simply moved into the desk..
Is it possible check only when the windows is only resized??
In my mind I can save the last width and the last height and into the ResizeEnd method like this:
int lastWidth;
int lastHeigth;
private void frmMain_ResizeEnd(object sender, EventArgs e)
{
if (lastHeigth != this.Height || lastWidth != this.Width)
{
lastHeigth = this.Height;
lastWidth = this.Width;
fireResize();
}
}
But this is an ugly solution...
Only marginally better than your original solution, but at least it adresses the problem instead of just quoting the docs.
Obviousy the problem is that Resize fires all the time, so a flag seems to be necessary:
bool sizing = false;
private void Form1_ResizeEnd(object sender, EventArgs e)
{
if (!sizing) return;
if (sizing) {sizing = false; /*do your stuff*/ }
}
private void Form1_Resize(object sender, EventArgs e)
{
sizing = true;
}
Of course it would be nice to have an indicator in the EventArgs of ResizeEnd but can't see a simpler way to do it.
BTW, instead of checking Width and Height using Size would also be a small improvement..
Why not use this? This works fine for me...
public Form1()
{
this.Resize += Form1_Resize;
}
void Form1_Resize(object sender, EventArgs e)
{
// do what you want to do
}
Here read this from MSDN
The ResizeBegin event is raised when the user begins to resize a form, typically by clicking and dragging one of the borders or the sizing grip located on the lower-right corner of the form. This action puts the form into a modal sizing loop until the resize operation is completed. Typically, the following set of events occurs during a resize operation:
A single ResizeBegin event occurs as the form enters resizing mode.
Zero or more pairs of Resize and SizeChanged events occur as the form's Size is modified.
A single ResizeEnd event occurs as the form exits resizing mode.
Note:
Just clicking without dragging on a border or resizing grip will generate the ResizeBegin and ResizeEnd events without any intermediate Resize and SizeChanged event pairs.
The ResizeBegin and ResizeEnd pair of events is also raised when the user moves the form, typically by clicking and dragging on the caption bar. These events are not generated by programmatic manipulation of the form, for example by changing the Size or Location properties.
Use simple Resize event. It's only triggered on resizing.
How about checking the documentation? The second search engine hit is the following.
Form.ResizeBegin Event - MSDN
The ResizeBegin event is raised when the user begins to resize a form, typically by clicking and dragging one of the borders or the sizing grip located on the lower-right corner of the form. This action puts the form into a modal sizing loop until the resize operation is completed. Typically, the following set of events occurs during a resize operation:
A single ResizeBegin event occurs as the form enters resizing mode.
Zero or more pairs of Resize and SizeChanged events occur as the form's Size is modified.
A single ResizeEnd event occurs as the form exits resizing mode.
Just clicking without dragging on a border or resizing grip will generate the ResizeBegin and ResizeEnd events without any intermediate Resize and SizeChanged event pairs.
The ResizeBegin and ResizeEnd pair of events is also raised when the user moves the form, typically by clicking and dragging on the caption bar. These events are not generated by programmatic manipulation of the form, for example by changing the Size or Location properties.
I've created a form that's maximized over the entire screen.
Within that form, and sized to fill the entire form, I've placed a panel with background color set to Red. The form's TransparencyKey is set to Red.
Therefore, the Panel is like a "keyhole" - you can see the desktop that's directly underneath it.
When the user clicks on the panel, OR, presses a key on the keyboard, I want to take action.
But, because the panel is completely transparent, when it is clicked or a key is pressed, nothing happens. If I make the panel non-transparent (setting it's background color to Blue, for example), it does respond to clicks.
What's the best way to get the panel to respond to clicks and keypresses?
Do I have to hook all mouse an keyboard events on the entire system or is there a simpler way?
The panel does not take in text input, you can set your main form to handle the keypress event.
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.form_KeyPress);
http://screensnapr.com/v/VovWAi.png
As for getting the mouse location on your panel, you can use the mouseclick event
this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseClick);
http://screensnapr.com/v/DB3kCQ.png
I have tested it, and it works on a fullsize transparent panel
I've discovered that I can use GetAsyncKeyState to respond to both keypresses and mouse clicks.
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(System.Windows.Forms.Keys vKey);
Then, I can call GetAsyncKeyState(Keys.LButton) and GetAsyncKeyState(Keys.Escape), for example. Much simpler than hooking all keyboard and mouse events.
I want to show a tooltip when hovering over a button and as long as the mouse is over the button the tooltip should follow the mouse. What is the correct way to achieve that?
When I add a MouseMove event that calls tooltip.Show(...) with the updated mouse position it flickers extremely, and also redraws the tooltip when the mouse rests. And if it is an OwnerDraw tooltip I can see the default system tooltip style "fighting" with the self-drawn tooltip.
Indeed, with .Net 2.0 the ToolTip object has been altered. Before 2.0, there were some inconsistency problems when the ToolTip text was changed while the ToolTip was active, or with some other situations.
Since 2.0, the Tooltip is hidden each time something happens what could affect the currently active Tooltip.
While this solved some problems, it now causes some events being fired right after e.g. a SetToolTip(), even if this function has been called from within this very event, resulting in an endless loop of ToolTip draw/hide until the mouse moves away from the ToolTip area.
My own workaround is to check whether the ToolTip is already the same and omitting the Set ToolTip() if so. (simply omitting the next event by a static flag as suggested above can cause problems as there is no guarantee that there will be a new event right after, e.g. if the mouse has just touched the ToolTip area and moved away already).
Also, using OnMouseHover just to display a Tooltip disables the internal timer functionality of the ToolTip component as well as causing many many unnecessary events and therefore wastes processor time. The Popup Event of the ToolTip component serves well as point of action.
In this special case, however, OnMouse Hover is necessary to track the mouse movement.
Anyways, altering the ToolTip position causes a complete redraw of the Tooltip and therefore flicker. This can be reduced for a motionless mouse by checking whether the mouse position has changed between two events.
Unfortunately, the ToolTip component has no way to change the position of the ToolTip adn is shown always relative to the current mouse position. So the only way to have it follow the mouse is to close and redraw it.
it MAY help to set the UseFading and/or UseAnimation properties to false so the flicker can be further reduced.
OK, this may be complete overkill, and probably not the best solution, but I think a fun little hack nonthless.
Basically, I'm drawing a ListView at the location of the mouse. Some code:
ListView v = new ListView();
public Form1()
{
InitializeComponent();
v.Items.Add("Foo");
v.Height = 30;
v.Width = 50;
this.button1.Controls.Add(v);
v.MouseMove += new MouseEventHandler(v_MouseMove);
v.BackColor = SystemColors.Info;
this.button1.MouseMove += new MouseEventHandler(button1_MouseMove);
}
void v_MouseMove(object sender, MouseEventArgs e)
{
v.Location = new Point(v.Location.X + e.Location.X, v.Location.Y + e.Location.Y);
}
void button1_MouseMove(object sender, MouseEventArgs e)
{
v.Location = e.Location;
}
I've noticed that when manually showing a tooltip with OnMouseHover, OnMouseMove gets called one more time after the tooltip is shown. As a hack, I've ignored the next OnMouseMove call immediately following the tooltip being shown (using a flag). Perhaps a similar phenomenon is occurring?