【发布时间】:2014-04-30 06:48:16
【问题描述】:
我正在学习一个让我在 winforms 上启动并运行的教程。它基本上给了我窗口、按钮和标签控件。我试图添加一个正在显示的文本框,但它不允许用户输入任何内容。我究竟做错了什么。这是代码。我将所有内容都包含在内,因此很容易复制/粘贴。在此先感谢您的帮助。仅供参考,我正在使用带有 xna 4.0 的 studio 2010
namespace AddingWinformControlsTest
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Texture2D img;
Panel pnl = new Panel();
Label lbl = new Label();
TextBox txt = new System.Windows.Forms.TextBox();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// allow the mouse to remain visible and allow user resizing ofthe window
this.IsMouseVisible = true;
this.Window.AllowUserResizing = true;
// create and setup the winform controls on the game window
this.CreateControls();
// hook into the client size change event so we can update the win form
// controls and update the viewport on the graphics device
this.Window.ClientSizeChanged += this.Window_ClientSizeChanged;
}
private void CreateControls()
{
// get game window as a win form Form class
Form frm = Control.FromHandle(this.Window.Handle) as Form;
frm.SuspendLayout();
// setup the panel control
this.pnl.Dock = DockStyle.Right;
this.pnl.Width = 250;
// createa exit button and add it to the panel
Button btn = new Button();
btn.Location = new System.Drawing.Point(10, 10);
btn.Text = "Exit";
btn.Click += (sender, e) => { this.Exit(); };
// add the button to the panel and add the panel to the game window form
this.pnl.Controls.Add(btn);
frm.Controls.Add(this.pnl);
// setup the lable control and add it to the panel control
this.lbl.Text = "";
this.lbl.Location = new Point(10, btn.Top + btn.Height + 10);
this.lbl.AutoSize = true;
this.pnl.Controls.Add(this.lbl);
// Text box
txt.Text = "test";
txt.Location = new Point(100, 100);
txt.AcceptsReturn = true;
txt.AcceptsTab = true;
txt.Enabled = true;
txt.ReadOnly = false;
txt.Size = new System.Drawing.Size(50, 16);
this.pnl.Controls.Add(this.txt);
frm.ResumeLayout(false);
frm.PerformLayout();
}
void Window_ClientSizeChanged(object sender, EventArgs e)
{
// get the viewport from the graphics device
var vp = this.GraphicsDevice.Viewport;
// change the viewport dimensions so that it is not drawn under any of our winform controls
vp.Width = this.Window.ClientBounds.Width - pnl.Width;
vp.Height = this.Window.ClientBounds.Height;
// set the viewport back onto the graphics device
this.GraphicsDevice.Viewport = vp;
// update the label to display the rectangle info
Rectangle rect = new Rectangle(vp.X, vp.Y, vp.Width, vp.Height);
lbl.Text = "Client: " + this.Window.ClientBounds.ToString() +
"\r\n" +
"Viewport: " + rect.ToString();
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
this.Window_ClientSizeChanged(this, EventArgs.Empty);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// load a sample image that will be drawn the size of the viewport
this.img = this.Content.Load<Texture2D>("Map2");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// draw the image the same size of the viewport
this.spriteBatch.Begin();
this.spriteBatch.Draw(this.img, this.GraphicsDevice.Viewport.Bounds, img.Bounds, Color.White);
this.spriteBatch.End();
base.Draw(gameTime);
}
}
}
【问题讨论】:
-
由于无法进入这里的原因,我不得不使用 XNA。我感谢你的坦率,但我真的需要你的帮助
-
您不能在 XNA 游戏中使用 WinForms 控件。 XNA 负责整个渲染和交互。您必须实现基于 XNA 的控件或寻找现有的实现。
-
窗口、按钮和标签工作正常。这只是我遇到问题的文本框。这只需要在 Windows 上运行
-
文本框有点特别。它(与其他控件一样)包装在 Win32 库中,但由于其复杂的功能,它使用非常具体的绘画和 Windows 事件处理。