c# detect windows really resized instead moved - c#

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.

Related

Why does my FlowLayoutPanel automatically scroll back to the top when the form loses and regains focus?

I am creating an image browser that uses a FlowLayoutPanel to display thumbnails of images. See animated GIF that shows how I scroll down the panel, switch to another window, and then back to the form which causes the FlowLayoutPanel to scroll back to the top. I can't imagine any reason why it would do this.
Also, I seem to be able set the scroll location by clicking on the panel. When the form loses and regains focus, it scrolls back to the last Y position that I clicked.
Why is it exhibiting this behavior and how can I prevent it from occurring?
The effect described is quite common: when a FlowLayoutPanel contains Controls that can be activated and one of these child Controls is selected at some point (in this case a UserControl, which has the WS_EX_CONTROLPARENT extended style, so SetStyle(ControlStyles.Selectable, false) won't do much) and the FlowLayoutPanel is then scrolled to hide this Control, when the Form is deactivated and then activated again, the ActiveControl is scrolled into view.
This causes the FlowLayoutPanel to scroll to a position where the child ActiveControl is visible.
▶ This doesn't happen when the child Controls are not selectable, as the PictureBox Control, for example. If this Control were used to present the thumbnails (as shown in the question), the FlowLayoutPanel would not scroll.
I think the simplest way to prevent the FlowLayoutPanel from scrolling to the ActiveControl is to set the FlowLayoutPanel itself as the ActiveControl when the Form is deactivated, handling the Deactivate event.
private void form1_Deactivate(object sender, EventArgs e)
{
this.ActiveControl = this.flowLayoutPanel1;
}
This has no meaningful side affects, except the Control that previously was the ActiveControl will raise the Leave event.
It may be also used to suspend some other activity, since the User is now focusing on another Window.
▶ To set the ActiveControl to the default one instead (the Control that is activated when the Form is first shown), set this.ActiveControl = null;. It will be reset when the Form is activated again.
I've seen sometimes the Activated and Deactivate events used to disable and enable back a ContainerControl: this also prevents the scrolling, of course, but may cause unwanted cascading effects when child Controls are disabled.
But it may also be something expected and possibly desired. It depends on what happens behind the scene (implementation details).
The solution proposed by #Loathing in comments can also work, deriving a Custom Control from FlowLayoutPanel. It depends on the use case.
Stop form from scrolling when moving controls

How to bubble event in winforms c#?

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
}
}

Is there an event in Powerpoint when the window is minimized maximized or changed in size?

Is there an event in Powerpoint when the window is minimizedm maximized or changed in size? I need to do some operation when the active Powerpoint window is maximized or the size is changed.
Additional Note:
The need for this is, i have 3 monitors with different resolutions in extended mode,
so when i drag my Powerpoint application from one monitor to another and place it, this size change event should fire, so i can change the size of the taskpane based on monitor resolution size.
Found an alternative, Though i can't find the minimize/maximize etc, i at least have an event which fires when the size of the window is changed.
Just use the SizeChanged Event inside the UserControl which was binded to the CustomTaskPane you created in Powerpoint.
this.SizeChanged += Event_SizeChanged;
private void Event_SizeChanged(object sender, EventArgs e)
{
//Your code here
}

Prevent window redraw when resizing c# windows forms

What windows message or event can i listen to in order to stop a window from being redrawing every pixel of it's resize?
That is, when a user clicks on the edge of the window and starts to re-size it, i don't want to re-draw the entire contents until he lets go. This is because for some reason it's currently choppy at resizing probably because everything is re-docking and what not.
I tried WM_SIZING but that only tells me it's being re-sized, i wish to know the start and end of the sizing so i can suspend the layout until the user stops resizing.
Nevermind, just found these two events.
this.ResizeBegin += (s, e) => { this.SuspendLayout(); };
this.ResizeEnd += (s, e) => { this.ResumeLayout(true); };
Works a treat

Anchoring a Windows Forms Tooltip to the mouse

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?

Categories

Resources