Jump to content
  • Advertisement
Sign in to follow this  
anderson_geller

D3D Device Cannot be used in a UserControl?(C#)

This topic is 4079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have created a D3D device in my own control which extends UserControl. Everything seems OK except minimizing the main window. When the app starts, the 3D objects are correctly rendered. I can change the size of the main window, rotate the 3D objects by a mouse. No Problem. But when I minimize the main form, an NotAvailableException was thrown. I copy the same code into a Form, the problem disappears. So, D3D device cannot be used in a usercontrol? My environment: WinXP + SP2, DirectX 9.0c, VS2005 ------------------------------------------------------------- 未处理 Microsoft.DirectX.Direct3D.NotAvailableException Message="应用程序中的错误。" Source="Microsoft.DirectX.Direct3D" ErrorCode=-2005530518 ErrorString="D3DERR_NOTAVAILABLE" StackTrace: 在 Microsoft.DirectX.Direct3D.Device.Reset(PresentParameters[] presentationParameters) 在 Microsoft.DirectX.Direct3D.Device.OnParentResized(Object sender, EventArgs e) 在 System.Windows.Forms.Control.OnResize(EventArgs e) 在 System.Windows.Forms.UserControl.OnResize(EventArgs e) 在 System.Windows.Forms.Control.OnSizeChanged(EventArgs e) 在 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight) 在 System.Windows.Forms.Control.UpdateBounds() 在 System.Windows.Forms.Control.WmWindowPosChanged(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ContainerControl.WndProc(Message& m) 在 System.Windows.Forms.UserControl.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.SafeNativeMethods.SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, Int32 x, Int32 y, Int32 cx, Int32 cy, Int32 flags) 在 System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified) 在 System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified) 在 System.Windows.Forms.Layout.DefaultLayout.xLayoutDockedControl(IArrangedElement element, Rectangle newElementBounds, Boolean measureOnly, Size& preferredSize, Rectangle& remainingBounds) 在 System.Windows.Forms.Layout.DefaultLayout.LayoutDockedControls(IArrangedElement container, Boolean measureOnly) 在 System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize) 在 System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args) 在 System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs) 在 System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent) 在 System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent) 在 System.Windows.Forms.Form.OnLayout(LayoutEventArgs levent) 在 System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args) 在 System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty) 在 System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property) 在 System.Windows.Forms.Control.OnResize(EventArgs e) 在 System.Windows.Forms.Form.OnResize(EventArgs e) 在 System.Windows.Forms.Control.OnSizeChanged(EventArgs e) 在 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight) 在 System.Windows.Forms.Control.UpdateBounds() 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ContainerControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 System.Windows.Forms.NativeWindow.DefWndProc(Message& m) 在 System.Windows.Forms.Form.DefWndProc(Message& m) 在 System.Windows.Forms.Control.WmWindowPosChanged(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ContainerControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 System.Windows.Forms.NativeWindow.DefWndProc(Message& m) 在 System.Windows.Forms.Form.DefWndProc(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ContainerControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WmSysCommand(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 System.Windows.Forms.NativeWindow.DefWndProc(Message& m) 在 System.Windows.Forms.Form.DefWndProc(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ContainerControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WmNcButtonDown(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(Form mainForm) 在 Com.FangZheng.DirectXControl.Program.Main() 位置 F:\DotNetProjects\DxPlaneProject\DxPlaneProject\Program.cs:行号 17 在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart() ------------------------------------------------------------- Here is my code in C# which throws the exception. ------------------------------------------------------------- public partial class DxPanel : UserControl { Device _dev = null; //-------------------------------------------------------- public void DxCreateDevice() { int adapter = 0; DeviceType devType = DeviceType.Hardware; Control renderWin = this; CreateFlags behaviorFlags = CreateFlags.SoftwareVertexProcessing; PresentParameters presentParam = new PresentParameters(); presentParam.Windowed = true; presentParam.SwapEffect = SwapEffect.Discard; presentParam.EnableAutoDepthStencil = true; presentParam.AutoDepthStencilFormat = DepthFormat.D16; this._dev = new Device(adapter, devType, renderWin, behaviorFlags, presentParam); } //-------------------------------------------------------- public DxPanel() { InitializeComponent(); } } -------------------------------------------------------------

Share this post


Link to post
Share on other sites
Advertisement
I don't know the exact details, but this is actually common (if not expected) behavior. When the form is minimized, the child control has a size of [0,0], which is the root cause of this problem. A common way to go about this to handle the resetting of the device manually to match the new size and to catch this special case. If you don't want to go about this manually, you can use the Device.DeviceResizing event to cancel the resize if the usercontrol's size is [0,0].

While you're at it, you'll probably also want to set some bool somewhere whether or not the device was resized sucessfully, so you can skip the rendering in your loop if this wasn't the case. If you try to render something while the target control is on a mimimized form, you'll probably get an InvalidCallException from Device.Present.

Hope this helps :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!