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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 布局管理 > 原文: [http://zetcode.com/tutorials/ironpythontutorial/layout/](http://zetcode.com/tutorials/ironpythontutorial/layout/) IronPython Mono Winforms 教程繼續進行控件的布局管理。 在將控件放置在其父容器上之后,我們必須確保其布局正確。 ## `Anchor` 控件的`Anchor`屬性確定如何使用其父控件調整其大小。 錨是海洋世界中的一個術語。 當船錨掉入水中時,船就固定在某個地方。 Winforms 控件也是如此。 Winforms 中的每個控件都可以具有以下`AnchorStyles`值之一: * `TOP` * `LEFT` * `RIGHT` * `BOTTOM` 注意,控件不限于一個值。 他們可以使用`|`運算符將這些值進行任意組合。 ## 基本`Anchor`示例 下面的示例顯示一個非常基本的示例,演示`Anchor`屬性。 `anchor.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form from System.Windows.Forms import Button, AnchorStyles from System.Drawing import Size, Point class IForm(Form): def __init__(self): self.Text = 'Anchor' self.Size = Size(210, 210) btn1 = Button() btn1.Text = "Button" btn1.Parent = self btn1.Location = Point(30, 30) btn2 = Button() btn2.Text = "Button" btn2.Parent = self btn2.Location = Point(30, 80) btn2.Anchor = AnchorStyles.Right self.CenterToScreen() Application.Run(IForm()) ``` 這是一個非常基本的代碼示例,清楚地顯示了`Anchor`屬性的含義。 我們在表單上有兩個按鈕。 第一個按鈕具有默認的`AnchorStyles`值,即`AnchorStyles.Top | AnchorStyles.Left`。 第二個按鈕已顯式設置`AnchorStyles.Right`。 ```py btn2.Anchor = AnchorStyles.Right ``` 我們將第二個按鈕的`Anchor`屬性明確設置為`AnchorStyles`。 正確的價值。 現在看看以下兩個圖像。 左邊的是開始時顯示的應用。 調整大小后,右側顯示相同的應用。 第一個按鈕與表單的左邊界和上邊界保持距離。 第二個按鈕與表單的右邊框保持距離。 但是它在垂直方向上沒有保持任何距離。 ![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.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form, MainMenu, StatusBar from System.Windows.Forms import Shortcut, MenuItem, TextBox, DockStyle from System.Drawing import Size, Point class IForm(Form): def __init__(self): self.Text = 'Editor' self.Size = Size(210, 180) mainMenu = MainMenu() filem = mainMenu.MenuItems.Add('&File') filem.MenuItems.Add(MenuItem('E&xit', self.OnExit, Shortcut.CtrlX)) self.Menu = mainMenu tb = TextBox() tb.Parent = self tb.Dock = DockStyle.Fill tb.Multiline = True sb = StatusBar() sb.Parent = self sb.Text = 'Ready' self.CenterToScreen() def OnExit(self, sender, event): self.Close() Application.Run(IForm()) ``` 我們顯示一個菜單欄和一個狀態欄。 其余區域由`TextBox`控件占用。 ```py tb = TextBox() tb.Parent = self ``` 在這里,我們創建`TextBox`控件。 `Form`容器被設置為文本框的父級。 ```py tb.Dock = DockStyle.Fill ``` 此代碼行使`TextBox`控件占用了表單容器內的剩余空間。 ![Editor skeleton](https://img.kancloud.cn/6b/d1/6bd12c1341442f64f64fd62f6a8147bf_250x251.jpg) 圖:編輯器骨架 ## 固定按鈕 下一個示例顯示了位于窗體右下角的兩個按鈕。 `anchoredbuttons.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") clr.AddReference("System") from System.Windows.Forms import Application, Form, Button, Panel from System.Windows.Forms import DockStyle, AnchorStyles from System.Drawing import Size, Point WIDTH = 250 HEIGHT = 150 BUTTONS_SPACE = 15 PANEL_SPACE = 8 CLOSE_SPACE = 10 class IForm(Form): def __init__(self): self.Text = 'Buttons' self.Size = Size(WIDTH, HEIGHT) ok = Button() PANEL_HEIGHT = ok.Height + PANEL_SPACE panel = Panel() panel.Height = PANEL_HEIGHT panel.Dock = DockStyle.Bottom panel.Parent = self x = ok.Width * 2 + BUTTONS_SPACE y = (PANEL_HEIGHT - ok.Height) / 2 ok.Text = "Ok" ok.Parent = panel ok.Location = Point(WIDTH-x, y) ok.Anchor = AnchorStyles.Right close = Button() x = close.Width close.Text = "Close" close.Parent = panel close.Location = Point(WIDTH-x-CLOSE_SPACE, y) close.Anchor = AnchorStyles.Right self.CenterToScreen() Application.Run(IForm()) ``` 該示例在對話框的右下角顯示“確定”,“關閉”按鈕,這在對話框窗口中很常見。 ```py WIDTH = 250 HEIGHT = 150 ``` `WIDTH`和`HEIGHT`變量確定應用窗口的寬度和高度。 ```py BUTTONS_SPACE = 15 PANEL_SPACE = 8 CLOSE_SPACE = 10 ``` `BUTTONS_SPACE`是“確定”和“關閉”按鈕之間的空間。 `PANEL_SPACE`是面板和表單底部之間的空間。 最后,`CLOSE_SPACE`變量設置“關閉”按鈕和表單右邊框之間的間隔。 ```py PANEL_HEIGHT = ok.Height + PANEL_SPACE ``` 在這里,我們計算面板的高度。 面板的高度基于“確定”按鈕的高度。 并且我們添加了一些額外的空間,以使按鈕不會太靠近邊框。 ```py panel = Panel() panel.Height = PANEL_HEIGHT panel.Dock = DockStyle.Bottom panel.Parent = self ``` 在這里,我們創建和管理`Panel`控件。 在此示例中,它用作按鈕的容器。 它被粘貼到表單的底部邊框。 然后將按鈕放置在面板內。 ```py ok.Text = "Ok" ok.Parent = panel ok.Location = Point(WIDTH-x, y) ok.Anchor = AnchorStyles.Right ``` “確定”按鈕的父級設置為面板小部件。 計算位置。 并且`Anchor`屬性設置為右側。 另一個按鈕的創建類似。 ![Anchored buttons](https://img.kancloud.cn/37/fd/37fde140f5dd3864ea242692bdf4f260_250x151.jpg) 圖:固定按鈕 ## 播放器骨架 IronPython Mono Winforms 教程這一部分的最后一個示例顯示了一個更復雜的示例。 它是音樂播放器的骨架。 `player.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form, Button, Panel from System.Windows.Forms import DockStyle, AnchorStyles, StatusBar from System.Windows.Forms import TrackBar, MainMenu, MenuItem from System.Windows.Forms import FlatStyle, TickStyle, Shortcut from System.Drawing import Size, Point, Bitmap, Color class IForm(Form): def __init__(self): self.Text = 'Player' self.Size = Size(350, 280) mainMenu = MainMenu() filem = mainMenu.MenuItems.Add("&File") playm = mainMenu.MenuItems.Add("&Play") view = mainMenu.MenuItems.Add("&View") tools = mainMenu.MenuItems.Add("&Tools") favourites = mainMenu.MenuItems.Add("&Favourites") help = mainMenu.MenuItems.Add("&Help") filem.MenuItems.Add(MenuItem("E&xit", self.OnExit, Shortcut.CtrlX)) self.Menu = mainMenu panel = Panel() panel.Parent = self panel.BackColor = Color.Black panel.Dock = DockStyle.Fill buttonPanel = Panel() buttonPanel.Parent = self buttonPanel.Height = 40 buttonPanel.Dock = DockStyle.Bottom pause = Button() pause.FlatStyle = FlatStyle.Popup pause.Parent = buttonPanel pause.Location = Point(5, 10) pause.Size = Size(25, 25) pause.Image = Bitmap("pause.png") play = Button() play.FlatStyle = FlatStyle.Popup play.Parent = buttonPanel play.Location = Point(35, 10) play.Size = Size(25, 25) play.Image = Bitmap("play.png") forward = Button() forward.FlatStyle = FlatStyle.Popup forward.Parent = buttonPanel forward.Location = Point(80, 10) forward.Size = Size(25, 25) forward.Image = Bitmap("forward.png") backward = Button() backward.FlatStyle = FlatStyle.Popup backward.Parent = buttonPanel backward.Location = Point(110, 10) backward.Size = Size(25, 25) backward.Image = Bitmap("backward.png") tb = TrackBar() tb.Parent = buttonPanel tb.TickStyle = TickStyle.None tb.Size = Size(150, 25) tb.Location = Point(200, 10) tb.Anchor = AnchorStyles.Right audio = Button() audio.FlatStyle = FlatStyle.Popup audio.Parent = buttonPanel audio.Size = Size(25, 25) audio.Image = Bitmap("audio.png") audio.Location = Point(170, 10) audio.Anchor = AnchorStyles.Right sb = StatusBar() sb.Parent = self sb.Text = "Ready" self.CenterToScreen() def OnExit(self, sender, event): self.Close() Application.Run(IForm()) ``` 這是一個更復雜的示例,它同時顯示了`Dock`和`Anchor`屬性。 ```py mainMenu = MainMenu() filem = mainMenu.MenuItems.Add("&File") ... self.Menu = mainMenu ``` 在這里,我們創建菜單欄。 ```py panel = Panel() panel.Parent = self panel.BackColor = Color.Black panel.Dock = DockStyle.Fill ``` 這是黑色的面板,占據了菜單欄,狀態欄和控制面板剩余的所有剩余空間。 ```py buttonPanel = Panel() buttonPanel.Parent = self buttonPanel.Height = 40 buttonPanel.Dock = DockStyle.Bottom ``` 這是控制面板。 它的父級是表單容器。 它被粘貼到表格的底部。 高度為 40 像素。 在此控制面板內部,我們放置了所有按鈕和軌跡儀。 ```py pause = Button() pause.FlatStyle = FlatStyle.Popup pause.Parent = buttonPanel pause.Location = Point(5, 10) pause.Size = Size(25, 25) pause.Image = Bitmap("pause.png") ``` 暫停按鈕是具有默認`Anchor`屬性值的四個按鈕之一。 該按鈕的樣式設置為平面,因為它看起來更好。 我們在按鈕上放置一個位圖。 ```py tb.Anchor = AnchorStyles.Right ... audio.Anchor = AnchorStyles.Right ``` 最后兩個控件固定在右側。 ![Player skeleton](https://img.kancloud.cn/fd/16/fd1622bfb41428c250837eed0cdeeb00_350x281.jpg) 圖:播放器骨架 IronPython 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>

                              哎呀哎呀视频在线观看