Direct3D in a PictureBox - C#
Hey, I''ve got Direct3D up and running in a picture box control in C# (it was surprisingly easy and worked the first time). However, I find that when I minimize the application, I get an error: "InvalidCallException" on a line in my OnResetDevice code:
public void OnResetDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
// Turn off culling, so we see the front and back of the triangle
dev.RenderState.CullMode = Cull.None;
dev.RenderState.Lighting = false; << ERROR
I strongly suspect that it has something to do with the fact that this event code is handled by my GraphicsWindow class instead of the main form. By that I mean that the PictureBox control itself "owns" the direct3d device:
device = new Device(0, DeviceType.Hardware, window, CreateFlags.SoftwareVertexProcessing, presentParams); //Create a device
device.DeviceCreated += new System.EventHandler(this.OnCreateDevice);
device.DeviceReset += new System.EventHandler(this.OnResetDevice);
this.OnCreateDevice(device, null);
this.OnResetDevice(device, null);
(in the above code "this" is a picture box control)
So maybe I have to add some event to handle Resetting the device in my main form? Or insert a stub event that calls the event in the GraphicsWindow?
Thanks for any help!
----------------------------------------
Let be be finale of seem, seems to me.
----------------------------------------
Coding:
http://www.stanford.edu/~jjshed/coding
Miscellany:
http://www.stanford.edu/~jjshed
Yes, you are right. There is a difference when handling forms and custom controls like picture box. I think all you have to do is hook on DeviceResizing event:
device.DeviceResizing += new System.CancelEventHandler(this.DeviceResizingEventHandler);
And in DeviceResizingEventHandler return true when your application is minimized...
device.DeviceResizing += new System.CancelEventHandler(this.DeviceResizingEventHandler);
And in DeviceResizingEventHandler return true when your application is minimized...
That seems promising. I''m just starting with C# and DirectX, do you think you could give a little bit more detail? In specific, I don''t understand two things:
1. What do you mean by "return true" if the window is minimized? The DeviceResizing event handler delegate seems to have a void return type.
2. How can I find out if the window is minimized or not? I used to know how to do it in Visual Basic, but it''s been forever since I''ve had to do something like test if a window is open.
Thanks! I would have taken me forevAr to figure out to hook the DeviceResize event on my own (I don''t have a very good understanding of device events).
----------------------------------------
Let be be finale of seem, seems to me.
----------------------------------------
Coding:
http://www.stanford.edu/~jjshed/coding
Miscellany:
http://www.stanford.edu/~jjshed
1. What do you mean by "return true" if the window is minimized? The DeviceResizing event handler delegate seems to have a void return type.
2. How can I find out if the window is minimized or not? I used to know how to do it in Visual Basic, but it''s been forever since I''ve had to do something like test if a window is open.
Thanks! I would have taken me forevAr to figure out to hook the DeviceResize event on my own (I don''t have a very good understanding of device events).
----------------------------------------
Let be be finale of seem, seems to me.
----------------------------------------
Coding:
http://www.stanford.edu/~jjshed/coding
Miscellany:
http://www.stanford.edu/~jjshed
As evidence that I am not a complete idiot, I have figured out 90% of the solution:
public void DeviceResizingEventHandler(object sender, System.ComponentModel.CancelEventArgs e)
{
Device dev = (Device)sender;
if(dev.CheckCooperativeLevel())
{
e.Cancel = true;
return;
}
if(((Form)window.Parent).WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
e.Cancel = true;
return;
}
}
However, it also seems that I might want to cancel the event if the Form holding my PictureBox is not active. Is this true? If so, how can I determine if a form is active or not?
Thanks!
----------------------------------------
Let be be finale of seem, seems to me.
----------------------------------------
Coding:
http://www.stanford.edu/~jjshed/coding
Miscellany:
http://www.stanford.edu/~jjshed
public void DeviceResizingEventHandler(object sender, System.ComponentModel.CancelEventArgs e)
{
Device dev = (Device)sender;
if(dev.CheckCooperativeLevel())
{
e.Cancel = true;
return;
}
if(((Form)window.Parent).WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
e.Cancel = true;
return;
}
}
However, it also seems that I might want to cancel the event if the Form holding my PictureBox is not active. Is this true? If so, how can I determine if a form is active or not?
Thanks!
----------------------------------------
Let be be finale of seem, seems to me.
----------------------------------------
Coding:
http://www.stanford.edu/~jjshed/coding
Miscellany:
http://www.stanford.edu/~jjshed
Maybe (((Form)window.Parent).Focused ? But, after you lose focus, you don`t need reset anyway, so I don`t see a point in canceling reset on that event. But, if you don`t want to render when your applications is out of focus (not active), render only when your app has focus, like:
if( ((Form)window.Parent)).Focused )
{
Render();
...
}
if( ((Form)window.Parent)).Focused )
{
Render();
...
}
Hey, it seemed like I had that resizing thing all sorted out by adding the CancelEventHandler to the resize message. However, I have recently discovered that I now have a new problem:
Since I''m canceling the resize event for the device, whenever I change the size (increasing it) of the PictureBox that I''m using DirectX to visualize through, I can tell that the resolution on the window is not getting larger. DirectX still draws in the entire control, but if the control''s initial size was 100,100 and I resize my window to make it very large (like 500,500), DirectX merely scales up the 100,100 to make a 500,500 image with blocky 5x5 pixels. This is just a mess - how can I fix it?
PS - What''s the deal with managing these OnDeviceLost messages anyways? Can''t DirectX keep track of it''s stuff and make sure it doesn''t get lost? OpenGL doesn''t have this problem.
----------------------------------------
Let be be finale of seem, seems to me.
----------------------------------------
Coding:
http://www.stanford.edu/~jjshed/coding
Miscellany:
http://www.stanford.edu/~jjshed
Since I''m canceling the resize event for the device, whenever I change the size (increasing it) of the PictureBox that I''m using DirectX to visualize through, I can tell that the resolution on the window is not getting larger. DirectX still draws in the entire control, but if the control''s initial size was 100,100 and I resize my window to make it very large (like 500,500), DirectX merely scales up the 100,100 to make a 500,500 image with blocky 5x5 pixels. This is just a mess - how can I fix it?
PS - What''s the deal with managing these OnDeviceLost messages anyways? Can''t DirectX keep track of it''s stuff and make sure it doesn''t get lost? OpenGL doesn''t have this problem.
----------------------------------------
Let be be finale of seem, seems to me.
----------------------------------------
Coding:
http://www.stanford.edu/~jjshed/coding
Miscellany:
http://www.stanford.edu/~jjshed
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement