windows forms

Started by
11 comments, last by GameDev.net 17 years, 8 months ago
I didn't realize that vc++ express comes with a forms editor. So if i want to run a directx /c++ prog using a window form in say a large box, can this be done and how to add the code to a display box exactly?
Advertisement
Funny enough when I was doing the same thing, questions like this would remain unanswered. Even if you ask them a multiple of times, I seem to figure most people don't know the answer to this.

I assume all you need is the handle to the window to get your DX application up and running. You could get this by casting the handle pointer.

(HWND)pictureBox->Handle.ToPointer();

Cheers
The windows form environment is a little foreign to me. Maybe I would need a an example of the whole code to the point where you include directx. Also by including controls on the formdoes that slow the program down?

q) what limitations do yoy have?
You'll need a timer, which you will automatically disable the first time it's run. This way you can make a render loop in the timers callback function. The winform shouldn't slow it down a lot since you're running in a window. From a dev standpoint you can integrate unmanaged code in to your managed code through the window handle, and just run directX like you normally would.
Quote:Original post by PumpkinPieman
You'll need a timer, which you will automatically disable the first time it's run. This way you can make a render loop in the timers callback function. The winform shouldn't slow it down a lot since you're running in a window. From a dev standpoint you can integrate unmanaged code in to your managed code through the window handle, and just run directX like you normally would.


I think i will need a code snippet of the window and where you put the directx in.

Quote:
Funny enough when I was doing the same thing, questions like this would remain unanswered. Even if you ask them a multiple of times, I seem to figure most people don't know the answer to this.


I would like to ask for a request.

It would be VERY greatly appreciated if someone could post code for this. I have yet to see a single implementation work how it was supposed to. People say cast this and cast that, then the person always replies with that didnt work, and the person with the supposed answer is never found again. I for one (and Im sure a lot of other people) would greatly appreciate a code snippet or a reference to how to do this (run a C++ DX engine in a managed forms window). I think you might be the first on the internet to post the solution to this problem, if you provided some code (seriously).

PS: I'll give ya a cookie if you do it :)
ditto if it isn't much bother.
I did see it work for Tao.OpenGL which uses C#.Net to get a hDC. If you could set up a render context from a hDC for Direct3D this should work.

It got the hDC of the form like this:
hDC = User.GetDC(form.Handle);
I suppose you could use a panel in stead of a form as well.
Use this hDC to set up a render context

In the main loop it would call
Application.DoEvents();//draw your sceneGdi.SwapBuffers(hDC);
where Gdi.SwapBuffers is just a native dll import inside a class like this:
[DllImport("gdi32.dll", SetLastError=true), SuppressUnmanagedCodeSecurity]public static extern bool SwapBuffers(IntPtr deviceContext);


Will that work?

EDIT: starting up MSVS right now to try and build it :)
I got a direct3d device set up on a Panel. I didn't need to mess with hDC as i wrote earlier. I used Managed DirectX / Direct3D using C#, but you should be able to translate it to C++ fairly easy

Here is how i did it:

using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;//ADDEDusing Microsoft.DirectX;using Microsoft.DirectX.Direct3D;namespace DXProject{	/// <summary>	/// Summary description for DirectXForm.	/// </summary>	public class DirectXForm : System.Windows.Forms.Form	{		private System.Windows.Forms.Panel DrawPanel;		/// <summary>		/// Required designer variable.		/// </summary>		private System.ComponentModel.Container components = null;		//ADDED		private Device dev;		public DirectXForm()		{			//			// Required for Windows Form Designer support			//			InitializeComponent();						//ADDED:			InitializeGraphics();			this.DrawPanel.Paint += new PaintEventHandler(DrawPanel_Paint);			//			// TODO: Add any constructor code after InitializeComponent call			//		}		public void InitializeGraphics()		{			PresentParameters prms = new PresentParameters();			prms.SwapEffect = SwapEffect.Discard;			prms.Windowed = true;			dev = new Device(0,DeviceType.Hardware,this.DrawPanel,CreateFlags.SoftwareVertexProcessing,prms);		}		private void DrawPanel_Paint(object sender, PaintEventArgs e)		{			dev.Clear(ClearFlags.Target,System.Drawing.Color.Red, 1.0f,0);			dev.Present();		}		public static void Main()		{			DirectXForm dxf = new DirectXForm();			Application.Run(dxf);		}		/// <summary>		/// Clean up any resources being used.		/// </summary>		protected override void Dispose( bool disposing )		{			if( disposing )			{				if(components != null)				{					components.Dispose();				}			}			base.Dispose( disposing );		}		#region Windows Form Designer generated code		#endregion	}}


Greetings.
The code in the windows form with c++ is nothing like this so I am a little lost.
All I would need is a window code and point out where you insert the code

This is a form1.h
///

#pragma once


namespace form1 {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(42, 63);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(185, 143);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
this->ResumeLayout(false);

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
///
//this is form1.cpp with a picture box

// form1.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace form1;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualstyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
//where does directx go, in the picture box?

This topic is closed to new replies.

Advertisement