<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Mono Winforms 中的布局管理 > 原文: [http://zetcode.com/gui/csharpwinforms/layout/](http://zetcode.com/gui/csharpwinforms/layout/) Mono Winforms 教程繼續進行控件的布局管理。 在將控件放置在其父容器上之后,我們必須確保其布局正確。 ## `Anchor` 控件的`Anchor`屬性確定如何使用其父控件調整其大小。 錨是海洋世界中的一個術語。 當船錨掉入水中時,船就固定在某個地方。 Winforms 控件也是如此。 Winforms 中的每個控件都可以具有以下`AnchorStyles`值之一: * `Top` * `Left` * `Right` * `Bottom` 注意,控件不限于一個值。 他們可以使用|組合這些值。 運算符。 ## `Anchor`基本示例 下面的示例顯示一個非常基本的示例,演示`Anchor`屬性。 `anchor.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Anchor"; Size = new Size(210, 210); Button btn1 = new Button(); btn1.Text = "Button"; btn1.Parent = this; btn1.Location = new Point(30, 30); Button btn2 = new Button(); btn2.Text = "Button"; btn2.Parent = this; btn2.Location = new Point(30, 80); btn2.Anchor = AnchorStyles.Right; CenterToScreen(); } } class MApplication { public static void Main() { MForm mf = new MForm(); Application.Run(mf); } } ``` 這是一個非常基本的代碼示例,清楚地顯示了`Anchor`屬性的含義。 我們在表單上有兩個按鈕。 第一個按鈕具有默認的`AnchorStyles`值,即`AnchorStyles.Left`。 第二個按鈕明確設置了`AnchorStyles.Right`。 ```cs btn2.Anchor = AnchorStyles.Right; ``` 我們將第二個按鈕的`Anchor`屬性明確設置為`AnchorStyles.Right`值。 現在看看以下兩個圖像。 左邊的是開始時顯示的應用。 調整大小后,右側顯示相同的應用。 第一個按鈕與表單的左邊界和上邊界保持距離。 第二個按鈕與表單的右邊框保持距離。 但是它在垂直方向上沒有保持任何距離。 ![Before resizing](https://img.kancloud.cn/70/78/707804f943e85bd3747695c9adf189c8_210x211.jpg) ![After resizing](https://img.kancloud.cn/b6/03/b603707ba85de576f76c70bf0344c262_415x104.jpg) 圖:調整大小前后 ## `Dock` `Dock`屬性允許我們將控件粘貼到父窗體或控件的特定邊緣。 以下是可能的`DockStyle`值。 * `Top` * `Left` * `Right` * `Bottom` * `Fill` * `None` ## 編輯器骨架 以下代碼示例演示了正在使用的`Dock`屬性。 `editor.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Editor"; Size = new Size(210, 180); MainMenu mainMenu = new MainMenu(); MenuItem file = mainMenu.MenuItems.Add("&File"); file.MenuItems.Add(new MenuItem("E&xit", new EventHandler(this.OnExit), Shortcut.CtrlX)); Menu = mainMenu; TextBox tb = new TextBox(); tb.Parent = this; tb.Dock = DockStyle.Fill; tb.Multiline = true; StatusBar sb = new StatusBar(); sb.Parent = this; sb.Text = "Ready"; CenterToScreen(); } void OnExit(object sender, EventArgs e) { Close(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們顯示一個菜單欄和一個狀態欄。 其余區域由`TextBox`控件占用。 ```cs TextBox tb = new TextBox(); tb.Parent = this; ``` 在這里,我們創建`TextBox`控件。 `Form`容器被設置為文本框的父級。 ```cs tb.Dock = DockStyle.Fill; ``` 此代碼行使`TextBox`控件占用了表單容器內的剩余空間。 ![Editor skeleton](https://img.kancloud.cn/6b/d1/6bd12c1341442f64f64fd62f6a8147bf_250x251.jpg) 圖:編輯器骨架 ## 固定按鈕 下一個示例顯示了位于窗體右下角的兩個按鈕。 `anchoredbuttons.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private int WIDTH = 250; private int HEIGHT = 150; private int BUTTONS_SPACE = 15; private int PANEL_SPACE = 8; private int CLOSE_SPACE = 10; public MForm() { Text = "Anchor"; Size = new Size(WIDTH, HEIGHT); Button ok = new Button(); int PANEL_HEIGHT = ok.Height + PANEL_SPACE; Panel panel = new Panel(); panel.Height = PANEL_HEIGHT; panel.Dock = DockStyle.Bottom; panel.Parent = this; int x = ok.Width * 2 + BUTTONS_SPACE; int y = (PANEL_HEIGHT - ok.Height) / 2; ok.Text = "Ok"; ok.Parent = panel; ok.Location = new Point(WIDTH-x, y); ok.Anchor = AnchorStyles.Right; Button close = new Button(); x = close.Width; close.Text = "Close"; close.Parent = panel; close.Location = new Point(WIDTH-x-CLOSE_SPACE, y); close.Anchor = AnchorStyles.Right; CenterToScreen(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 該示例在對話框的右下角顯示確定,關閉按鈕,這在對話框窗口中很常見。 ```cs private int WIDTH = 250; private int HEIGHT = 150; ``` `WIDTH`和`HEIGHT`變量確定應用窗口的寬度和高度。 ```cs private int BUTTONS_SPACE = 15; private int PANEL_SPACE = 8; private int CLOSE_SPACE = 10; ``` `BUTTONS_SPACE`是“確定”和“關閉”按鈕之間的空間。 `PANEL_SPACE`是面板和表單底部之間的空間。 最后,`CLOSE_SPACE`變量設置“關閉”按鈕和表單右邊框之間的間隔。 ```cs int PANEL_HEIGHT = ok.Height + PANEL_SPACE; ``` 在這里,我們計算面板的高度。 面板的高度基于“確定”按鈕的高度。 并且我們添加了一些額外的空間,以使按鈕不會太靠近邊框。 ```cs Panel panel = new Panel(); panel.Height = PANEL_HEIGHT; panel.Dock = DockStyle.Bottom; panel.Parent = this ``` 在這里,我們創建和管理`Panel`控件。 在此示例中,它用作按鈕的容器。 它被粘貼到表單的底部邊框。 然后將按鈕放置在面板內。 ```cs ok.Text = "Ok"; ok.Parent = panel; ok.Location = new Point(WIDTH-x, y); ok.Anchor = AnchorStyles.Right; ``` “確定”按鈕的父級設置為面板小部件。 計算位置。 并且`Anchor`屬性設置為右側。 另一個按鈕的創建類似。 ![Anchored buttons](https://img.kancloud.cn/37/fd/37fde140f5dd3864ea242692bdf4f260_250x151.jpg) 圖:固定按鈕 ## 播放器骨架 Mono Winforms 教程這一部分的最后一個示例顯示了一個更復雜的示例。 它是音樂播放器的骨架。 `player.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Player"; Size = new Size(350, 280); MainMenu mainMenu = new MainMenu(); MenuItem file = mainMenu.MenuItems.Add("&File"); MenuItem playm = mainMenu.MenuItems.Add("&Play"); MenuItem view = mainMenu.MenuItems.Add("&View"); MenuItem tools = mainMenu.MenuItems.Add("&Tools"); MenuItem favourites = mainMenu.MenuItems.Add("&Favourites"); MenuItem help = mainMenu.MenuItems.Add("&Help"); file.MenuItems.Add(new MenuItem("E&xit", new EventHandler(this.OnExit), Shortcut.CtrlX)); Menu = mainMenu; Panel panel = new Panel(); panel.Parent = this; panel.BackColor = Color.Black; panel.Dock = DockStyle.Fill; Panel buttonPanel = new Panel(); buttonPanel.Parent = this; buttonPanel.Height = 40; buttonPanel.Dock = DockStyle.Bottom; Button pause = new Button(); pause.FlatStyle = FlatStyle.Popup; pause.Parent = buttonPanel; pause.Location = new Point(5, 10); pause.Size = new Size(25, 25); pause.Image = new Bitmap("pause.png"); Button play = new Button(); play.FlatStyle = FlatStyle.Popup; play.Parent = buttonPanel; play.Location = new Point(35, 10); play.Size = new Size(25, 25); play.Image = new Bitmap("play.png"); Button forward = new Button(); forward.FlatStyle = FlatStyle.Popup; forward.Parent = buttonPanel; forward.Location = new Point(80, 10); forward.Size = new Size(25, 25); forward.Image = new Bitmap("forward.png"); Button backward = new Button(); backward.FlatStyle = FlatStyle.Popup; backward.Parent = buttonPanel; backward.Location = new Point(110, 10); backward.Size = new Size(25, 25); backward.Image = new Bitmap("backward.png"); TrackBar tb = new TrackBar(); tb.Parent = buttonPanel; tb.TickStyle = TickStyle.None; tb.Size = new Size(150, 25); tb.Location = new Point(200, 10); tb.Anchor = AnchorStyles.Right; Button audio = new Button(); audio.FlatStyle = FlatStyle.Popup; audio.Parent = buttonPanel; audio.Size = new Size(25, 25); audio.Image = new Bitmap("audio.png"); audio.Location = new Point(170, 10); audio.Anchor = AnchorStyles.Right; StatusBar sb = new StatusBar(); sb.Parent = this; sb.Text = "Ready"; CenterToScreen(); } void OnExit(object sender, EventArgs e) { Close(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 這是一個更復雜的示例,它同時顯示了`Dock`和`Anchor`屬性。 ```cs MainMenu mainMenu = new MainMenu(); MenuItem file = mainMenu.MenuItems.Add("&File"); ... Menu = mainMenu; ``` 在這里,我們創建菜單欄。 ```cs Panel panel = new Panel(); panel.Parent = this; panel.BackColor = Color.Black; panel.Dock = DockStyle.Fill; ``` 這是黑色的面板,占據了菜單欄,狀態欄和控制面板剩余的所有剩余空間。 ```cs Panel buttonPanel = new Panel(); buttonPanel.Parent = this; buttonPanel.Height = 40; buttonPanel.Dock = DockStyle.Bottom; ``` 這是控制面板。 它的父級是表單容器。 它被粘貼到表格的底部。 高度為 40 像素。 在此控制面板內部,我們放置了所有按鈕和軌跡儀。 ```cs Button pause = new Button(); pause.FlatStyle = FlatStyle.Popup; pause.Parent = buttonPanel; pause.Location = new Point(5, 10); pause.Size = new Size(25, 25); pause.Image = new Bitmap("pause.png"); ``` 暫停按鈕是具有默認`Anchor`屬性值的四個按鈕之一。 該按鈕的樣式設置為平面,因為它看起來更好。 我們在按鈕上放置一個位圖。 ```cs tb.Anchor = AnchorStyles.Right; ... audio.Anchor = AnchorStyles.Right; ``` 最后兩個控件固定在右側。 ![Player skeleton](https://img.kancloud.cn/fd/16/fd1622bfb41428c250837eed0cdeeb00_350x281.jpg) 圖:播放器骨架 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>

                              哎呀哎呀视频在线观看