當前位置:
首頁 >
Anchor属性
發布時間:2024/9/5
47
豆豆
Control.Anchor 屬性 :獲取或設置控件綁定到的容器的邊緣并確定控件如何隨其父級一起調整大小。
AnchorStyles 值的按位組合。默認值是 Top 和 Left。
使用 Anchor 屬性可以定義在調整控件的父控件大小時如何自動調整控件的大小。將控件錨定到其父控件后,可確保當調整父控件的大小時錨定的邊緣與父控件的邊緣的相對位置保持不變。
一個控件可以錨定到其容器的一個或多個邊緣。例如,如果有一個帶有 Button 的 Form,而該按鈕的 Anchor 屬性值設置為 Top 和 Bottom,當 Form 的 Height 增加時,Button 伸展,以保持到 Form 的上邊緣和下邊緣的錨定距離不變。
下面的代碼示例將 Button 添加到窗體并設置該控件的一些通用屬性。示例將該按鈕定位在窗體的右下角,因此調整窗體大小時其相對位置不變。接著,它設置 BackgroundImage 并將該按鈕的大小調整為與 同樣的大小。然后,該示例將 TabStop 設置為 true,并設置 TabIndex 屬性。最后,它添加一個事件處理程序以處理按鈕的 Click 事件。此示例要求您有一個名為 imageList1 的 ImageList。
// Add a button to a form and set some of its common properties. private void AddMyButton() {// Create a button and add it to the form.Button button1 = new Button();// Anchor the button to the bottom right corner of the formbutton1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);// Assign a background image.button1.BackgroundImage = imageList1.Images[0];// Specify the layout style of the background image. Tile is the default.button1.BackgroundImageLayout = ImageLayout.Center;// Make the button the same size as the image.button1.Size = button1.BackgroundImage.Size;// Set the button's TabIndex and TabStop properties.button1.TabIndex = 1;button1.TabStop = true;// Add a delegate to handle the Click event.button1.Click += new System.EventHandler(this.button1_Click);// Add the button to the form.this.Controls.Add(button1); }轉載于:https://www.cnblogs.com/irving314/archive/2011/06/23/anchorstyle.html
總結
- 上一篇: 这是最好的时代,也是最坏的时代
- 下一篇: 50佳设计独特的名片设计欣赏(上篇)