⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d3dapp.cs

📁 《.NET游戏编程入门经典-c#篇》
💻 CS
📖 第 1 页 / 共 4 页
字号:
		base.Dispose(disposing);
	}




	/// <summary>
	/// Handle any key presses
	/// </summary>
	protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) {
		// Check for our shortcut keys (Return to start or stop)
		if (e.KeyChar == '\r') {
			mnuGo.PerformClick();
			e.Handled = true;
		}

		// Check for our shortcut keys (Escape to quit)
		if ((byte)e.KeyChar == (byte)(int)System.Windows.Forms.Keys.Escape) {
			mnuExit.PerformClick();
			e.Handled = true;
		}

		// Allow the control to handle the keystroke now
		base.OnKeyPress(e);
	}




	/// <summary>
	/// Handle system keystrokes (ie, alt-enter)
	/// </summary>
	protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e) {
		if ((e.Alt) && (e.KeyCode == System.Windows.Forms.Keys.Return)) {
			// Toggle the fullscreen/window mode
			if (active && ready) {
				Pause(true);

				try {
					ToggleFullscreen();
					Pause(false);                        
					return;
				}
				catch {
					HandleSampleException(new ResetFailedException(), ApplicationMessage.ApplicationMustExit);
				}
				finally {
					e.Handled = true;
				}
			}
		}
		// Allow the control to handle the keystroke now
		base.OnKeyDown(e);
	}




	/// <summary>
	/// Winforms generated code for initializing the form
	/// </summary>
	private void InitializeComponent() {
		// 
		// GraphicsSample
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.MinimumSize = new System.Drawing.Size(100, 100);
		#region MenuCreation/etc
		this.mnuMain = new System.Windows.Forms.MainMenu();
		this.mnuFile = new System.Windows.Forms.MenuItem();
		this.mnuGo = new System.Windows.Forms.MenuItem();
		this.mnuSingle = new System.Windows.Forms.MenuItem();
		this.mnuBreak1 = new System.Windows.Forms.MenuItem();
		this.mnuChange = new System.Windows.Forms.MenuItem();
		this.mnuBreak2 = new System.Windows.Forms.MenuItem();
		this.mnuExit = new System.Windows.Forms.MenuItem();
		// 
		// mainMenu1
		// 
		this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				this.mnuFile});
		// 
		// mnuFile
		// 
		this.mnuFile.Index = 0;
		this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				this.mnuGo,
																				this.mnuSingle,
																				this.mnuBreak1,
																				this.mnuChange,
																				this.mnuBreak2,
																				this.mnuExit});
		this.mnuFile.Text = "&File";
		// 
		// mnuGo
		// 
		this.mnuGo.Index = 0;
		this.mnuGo.Text = "&Go/stop\tEnter";
		this.mnuGo.Click += new System.EventHandler(this.ToggleStart);
		// 
		// mnuSingle
		// 
		this.mnuSingle.Index = 1;
		this.mnuSingle.Text = "&Single step\tSpace";
		this.mnuSingle.Click += new System.EventHandler(this.SingleStep);
		// 
		// mnuBreak1
		// 
		this.mnuBreak1.Index = 2;
		this.mnuBreak1.Text = "-";
		// 
		// mnuChange
		// 
		this.mnuChange.Index = 3;
		this.mnuChange.Shortcut = System.Windows.Forms.Shortcut.F2;
		this.mnuChange.ShowShortcut = true;
		this.mnuChange.Text = "&Change Device...";
		this.mnuChange.Click += new System.EventHandler(this.UserSelectNewDevice);
		// 
		// mnuBreak2
		// 
		this.mnuBreak2.Index = 4;
		this.mnuBreak2.Text = "-";
		// 
		// mnuExit
		// 
		this.mnuExit.Index = 5;
		this.mnuExit.Text = "E&xit\tESC";
		this.mnuExit.Click += new System.EventHandler(this.ExitSample);
		#endregion
	}




	/// <summary>
	/// When the menu is starting pause our simulation
	/// </summary>
	protected override void OnMenuStart(System.EventArgs e) {
		Pause(true); // Pause the simulation while the menu starts
	}




	/// <summary>
	/// When the menu is complete our simulation can continue
	/// </summary>
	protected override void OnMenuComplete(System.EventArgs e) {
		Pause(false); // Unpause the simulation 
	}




	/// <summary>
	/// Make sure our graphics cursor (if available) moves with the cursor
	/// </summary>
	protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) {
		if ((device != null) && (!device.Disposed)) {
			// Move the D3D cursor
			device.SetCursorPosition(e.X, e.Y, false);
		}
		// Let the control handle the mouse now
		base.OnMouseMove(e);
	}




	/// <summary>
	/// Handle size changed events
	/// </summary>
	protected override void OnSizeChanged(System.EventArgs e) {
		this.OnResize(e);
		base.OnSizeChanged(e);
	}




	/// <summary>
	/// Handle resize events
	/// </summary>
	protected override void OnResize(System.EventArgs e) {
		if (isHandlingSizeChanges) {
			// Are we maximized?
			isMaximized = (this.WindowState == System.Windows.Forms.FormWindowState.Maximized);
			if (!isMaximized) {
				storedSize = this.ClientSize;
				storedLocation = this.Location;
			}
		}
		active = !(this.WindowState == System.Windows.Forms.FormWindowState.Minimized || this.Visible == false);
		base.OnResize(e);
	}




	/// <summary>
	/// Once the form has focus again, we can continue to handle our resize
	/// and resets..
	/// </summary>
	protected override void OnGotFocus(System.EventArgs e) {
		isHandlingSizeChanges = true;
		isWindowActive = true;
		base.OnGotFocus (e);
	}




	/// <summary>
	/// Handle move events
	/// </summary>
	protected override void OnMove(System.EventArgs e) {
		if (isHandlingSizeChanges) {
			storedLocation = this.Location;
		}
		base.OnMove(e);
	}




	/// <summary>
	/// Handle closing event
	/// </summary>
	protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
		isClosing = true;
		base.OnClosing(e);
	}
	#endregion


}

#region Enums for D3D Applications
/// <summary>
/// Messages that can be used when displaying an error
/// </summary>
public enum ApplicationMessage { 
	None, 
	ApplicationMustExit, 
	WarnSwitchToRef
};
#endregion




#region Various SampleExceptions
/// <summary>
/// The default sample exception type
/// </summary>
public class SampleException : System.ApplicationException {
	/// <summary>
	/// Return information about the exception
	/// </summary>
	public override string Message { 
		get { 
			string strMsg = string.Empty;

			strMsg = "Generic application error. Enable\n";
			strMsg += "debug output for detailed information.";

			return strMsg;
		} 
	}
}




/// <summary>
/// Exception informing user no compatible devices were found
/// </summary>
public class NoCompatibleDevicesException : SampleException {
	/// <summary>
	/// Return information about the exception
	/// </summary>
	public override string Message {
		get {
			string strMsg = string.Empty;
			strMsg = "This sample cannot run in a desktop\n";
			strMsg += "window with the current display settings.\n";
			strMsg += "Please change your desktop settings to a\n";
			strMsg += "16- or 32-bit display mode and re-run this\n";
			strMsg += "sample.";

			return strMsg;
		}
	}
}




/// <summary>
/// An exception for when the ReferenceDevice is null
/// </summary>
public class NullReferenceDeviceException : SampleException {
	/// <summary>
	/// Return information about the exception
	/// </summary>
	public override string Message {
		get {
			string strMsg = string.Empty;
			strMsg = "Warning: Nothing will be rendered.\n";
			strMsg += "The reference rendering device was selected, but your\n";
			strMsg += "computer only has a reduced-functionality reference device\n";
			strMsg += "installed.  Install the DirectX SDK to get the full\n";
			strMsg += "reference device.\n";

			return strMsg;
		}
	}
}




/// <summary>
/// An exception for when reset fails
/// </summary>
public class ResetFailedException : SampleException {
	/// <summary>
	/// Return information about the exception
	/// </summary>
	public override string Message {
		get {
			string strMsg = string.Empty;
			strMsg = "Could not reset the Direct3D device.";

			return strMsg;
		}
	}
}




/// <summary>
/// The exception thrown when media couldn't be found
/// </summary>
public class MediaNotFoundException : SampleException {
	private string mediaFile;
	public MediaNotFoundException(string filename) : base() {
		mediaFile = filename;
	}
	public MediaNotFoundException() : base() {
		mediaFile = string.Empty;
	}


    
    
	/// <summary>
	/// Return information about the exception
	/// </summary>
	public override string Message {
		get {
			string strMsg = string.Empty;
			strMsg = "Could not load required media.";
			if (mediaFile.Length > 0)
				strMsg += string.Format("\r\nFile: {0}", mediaFile);

			return strMsg;
		}
	}
}
#endregion




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -