Resize a winform window only when resize end - c#

Is there a way to resize a Winform window just in Resize End?
This means that as long as the mouse is clicked I see lines and only when I leave the mouse (Resize end) window will resize on the screen.

You will need to use DrawReversibleFrame. Default resizing will need to be disabled (FormBorderStyle = FixedSingle).
Basic Logic is -
MouseDown - Begin tracking
MouseMove - Draw Reversible Frame
MouseUp - Stop Drawing Reversible Frame. Resize Form.
Luckily this MSDN post had working code for this. I have a made a working sample for you.
http://www.mediafire.com/download/427g2h2ajm5z62m/ResizeFrame.zip
You will need to tweak this so Form resize only happens when user 'MouseDown' near the border.

If it's fine to only have the contained controls resize then (and the Form itself - immediately) - use the Form's ResizeEnd event.
(I'm assuming this is the case, because usually there is no reason to delay the Form's resize itself, rather the contained controls', because their resize might be 'expensive'.)
Note: "The ResizeEnd event is also generated after the user moves a form".

Try this.
protected override void OnResizeBegin(EventArgs e) {
SuspendLayout();
base.OnResizeBegin(e);
}
protected override void OnResizeEnd(EventArgs e) {
ResumeLayout();
base.OnResizeEnd(e);
}

Related

Undo the border i created with ControlPaint.DrawBorder()

I am working on WindowsForm C#.
The border i create becomes problem when maximized!
private void App_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Crimson, ButtonBorderStyle.Solid);
}
Normal:
Maximized:
Am i doing something wrong?
Is there a better way of doing this?
DrawBorder() has a parameter that can be used to remove a previously drawn border, just set ButtonBorderStyle to None
Example:
ControlPaint.DrawBorder(e.Graphics, (sender as Control).ClientRectangle, Color.Red, ButtonBorderStyle.None);
The only thing I don't like as much of my approach, is that it requires calls to the Paint event, which can get slow.
See this to refresh the form at runtime
Based on the information provided, I see it twice, meaning the painted information is being retained (relative to the position of the form) from the previous instance.
I'd strongly recommend staying away from trying to paint the non-client area.
If you have a custom request such as this, create a form with FormBorderStyle.None. Then, have a custom event to paint the Split Container's border. The top panel will have the custom min/max buttons. The rest of the content should be in the lower panel that fills below the min/max button.
Stay away from non-client painting. It's more trouble than it's worth.
I did it my self :D
1) Create a button, turn its FlatStyle to flat.
2) Turn Enable property to false, also remove all text
3) Dock the button to fill.
4) If previous button,labels(control) got hidden because of button, right click the button , send it to back!

How to create fade button effect with pictureboxes

I'm trying to create an effect so when the user hovers the mouse over a picturebox, the button fades to a hoverimage, and when they leave, it fades back to the original. I'm using pictureboxes as buttons in a program. I'm doing this because all the buttons will be pictures with no button textures, so I didn't see the point in using a button. Just so you can visualize it, here is the original image:
And the image to fade to:
I could still change these images a bit, but thats the general idea.
How would I go about creating this fading effect? I'm picturing something using timers and opacity settings, but I don't know how any of that stuff could help me solve this.
E: Heres a bit of code I have. It changes from image to image when I hover, but its not a fade, and it looks very choppy.
private void pictureBox3_MouseEnter(object sender, EventArgs e)
{
pictureBox3.Image = pictureBox37.Image;
}
private void pictureBox3_MouseLeave(object sender, EventArgs e)
{
pictureBox3.Image = pictureBox38.Image;
}
pictureBox37 and pictureBox38 are invisible reference pictureboxes with the images I need.
I don't think there's any WinForms support for this kind of animation, so you'd have to do this manually. If you have more than one of these buttons, I recommend creating your own UserControl with this functionality. I can give you some pointers how to do this:
Your UserControl will have two Image properties, one for the normal and one for the hover image
You'll have to override the OnPaint method to do your own custom painting, blending these two images onto your Graphics according to a position property. This is a float where 0 indicates showing the normal image and 1 the hover image. Any value in between means a blend of these two images. I found some good blending code here.
Then you'll need a Timer to update this position dynamically and redraw the button (by calling Invalidate). I recommend using a Windows.Forms.Timer, which has the advantage that its Tick event is always executed on the main Thread, so you don't have to use Invoke to modify your control.
And last but not least you override the OnMouseEnter and OnMouseLeave methods, to set this all into motion. These methods could set a positionChange property that indicates in which direction position is changing.
Hope this helps ...

Where should I put the code to resize/reposition controls on a form in C#?

I'm using Windows Forms and I'd like to write some code to change the layout of each of the controls on the form whenever anything gets scrolled or resized. I assume there must be a standard way of doing this before a form paint is done.
EDIT: There is a DataGridView on the form. I want to change the layout whenever a column width is changed or the horizontal scroll bar is moved.
Override those two methods in your form:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
}
protected override void OnScroll(ScrollEventArgs se)
{
base.OnScroll(se);
}
whenever anything gets scrolled or resized
Please be precise.
What do you expect to change size? Where does the scrolling occur? (in the form, in list box or other)
If you want to change layout in form resize, you can do it in Form.Resize event handler.
For scrolling in form, use ScrollEventArgs
Take a look at these questions as well.
Scrolling
Form Resize event - MSDN
You don't need to create any positioning and resizing code if you place your objects inside a TableLayoutPanel. This control acts pretty much as HTML table, well not quite exactly so.
Take a look at the following link how to use TableLayoutPanel:
TableLayoutPanel Class (System.Windows.Forms)

Can I make a window not resize itself until the user lets go of the mouse button?

I have some screens with a lot of stuff on them, and the redrawing performance is pretty poor. It is possible to set a form into a resizing mode where a rectangle is shown on the screen that denotes the new window dimensions as the user resizes it, but the actual form doesn't resize until they let go of the mouse button?
Thanks!
Yes; this behavior is defined by one of the window styles, which you can turn on/off using the Control.SetStyles method. In particular, I think you want this:
myForm.SetStyle(ControlStyles.ResizeRedraw, false);
You could then hook the mousedown/resize/mouseup events and force the redraw to happen when you wanted.
You can also try turning on the double buffering style:
myForm.SetStyle(ControlStyles.DoubleBuffer, true);
See this article for details: http://msdn.microsoft.com/en-us/library/fkf25009(v=vs.100).aspx
Completely suppressing all drawing is not practical, the window frame painting is out of your direct control. Nor is it necessary, all you have to do is make your drawing fast when the form is being resized. Like this:
private bool fastRender;
protected override void OnResizeBegin(EventArgs e) {
fastRender = true;
base.OnResizeBegin(e);
}
protected override void OnResizeEnd(EventArgs e) {
base.OnResizeEnd(e);
fastRender = false;
this.Invalidate();
}
And check the fastRender variable in your Paint event handler, drawing only the minimum. Or nothing at all. If the actual delay is caused by a large number of controls then tackle that by making them invisible in ResizeBegin and visible again in ResizeEnd. Easy to do with a Panel. If it is caused by a controls that are docked or have the Anchor set so that they'll resize or move whenever the user resizes the window then you'll find Suspend/ResumeLayout useful.
Have you tried using the DoubleBuffer feature of a winform/control?
How about using ResizeEnd instead of Resize.
Open the window that is abnormally sized; then hold down the control key, hit the + key until you return to your required size. Good luck.

OnResize not firing on anchored controls when the form maximises

I have a custom control that is anchored on all four sides. I have overridden its OnResize function as I need to update a matrix when the controls aspect ratio changes. This all works fine when you resize the form by grabbing the edges, but when the form is maximised the custom controls OnResize does not get called. The control still resizes as it should so I end up with a squashed projection. Does anyone know how I can get an event to fire when my control is resized due to its parent form being maximised? I would like for all of this code to be in the control so I don't have to jump through the same hoops for every form that uses it.
That should work, as it works on a non-custom control. There may be something in your code or control causing this. You could try using the control's sizechanged event the form's resize event.

Categories