Resizing a form

S

Søren Reinke

Hi there

I have a little problem.

How do i make sure that a graph is not redrawn while the form with the graph
is being resized ?
I have tried to add a mouse up/down event handler on the form1, but it
dosn't get called when resizing :(

I would like to be able to resize my form, but also make sure the graph is
only redrawn when the user is not resizing any more.

Hope my question make sense :)
 
F

Frans Bouma [C# MVP]

Søren Reinke said:
Hi there

I have a little problem.

How do i make sure that a graph is not redrawn while the form with the graph
is being resized ?
I have tried to add a mouse up/down event handler on the form1, but it
dosn't get called when resizing :(

I would like to be able to resize my form, but also make sure the graph is
only redrawn when the user is not resizing any more.

Hope my question make sense :)

First, override OnResize() and the first time you enter that method,
you set a flag. You also call this.SuspendLayout();, if the flag was
wasn't set already.

then, you override OnSizeChanged. In there, you reset the flag and call
this.ResumeLayout(false);. Then draw your graph.

Frans

--
 
A

Alan Pretre

Søren Reinke said:
How do i make sure that a graph is not redrawn while the form with the
graph is being resized ?

I would like to be able to resize my form, but also make sure the graph is
only redrawn when the user is not resizing any more.

It depends on the control. Sometimes there is a BeginUpdate/EndUpdate
mechanism, other times you have to call the Win32 ValidateRect() function,
passing in the control's window handle and client area.

-- Alan
 
S

Søren Reinke

First, override OnResize() and the first time you enter that method, you
set a flag. You also call this.SuspendLayout();, if the flag was wasn't
set already.

then, you override OnSizeChanged. In there, you reset the flag and call
this.ResumeLayout(false);. Then draw your graph.

Frans


Hi Frans

Thanks for the tip, i'll try it :)
 
R

Rachel Suddeth

I have a similar problem, but I don't see how this will work. Both Resize
and SizeChanged events get called multiple times during a single resizing
drag.

-Rachel
 
F

Frans Bouma [C# MVP]

Rachel said:
I have a similar problem, but I don't see how this will work. Both Resize
and SizeChanged events get called multiple times during a single resizing
drag.

Hmm. If SizeChanged is called multiple times, it indeed has a problem.
It then gets hacky, you then have to set a timer or something :X...

FB


--
 
R

Rachel Suddeth

Thanks, Mick, looks like that should work.
BTW, where do you get the constants from (for Windows messages)? You pull
them from a C++ header file?

-Rachel

"Mick Doherty"
 
M

Mick Doherty

Depends on my mood. Sometimes I look them up (winuser.h in this case),
sometimes I google and sometimes I just put Console.WriteLine(m.ToString());
in the WndProc() method. Usually the latter since I'm often checking to see
what messages are being sent. Then it's off to the MSDN documentation to
find out what info I can read/change for the message.
 
R

Rachel Suddeth

All right, I don't know VB, and I've never needed to do masking with C#, so
I have a couple questions about how to translate the WndProc in your
example. I think the "Static Sizemode" inside the method would have to be
replaces by a class level variable in C#? Also not quite sure about the
statement to set SizeMode?

----
private bool bSizeMode = false;
protected override void WndProc(ref Message m)
{
switch ( m.Msg )
{
case WM_SIZING:
// Resize event already provided
break;
case WM_SYSCOMMAND:
bSizeMode = ((m.WParam.ToInt32() & 0xFFF0) == SC_SIZE);
break;
case WM_ENTERSIZEMOVE:
if ( bSizeMode )
{
OnBeginResize(EventArgs.Empty);
}
break;
case WM_EXITSIZEMOVE:
{
if ( bSizeMode )
{
OnEndResize(EventArgs.Empty);
}
break;
}

} // end switch
base.WndProc (ref m);
}
----

Here is the VB code "copied" above
------
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Static SizeMode As Boolean = False
Select Case m.Msg
Case WM_SIZING
'Already have a Resize Method
Case WM_SYSCOMMAND
SizeMode = ((m.WParam.ToInt32 And &HFFF0) = SC_SIZE)
Case WM_ENTERSIZEMOVE
If SizeMode Then
OnBeginResize(EventArgs.Empty)
End If
Case WM_EXITSIZEMOVE
If SizeMode Then
OnEndResize(EventArgs.Empty)
End If
Case Else
End Select
MyBase.WndProc(m)
End Sub

---


Thanks,

Rachel



"Mick Doherty"
 
R

Rachel Suddeth

Nevermind, it's working perfectly so it must be right :)
Here is the whole business in C#, in case that helps anyone...

---
// Variables having to do with the resizing code follow
protected bool bSizeMode = false;

/// <summary>
/// Windows Constants for WndProc call in BeginResize/EndResize events
/// </summary>
protected const int WM_SIZING = 0x214,
WM_ENTERSIZEMOVE = 0x231,
WM_EXITSIZEMOVE = 0x232,
WM_SYSCOMMAND = 0x112,
SC_SIZE = 0xF000;

/// <summary>
/// Fires once at the beginning of a resizing drag.
/// </summary>
public event EventHandler BeginResize;

/// <summary>
/// Fires once at the end of a resizing drag.
/// </summary>
public event EventHandler EndResize;
/// <summary>
/// Process Windows messages (in this case to provide Begin/EndResize events
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
switch ( m.Msg )
{
case WM_SIZING:
// Resize event already provided
break;
case WM_SYSCOMMAND:
bSizeMode = ((m.WParam.ToInt32() & 0xFFF0) == SC_SIZE);
break;
case WM_ENTERSIZEMOVE:
if ( bSizeMode )
{
OnBeginResize(EventArgs.Empty);
}
break;
case WM_EXITSIZEMOVE:
{
if ( bSizeMode )
{
OnEndResize(EventArgs.Empty);
}
break;
}

} // end switch
base.WndProc (ref m);
}


/// <summary>
/// Raise BeginResize event
/// </summary>
/// <param name="e"></param>
protected virtual void OnBeginResize(System.EventArgs e)
{
if ( BeginResize != null )
BeginResize(this, e);
}

/// <summary>
/// Raise EndResize event
/// </summary>
/// <param name="e"></param>
protected virtual void OnEndResize(System.EventArgs e)
{
if ( EndResize != null )
EndResize(this, e);
}
---
 
M

Mick Doherty

You're right that VB's local Static variable must be declared as a Class
level variable in C#.

....and the translation looks good to me :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top