本文介绍了构建C#GUI
而不使用Visual Studio GUI设计器(Toolbox)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java Swing中,我们可以创建仅使用Java编码的GUI(例如在Eclipse中)。使用NetBeans的工具箱将组件拖放到UI是可选的。
In Java Swing, we can create GUI with only coding Java (for example in Eclipse). Using NetBeans's toolbox to drag and drop components to the UI is optional.
我想知道C#中是否有相同的概念。我可以把我的组件放到我的GUI中,只用编码添加它们的行为吗?这样我就觉得我有更多的控制我的应用程序。
I am wondering if there is the same concept in C#. Can I put my components into my GUI and add them behaviors with only coding? That way I would feel that I have more control over my application.
示例:我不想g工具箱将mousehover添加到我的按钮!相反,我想自己编写代码。我知道我可以在哪里找到代码,但是它是唯一应该写这行代码的地方吗?
Example: I do not want to g to toolbox to add "mousehover" to my button! Instead, I want to write the code myself. I know where I can find the code, but is it the only place that I should write that line of code?
请将Java Swing与C#进行比较。 p>
Please compare Java Swing with C# in this.
推荐答案
关于C#:
为了让您运行 C#应用程序从 cmd ,需要以下步骤:
Regarding C# :
In order for you to run a C# application from a cmd, following steps are needed :
转到文件系统上的 C:\Windows\Microsoft.NET\Framework\v4.0.30319 位置并复制路径。 现在右键单击计算机转到属性。 系统属性,选择高级选项卡,然后单击环境变量。 在环境变量 用户变量之前,选择新建。 对于变量名称写入 CSHARP_HOME 或某事另外,尽管我正在使用同样的进一步需求来解释这一点。对于变量值只需粘贴您在步骤1 中复制的内容。单击确定。 再次执行步骤4 ,如果路径变量不存在,否则可以简单选择路径,然后单击编辑执行下一个任务(放入;(分号)在变量值的末尾,并写入%CSHARP_HOME%\ (或使用您在步骤5 ))。这个时间为变量名称写入路径,而对于变量值使用%CSHARP_HOME%\ ,然后单击确定。 打开 cmd 并输入 csc ,然后按 ENTER ,您可能会看到像输出一样的 现在考虑我正在为我的CSharp项目创建一个目录结构(在文件系统)在这个位置 C:\Mine\csharp\command 。这里我在命令文件夹内创建了两个文件夹。 来源和构建。 现在从任何文本编辑器创建一个小示例程序(我正在使用Notepad ++),如下所示,将其另存为 WinFormExample.cs 在源文件夹下:
Go to C:\Windows\Microsoft.NET\Framework\v4.0.30319 location on your File System and copy the path.
Now right click Computer go to Properties.
Under System Properties, select Advanced Tab, and click on Environment Variables.
On Environment Variables under User Variables, select New.
For Variable Name write CSHARP_HOME or something else, though I am using the same for further needs to explain this out. For Variable Value simply Paste what you copied in Step 1. Click OK.
Again perform Step 4, if path variable does not exist, else you can simply select path and then click Edit to perform this next thingy (after putting ;(semi-colon) at the end of the Variable Value and write %CSHARP_HOME%\(or use what you used in Step 5) ). This time for Variable Name write path, and for Variable Value use %CSHARP_HOME%\ and click OK.
Open cmd and type csc and press ENTER, you might be able to see something like this as an output
Now consider I am creating a directory structure for my CSharp Project like this (on File System) at this location C:\Mine\csharp\command. Here I created two folders inside command folder. source and build.
Now from any Text Editor create a small sample program (I am using Notepad++), as below, save it as WinFormExample.cs under source folder :
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CSharpGUI {
public class WinFormExample : Form {
private Button button;
public WinFormExample() {
DisplayGUI();
}
private void DisplayGUI() {
this.Name = "WinForm Example";
this.Text = "WinForm Example";
this.Size = new Size(150, 150);
this.StartPosition = FormStartPosition.CenterScreen;
button = new Button();
button.Name = "button";
button.Text = "Click Me!";
button.Size = new Size(this.Width - 50, this.Height - 100);
button.Location = new Point(
(this.Width - button.Width) / 3 ,
(this.Height - button.Height) / 3);
button.Click += new System.EventHandler(this.MyButtonClick);
this.Controls.Add(button);
}
private void MyButtonClick(object source, EventArgs e) {
MessageBox.Show("My First WinForm Application");
}
public static void Main(String[] args) {
Application.Run(new WinFormExample());
}
}
}
现在键入 csc /out:build\WinFormExample.exe source\WinFormExample.cs (more信息在最后给出,对于编译器选项),然后按 ENTER 进行编译,如下所示: 现在只需使用 .\build\WinExample 运行它,如下所示: 现在您的简单 GUI应用程序正在运行: - )
Now type csc /out:build\WinFormExample.exe source\WinFormExample.cs (more info is given at the end, for compiler options)and press ENTER to compile as shown below :
Now simply run it using .\build\WinExample, as shown below :
Now your simple GUI Application is up and running :-)
让我知道,我可以解释一下关于 Java 同样的事情,如果需要的话: - )
Do let me know, I can explain the same thingy regarding Java as well, if need be :-)
有关编译器选项可以在 C#编译器选项按字母顺序排列
发布评论