用户操作
[即时聊天] [发私信] [加为好友]
潘胜国ID:oceanchip
3926次访问,排名2万外,好友0人,关注者0人。
oceanchip的文章
原创 5 篇
翻译 0 篇
转载 0 篇
评论 24 篇
最近评论
oceanchip:用户名:admin
密码123456
龙:没有登陆名和密码,怎么知道里面有什么啊
按取消后出现异常,没有捕捉
文章分类
收藏
    相册
    C#
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 (Divider Panel)分割面板--从ToolBar一步步学Windows窗体控件制作收藏

    新一篇: 有一定基础的加9705074技术群 | 旧一篇: 这是我做的一个作品

    这是我从网上看到的一篇文章,我感觉比较好,因此把它放到这里来共享一下。
    这篇文章是从英文网站上看到的。我的英语水平不好,但是我想学它,所以还是把大部分翻译过来了,翻译肯定是有错的,如果不能忍受这里面的错误的话,请不要往下看下去。
    好了,现在开始进入正题。

    新建一个空的解决方案:
      根据作者的观点是在开始一个新的项目是建一个空的解决方案是通常是一个习惯。不过你不一按照他的观点去做。
      1、建空方案:File->New-> Blank Solution。然后你把方案的名称设为“Windows Forms Divider Panel ”,再单击确定,这样一个空的解决方案就建成了。
      2、你建立好方案后,在方案面板中选定刚建的那个方案,然后右击,Add->New Project(添加->新工程)弹出对话框后,选“Windows Control Library (Windows控件)”,给它取个名字,我这里是“DividerPanel”,然后回车。这样就会在工程里出现两个文件UserControl1.cs 和AssemblyInfo.cs,选定UserControl1.cs,删除它(因为是从头开始做吗?),重新建一个空的类,add->Add Class,类名为:DividerPanel。
      3、让这个类继承Panel类,代码如下:
      public class DividerPanel:System.Windows.Forms.Panel
           {
            }
    这样我们的这个类就拥有Panel类的所有属性与方法(除了它内部的私有的)。
      4、给这个类添加两个属性(Border3DStyle,BorderSize)
      我们都知道Toolbar是有这两个属性的,但是Panel类是不存在的,因此我们添加它们。Border3DStyle与BorderSize的值分别引用于System.Windows.Forms.Border3DStyle和System.Windows.Forms.BorderSize枚举。
      新建两个私有变量:border3dStyle,borderSize,这是一个比较好的方法。
    // This system of private properties with public accessors is a
    // best practice coding style.
    // Note that our private properties are in camelCasing -
    // the first letter is lower case, and additional word
    // boundaries are capitalized.

    private System.Windows.Forms.Border3DSide borderSide;
    private System.Windows.Forms.Border3DStyle border3DStyle;

    // Next we have our public accessors, Note that for public accessors
    // we use PascalCasing - the first letter is capitalized and additional
    // word boundaries are also capitalized.

    public System.Windows.Forms.Border3DSide BorderSide
    {
        get { return this.borderSide; }
        set
        {
            if( this.borderSide != value )
            {
                this.borderSide = value;
                this.Invalidate();
            }
        }
    }

    public System.Windows.Forms.Border3DStyle Border3DStyle
    {
        get { return this.border3DStyle; }
        set
        {
            if( this.border3DStyle != value )
            {
                this.border3DStyle = value;
                this.Invalidate();
            }
        }
    }

    如果你在前面已经引入了System.Windows.Forms是话,你可以这样定义
    private Border3DStyle border3dStyle;
    private BorderSize borderSize;
    但是这样写不直观。
    这里我建了两个公有属性,同时使用了set 和get关键字。表示这两个属性是可读写的,如果只有一个set那就说明它是只写的,不能读取它,好像只用我看到的比较少,只用get关键字的还是比较多的。
    在上面这段代码中还用了Invalidate()方法,它的作用是强制控件不重画。

    你也可是直接这样定义上面的这两个属性:
    public System.Windows.Forms.Border3DSytle border3dStyle;
    public System.Windows.forms.BorderSize borderSize;
    这样定义的话,你将不能对这两个属性进行操作,向上面的Invalidate()也不行。如果属性的值发生变化,我们的类是不知道的。

    我们定义的变量,应该在使用前先进行初始化,不然的话,在运行过程中有可能会出现错误,所以在程序中都应将变量进行初始化。这就用到我样的构造函数了。
    // This is the Constructor for our class. Any private variables
    // we have defined should have their initial values set here. It
    // is good practice to always initialize every variable to a specific
    // value, rather then leaving it as an inferred value. For example,
    // all bool's should be set to false rather than leaving them as an
    // inferred false, and any objects referenced that are initially null
    // should be explicitly set to null. (eg. myObject = null)

    public DividerPanel()
    {
        // Set default values for our control's properties
        this.borderSide = System.Windows.Forms.Border3DSide.All;
        this.border3DStyle = System.Windows.Forms.Border3DStyle.Etched;
    }


    5、重载方法
    我们需要重写的方法:OnPaint
     protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        // allow normal control painting to occur first
        base.OnPaint(e);

        // add our custom border
        System.Windows.Forms.ControlPaint.DrawBorder3D (
            e.Graphics,
            this.ClientRectangle,
            this.border3DStyle,
            this.borderSide );
    }


    今天就写到这了,如果你想点知道的话,
    http://www.codeproject.com/cs/miscctrl/DividerPanel.asp上看去,那里面比我写的详细的多。我觉得这篇文章对我来讲堪称经典。

    发表于 @ 2004年10月19日 11:06:00|评论(loading...)|编辑

    新一篇: 有一定基础的加9705074技术群 | 旧一篇: 这是我做的一个作品

    评论:没有评论。

    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © oceanchip