Prevent window redraw when resizing c# windows forms - c#

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

Related

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
}

How to prevent shadow from hiding under window?

I have this issue where I open an extra window with window.show()
Which logically will draw on top of my first window(the part where the shadow is drawn).
But I want the new window to draw behind the shadow, or at least force the first window to redraw on top, so I don't get this effect that if I mouse over my original window the shadow pops back into view.
Any ideas? I tried window.focus() and window.Active(), window.show()..
Thanks!
http://i.imgur.com/GTes1NR.gifv
add eventHandler onloaded for this new Window in first window and in this handler you can set Focus on first window (this)
window.Loaded += (sender, args) => this.Focus();
window.Show();
For me it works.

c# detect windows really resized instead moved

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.

Some questions about Windows Presentation Foundation (C#)

I am working on a program that contains (among other things) a WPF window for which I am using the next code to maximize it at a MouseDoubleClick Event:
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
this.Topmost = true;
Now, what I want to do is that when the window is maximized and the mouse exits the screen (goes to the bottom of the screen until it exits the screen) a new window to appear at the bottom of the screen(WPF or WindowsForm) that will contains several things (buttons, a scrollBars, etc) and that will be active only as long as the mouse is over it (just like in BSplayer). My question is how to do that ? I'm really a starter with WPF, I don't know XAML and I would prefer to do as much as I can using C# code.
So: how do I know when the mouse leaves the screen and how do I make that window to appear on bottom of the screen (without minimizing or doing anything else with the original window) ?
I tried using this.MouseLeave but it doesn't work when the window is maximized.
And if I am asking this question here, I will use my chance to also ask two other things:
When the WPF window is maximized and if the mouse hasn't been moved for more than 5 seconds, than I want the mouse to be hidden and to become visible again only when the mouse moves. How do I do this ?
When the WPF window is not maximized, I want that the border of the screen to be very small, almost invisible (no minimize, close or other button). I am using this.WindowStyle = System.Windows.WindowStyle.ToolWindow but it still leaves the exit/close button there; If I use this.WindowStyle = System.Windows.WindowStyle.None it looks perfect, but then I can't move the window. I there anyway to make the window movable with WindowStyle.None ? Preferably, when I keep the mouse pressed on the interior of the screen, I want to be able to drag the WPF window around on my screen.
Really need help to these problems. It's a pretty important project that I am working on.
Answer to this question
When the WPF window is maximized and if the mouse hasn't been moved for more than 5 seconds, than I want the mouse to be hidden and to become visible again only when the mouse moves. How do I do this ?
This can be achieved by using the timer with an interval of 5 seconds. When timer elapse set mouse cursor to None to hide it and when mouse moves, reset the mouse cursor to original one.
Put below code in the constructor:
this.MouseMove += new MouseEventHandler(MainWindow_MouseMove);
tm = new System.Timers.Timer();
tm.Interval = 5000;
tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);
tm.Start();
Below are event defination:
void MainWindow_MouseMove(object sender, MouseEventArgs e)
{
tm.Stop();
tm.Start();
// Reseting the time back to original. Here I have assumed that original one is Arrow.
this.Dispatcher.Invoke(new Action(() =>
{
Mouse.OverrideCursor = Cursors.Arrow;
}));
}
void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Dispatcher.Invoke(new Action(() =>
{
if (Mouse.OverrideCursor != Cursors.None)
{
Mouse.OverrideCursor = Cursors.None;
currentCursor = Mouse.OverrideCursor;
}
}));
}
Hope this helps !!

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