Source: Network
// -----------------------------------------------------------------------------
// File: createdevice. CS
// Create a device
// Desc: This is the first tutorial for using direct3d. In this tutorial, all
// We are doing is creating a direct3d device and using it to clear
// Window.
// Note: This is the first teaching example of using d3d. In this example, we only need to create a d3d "device" and refresh the window.
// Copyright (c) Microsoft Corporation. All rights reserved.
// -----------------------------------------------------------------------------
// This project is C # ConsoleProgram
Using System;
Using System. drawing;
Using System. Windows. forms;
Using Microsoft. DirectX;
Using Microsoft. DirectX. direct3d;
Namespace Devicetutorial
{
Public Class Createdevice: Form
{
Device = Null ; // Our drawing devices
Public Createdevice ()
{
This . Clientsize = New System. Drawing. Size ( 640 , 480 ); // Set the initial value of the form.
This . Text = " D3d tutorial 01: createdevice " ; // Set the Form title
}
Public Bool Initializegraphics ()
{
Try
{
// Now let's set some options for d3d.
Presentparameters presentparams = New Presentparameters ();
Presentparams. receivwed = True ; // Indicates the window mode when the program is running.
Presentparams. swapeffect = Swapeffect. Discard; // Return or set swap area options
Device = New Device ( 0 , Devicetype. hardware, This , Createflags. softwarevertexprocessing, presentparams ); // Create a device instance
// Function Description: New Device (physical device (0: default device), device type (hardware is selected here), create a form for the graphic device, create a type, and create an object)
Return True ;
}
Catch (Directxexception) // Catch DX exceptions
{
Return False ;
}
}
Private Void Render () // Refresh Module
{
If (Device = Null ){ Return ;}
Device. Clear (clearflags. Target, color. Blue, 1.0f , 0 ); // Fl the device window to blue
// Function Description: clear (the fl parameters here select the target, color, depth (which may be used as a template), and template (0: no template is used ))
Device. beginscene (); // Start rendering the scene (because there is no scene, the rendering of the scene is directly ended if the sentence is empty)
//
// You can render the scenario here.
//
Device. endscene (); // End scene rendering
Device. Present ();
}
Protected Override Void Onpaint (system. Windows. Forms. painteventargs E) // Override onpaint Method
{
This . Render (); // Refresh cycle window
}
Protected Override Void Onkeypress (system. Windows. Forms. keypresseventargs E) // Override onkeypress Method
{
If (( Int )( Byte ) E. keychar = ( Int ) System. Windows. Forms. Keys. Escape)
{
This . Close (); // Exit the program if you press ESC.
}
}
/// <Summary> Main function of the program, entry point </Summary>
Static Void Main ()
{
// Use the using statement to create an object and ensure that the object is destroyed.
Using (Createdevice FRM = New Createdevice ())
{
If ( ! FRM. initializegraphics ()) // Initialization
{
MessageBox. Show ( " Cocould not initialize direct3d. This tutorial will exit. " );
Return ;
}
FRM. Show ();
// Message Loop
While (FRM. Created)
{
FRM. Render ();
Application. doevents ();
}
}
}
}
}