Anchor属性
Control.Anchor 屬性 :獲取或設(shè)置控件綁定到的容器的邊緣并確定控件如何隨其父級(jí)一起調(diào)整大小。
AnchorStyles 值的按位組合。默認(rèn)值是 Top 和 Left。
使用 Anchor 屬性可以定義在調(diào)整控件的父控件大小時(shí)如何自動(dòng)調(diào)整控件的大小。將控件錨定到其父控件后,可確保當(dāng)調(diào)整父控件的大小時(shí)錨定的邊緣與父控件的邊緣的相對(duì)位置保持不變。
一個(gè)控件可以錨定到其容器的一個(gè)或多個(gè)邊緣。例如,如果有一個(gè)帶有 Button 的 Form,而該按鈕的 Anchor 屬性值設(shè)置為 Top 和 Bottom,當(dāng) Form 的 Height 增加時(shí),Button 伸展,以保持到 Form 的上邊緣和下邊緣的錨定距離不變。
下面的代碼示例將 Button 添加到窗體并設(shè)置該控件的一些通用屬性。示例將該按鈕定位在窗體的右下角,因此調(diào)整窗體大小時(shí)其相對(duì)位置不變。接著,它設(shè)置 BackgroundImage 并將該按鈕的大小調(diào)整為與 同樣的大小。然后,該示例將 TabStop 設(shè)置為 true,并設(shè)置 TabIndex 屬性。最后,它添加一個(gè)事件處理程序以處理按鈕的 Click 事件。此示例要求您有一個(gè)名為 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); }轉(zhuǎn)載于:https://www.cnblogs.com/irving314/archive/2011/06/23/anchorstyle.html
總結(jié)
- 上一篇: 这是最好的时代,也是最坏的时代
- 下一篇: 50佳设计独特的名片设计欣赏(上篇)