新加坡之旅

公車上的小朋友,回頭偷看我在「偷拍」

新加坡之旅

新加坡之旅

Jurong鳥園

新加坡之旅

新加坡之旅

路人

新加坡之旅

新加坡之旅

市場一角

新加坡之旅

新加坡之旅

會親人的鯉魚

新加坡之旅

新加坡之旅

地標-Merlion

新加坡之旅

[C#]撰寫一個UserControl,能限制其大小

2008-12-05 by Blave Huang | 回應(0) | 645次閱讀

原本希望能在Form裡頭放一條分隔線,畫面才會好看:

control

但找了Visual Studio的ToolBox,都找不到這樣的一個控制項(Control),一開始只能用PictureBox來實現…

但用PictureBox卻不夠嚴謹,當橫線的Height要拉大的時候,整條線變得奇醜無比。因此上網找了一些方法,將實作過程分享給大家。

1. 首先,建立一個使用者控制項(UserControl)

control2

2. 將這個UserControl設計好

3. 加入參考:System.Design

4. 在這個UserControl的程式碼加上一個Class(記得using System.Windows.Forms.Design;):

internal class HLineDesigner : ControlDesigner
    {
        public override SelectionRules SelectionRules
        {
            get
            {
                return SelectionRules.LeftSizeable | SelectionRules.RightSizeable | SelectionRules.Moveable;
            }
        }
    }

5. 將UserControl的Class改為:

[Designer(typeof(HLineDesigner))]
    [ToolboxBitmap(@"hline.png")]
    public partial class HorizontalLine : UserControl
    {
        //------------------------------------------------------------------------------
        /// <summary>
        /// 建構子
        /// </summary>
        public HorizontalLine()
        {
            InitializeComponent();
        }
      
        //------------------------------------------------------------------------------
        /// <summary>
        /// 強迫高度
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="specified"></param>
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, width, 2, specified);
        }
    }

6. hline.png是出現在ToolBox的小圖,記得把這個檔案放在Debug目錄下

7. 編譯後就能看到這個新控制項了

control3

其中的HorizontalLine及VerticalLine就是我自己建立的控制項,橫線只能左右伸展,豎線只能上下伸展。

control4

回應文章




  • :em21:
  • :em32:
  • :em28:
  • :em17:
  • :em31:
  • :em41:
  • :em63:
  • :em51:
  • :em35:
  • :em38:
  • :em71:
  • :em54:
  • :em22:
  • :em52:
  • :em16:
  • :em36:
  • :em68:
  • :em27:
  • :em47:
  • :em70:
  • :em69:
  • :em18:
  • :em64:
  • :em09:
  • :em66:
  • :em08:
  • :em48:
  • :em25:
  • :em26:
  • :em59:
  • :em65:
  • :em60:
  • :em19:
  • :em37:
  • :em58:
  • :em23:
  • :em13:
  • :em53:
  • :em45:
  • :em11:
  • :em15:
  • :em03:
  • :em61:
  • :em43:
  • :em56:
  • :em57:
  • :em10:
  • :em44:
  • :em02:
  • :em40:
  • :em67:
  • :em46:
  • :em14:
  • :em33:
  • :em07:
  • :em50:
  • :em62:
  • :em12:
  • :em01:
  • :em42:
  • :em30:
  • :em72:
  • :em39:
  • :em29:
  • :em05:
  • :em34:
  • :em20:
  • :em49:
  • :em04:
  • :em55:
  • :em06:
  • :em24:

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word