This should be a naive problem but it costs me a whole hour without any solution...
First i create a simple form that shows a sphere:
public partial class TestForm1 : Form
{
public TestForm1()
{
InitializeComponent();
Init();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Render();
}
private void Init()
{
//codes for initializing d3d graphics goes here...
}
private void Render()
{
//render codes goes here...
}
private Device device;
}
Everything works well as supposed, except two things. First, when i enlarge the form, the d3d scene will automatically resize itself to fit, but the scene refuse to shrink itself when i shrink the form so the sphere is simply "clipped". Second, if i resize the form to zero size, i'll get an exception. See, below:
Error in the application.
-2005530585 (D3DERR_DRIVERINTERNALERROR)
at Microsoft.DirectX.Direct3D.Device.Reset(PresentParameters[] presentationParameters)
at Microsoft.DirectX.Direct3D.Device.OnParentResized(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.UserControl.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.UserControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Actually what i need is not an entire form showing a sphere but rather a user control that can be reused on many forms. So a step further, i make a d3d user control that shows a sphere:
public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
Init();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Render();
}
private void Init()
{
//codes for initializing d3d graphics goes here...
}
private void Render()
{
//set up matrices, materials, lights...
//draw the sphere
}
private Device device;
}
and then add this control to a form, setting the control's Dock property to "Fill" 'cause i wanna it to resize itself to fit the area of its parent form.
public partial class TestForm2 : Form
{
public TestForm2()
{
InitializeComponent();
}
public static void Main(string[] args)
{
TestForm2 form2 = new TestForm2();
TestControl control = new TestControl();
control.Parent = form2;
control.Dock = DockStyle.Fill;
Application.Run(form2);
}
}
Everything seems nice at first, though the two old problems still exist. However, a new problem emerges, which is probably a vital one. if i simply minimize the form, the same exception will also occur.
can anyone tell me why it works like this?
[Edited by - happyrobbie on December 29, 2005 12:49:49 AM]