How to run a C# winform in an XNA C# project

只愿长相守 提交于 2020-01-17 04:07:14

问题


I am trying to use the PDFsharp library in an XNA game. The example code I am trying to get to work is:

 static void Main() 
    {
      Renderer renderer = new Renderer();
      PreviewForm form = new PreviewForm();
      form.RenderEvent = new PagePreview.RenderEvent(renderer.Render);
      Application.Run(form);
    }

But I don't know how to get this to run in XNA.

Is it possible to pass the graphics that are passed to the winform to the XNA graphics engine instead?


回答1:


update 2

It appears that the original links to MSDN have been broken, I can't seem to find them on MSDN anymore, if someone does, please update the post. In the mean time you may want to check out this codeproject article -- http://www.codeproject.com/Articles/21330/Easy-Rendering-with-XNA-Inside-a-Windows-Form

update after a quick check, looks like the first link at (See also: XNA and WinForms) is the same one I found.

I recommend you look into mixing winforms and XNA, try this: http://create.msdn.com/en-US/education/catalog/sample/winforms_series_1




回答2:


The First thing you need to do is to create the login form. in this form create the fields you need as public properties

public string userName {get;set;} 
public string password {get;set;}

in the login form you need to write the user info to those properties:

private void btnOk_Click(object sender,EventArgs e)
{
    this.userName = txtName.Text;
    this.password = txtPass.Text;
    this.Close();
}

then, in the game (in the part you need) write something like this:

using (var form = new frmLogin())
{
    var result = form.ShowDialog();
    if (result == DialogResult.OK)
    {
        string user = form.userName;
        string pass = form.password;
    }
}

But as an FYI - winforms don't look so good in XNA Hope it helps



来源:https://stackoverflow.com/questions/7166440/how-to-run-a-c-sharp-winform-in-an-xna-c-sharp-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!