<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Mono Winforms 中的第一步 > 原文: [http://zetcode.com/gui/csharpwinforms/firststeps/](http://zetcode.com/gui/csharpwinforms/firststeps/) 在 Mono Winforms 教程的這一部分中,我們介紹 Winforms 編程庫中的一些基本程序。 ## 簡單 這是一個簡單的 Winforms 應用。 `simple.cs` ```cs using System.Windows.Forms; using System.Drawing; public class Simple : Form { public Simple() { Text = "Simple"; Size = new Size(250, 200); CenterToScreen(); } static public void Main() { Application.Run(new Simple()); } } ``` 此代碼示例在屏幕上顯示一個小窗口。 ```cs using System.Windows.Forms; using System.Drawing; ``` 在這里,我們使用`using`指令,該指令允許我們使用適當名稱空間中的類型,而無需使用完全限定的名稱。 例如,我們現在可以編寫`Form`而不是`System.Windows.Forms.Form`。 ```cs public class Simple : Form { ... } ``` 在 Winforms 中,任何窗口或對話框都是`Form`。 該控件是一個基本容器,其目的是顯示其他子控件。 我們的類`Simple`繼承自表單。 這樣,它本身就成為一種形式。 ```cs Text = "Simple"; Size = new Size(250, 200); ``` `Text`和`Size`是表單的屬性。 更改這些屬性,我們將修改表單控件。 第一行在表單控件的標題欄中顯示文本`"Simple"`。 第二行將表單的大小設置為`250x200px`。 ```cs CenterToScreen(); ``` 這種方法將我們的應用集中在屏幕上。 ```cs static public void Main() { Application.Run(new Simple()); } ``` 編譯并運行后,將首先執行`Main`方法。 該代碼實例化`Simple`類并運行它。 ```cs $ gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll simple.cs ``` 這是我們編譯源代碼的方式。 如果沒有犯任何錯誤,則應在當前工作目錄中包含`simple.exe`文件。 ![Simple](https://img.kancloud.cn/be/ef/beefe1a55e782a93c548f5f377cb822e_250x201.jpg) 圖:簡單 ## 圖標 Mono 在西班牙語中意為猴子。 如果我們不為應用提供圖標,則默認情況下,我們的頭是猴子。 下一個示例顯示如何更改此設置。 `icon.cs` ```cs using System.Windows.Forms; using System.Drawing; using System; public class MForm : Form { public MForm() { Text = "Icon"; Size = new Size(250, 200); try { Icon = new Icon("web.ico"); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(1); } CenterToScreen(); } static public void Main() { Application.Run(new MForm()); } } ``` 該代碼示例在窗體的左上角顯示一個圖標。 表單的圖標是代表任務欄中表單的圖片以及為表單的控制框顯示的圖標。 ```cs try { Icon = new Icon("web.ico"); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(1); } ``` 最好將所有輸入輸出工作放在`try/catch`關鍵字之間。 `web.ico`文件必須在當前工作目錄中可用。 這是我們執行(`./icon.exe`)應用的目錄。 ![Icon](https://img.kancloud.cn/78/e9/78e914ffb8f9fc04b8a80d4bd7b0a4f1_250x201.jpg) 圖:圖標 ## 工具提示 工具提示是一個小的矩形彈出窗口,當用戶將指針放在控件上時,它會顯示控件目的的簡短說明。 `tooltips.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Tooltips"; Size = new Size(250, 200); ToolTip btnTlp = new ToolTip(); btnTlp.SetToolTip(this, "This is a Form"); Button button = new Button(); btnTlp.SetToolTip(button, "This is a Button Control"); button.Text = "Button"; button.Location = new Point(30, 70); button.Parent = this; CenterToScreen(); } } class MApplication { static void Main() { Application.Run(new MForm()); } } ``` 我們的代碼示例為兩個控件創建一個工具提示。 `Button`控件和`Form`控件。 ```cs ToolTip btnTlp = new ToolTip(); ``` 在這里,我們創建`ToolTip`控件。 此實例用于為兩個控件提供工具提示。 ```cs btnTlp.SetToolTip(this, "This is a Form"); ``` 在這里,我們為表單設置工具提示。 ```cs btnTlp.SetToolTip(button, "This is a Button Control"); ``` 這里是我們的按鈕。 ```cs Button button = new Button(); btnTlp.SetToolTip(button, "This is a Button Control"); button.Text = "Button"; button.Location = new Point(30, 70); button.Parent = this; ``` 注意`Button`控件的創建。 `Text`屬性是按鈕的標簽。 `Location`屬性將按鈕放在`x = 30`,`y = 70px`坐標的表單上。 最后,`Parent`屬性確定按鈕所在的容器。 ![Tooltips](https://img.kancloud.cn/82/c2/82c266959192927628a54c0d43e61994_250x201.jpg) 圖:工具提示 s ## 按鈕 我們的最后一個代碼示例顯示了一個有效的按鈕控件。 `button.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Button"; Size = new Size(250, 200); Button button = new Button(); button.Location = new Point(30, 20); button.Text = "Quit"; button.Click += new EventHandler(OnClick); button.MouseEnter += new EventHandler(OnEnter); Controls.Add(button); CenterToScreen(); } void OnClick(object sender, EventArgs e) { Close(); } void OnEnter(object sender, EventArgs e) { Console.WriteLine("Button Entered"); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 所有 GUI 編程都是事件驅動的編程。 在我們的示例中,我們在表單容器上顯示了一個按鈕控件。 該按鈕將收聽兩個事件。 `Click`和`MouseEnter`事件。 ```cs button.Click += new EventHandler(OnClick); ``` 此代碼行將事件處理器插入`Click`事件。 當我們單擊按鈕時,將調用`OnClick()`方法。 ```cs button.MouseEnter += new EventHandler(OnEnter); ``` 當我們使用鼠標指針進入按鈕區域時,將觸發`MouseEnter`事件。 在這種情況下,我們的代碼將調用`OnEnter()`方法。 ```cs void OnClick(object sender, EventArgs e) { Close(); } ``` 該方法關閉應用。 ```cs void OnEnter(object sender, EventArgs e) { Console.WriteLine("Button Entered"); } ``` 當我們使用鼠標指針進入按鈕控制區域時,終端中將顯示`"Button Entered"`文本。 Mono Winforms 教程的這一部分顯示了一些入門代碼示例,以幫助您開始使用 Winforms 編程庫。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看