从Control类直接继承自定义控件(适合CF,因为CF没有UserControl基类)!

1.支持Click事件的Label控件

 

using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsApplication1 {
    
public enum VAlign {
        Top,Middle,Bottom
    }

    
public enum HAlign {
        Left,Middle,Right
    }

    
public class ClickableLabel:System.Windows.Forms.Control {

        
private VAlign vrtAlign=VAlign.Middle;
        
private HAlign hrtAlign=HAlign.Middle;
        
private BorderStyle bdrStyle=BorderStyle.None;

        
public ClickableLabel() {
            
        }


        
public VAlign VAlign {
            
get {
                
return this.vrtAlign;
            }

            
set {
                
this.vrtAlign=value;
                
this.Invalidate();
            }

        }


        
public HAlign HAlign {
            
get {
                
return this.hrtAlign;
            }

            
set {
                
this.hrtAlign=value;
                
this.Invalidate();
            }

        }


        
public BorderStyle BorderStyle {
            
get {
                
return this.bdrStyle;
            }

            
set {
                
this.bdrStyle=value;
                
this.Invalidate();
            }

        }


        
public override string Text {
            
get {
                
return base.Text;
            }

            
set {
                
base.Text = value;
                
this.Invalidate();
            }

        }


        
protected override void OnPaint(PaintEventArgs e) {
            
int borderSpace=0;
            Graphics gr
=e.Graphics;

            
using (Pen penDraw=new Pen(Color.Black)) {
                
switch(this.BorderStyle) {
                    
case BorderStyle.FixedSingle:
                        gr.DrawRectangle(penDraw,
0,0,this.Width-1,this.Height-1);
                        borderSpace
=2;
                        
break;
                    
case BorderStyle.Fixed3D:
                        gr.DrawRectangle(penDraw,
this.ClientRectangle);
                        borderSpace
=2;
                        
break;
                    
case BorderStyle.None:
                        borderSpace
=0;
                        
break;
                }


                SizeF sizeText
=gr.MeasureString(this.Text,this.Font);
                
float posX=0.0F;
                
float posY=0.0F;
                
switch(this.HAlign) {
                    
case HAlign.Left:
                        posX
=borderSpace;
                        
break;
                    
case HAlign.Middle:
                        posX
=(this.Width-sizeText.Width)/2;
                        
break;
                    
case HAlign.Right:
                        posX
=(this.Width-sizeText.Width)-borderSpace;
                        
break;
                }


                
switch(this.VAlign) {
                    
case VAlign.Top:
                        posY
=borderSpace;
                        
break;
                    
case VAlign.Middle:
                        posY
=(this.Height-sizeText.Height)/2;
                        
break;
                    
case VAlign.Bottom:
                        posY
=(this.Height-sizeText.Height)-borderSpace;
                        
break;
                }


                SolidBrush b
=new SolidBrush(this.ForeColor);
                gr.DrawString(
this.Text,this.Font,b,posX,posY);
                b.Dispose();
            }

            
base.OnPaint (e);
        }


    }


}


 

2.窗体测试代码:

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1 {
    
public class Form1 : System.Windows.Forms.Form {
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.RadioButton radioButton1;
        
private System.Windows.Forms.RadioButton radioButton2;
        
private System.Windows.Forms.RadioButton radioButton3;
        
private System.Windows.Forms.GroupBox groupBox2;
        
private System.Windows.Forms.RadioButton radioButton4;
        
private System.Windows.Forms.RadioButton radioButton5;
        
private System.Windows.Forms.RadioButton radioButton6;
        
private System.Windows.Forms.GroupBox groupBox3;
        
private System.Windows.Forms.RadioButton radioButton7;
        
private System.Windows.Forms.RadioButton radioButton8;
        
private System.Windows.Forms.RadioButton radioButton9;

        
private System.ComponentModel.Container components = null;
        
private ClickableLabel label=null;
        
public Form1() {

            InitializeComponent();

            label
=new ClickableLabel();
            label.ForeColor
=Color.Black;
            label.Location
=new System.Drawing.Point(15210);
            label.Size
=new Size(92,30);
            label.Text
="测试Label";
            label.Click
+=new EventHandler(label_Click);
            
this.Controls.Add(label);

        }


        
protected override void Dispose( bool disposing ) {
            
if( disposing ) {
                
if (components != null{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        [STAThread]
        
static void Main() {
            Application.Run(
new Form1());
        }


        
private void radioButton1_CheckedChanged(object sender, System.EventArgs e) {
            RadioButton r
=null;
            
if (sender==this.radioButton1) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.BorderStyle=BorderStyle.FixedSingle;
                }

            }

            
if (sender==this.radioButton2) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.BorderStyle=BorderStyle.Fixed3D;
                }

            }

            
if (sender==this.radioButton3) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.BorderStyle=BorderStyle.None;
                }

            }


            
if (sender==this.radioButton4) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.HAlign=HAlign.Right;
                }

            }

            
if (sender==this.radioButton5) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.HAlign=HAlign.Middle;
                }

            }

            
if (sender==this.radioButton6) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.HAlign=HAlign.Left;
                }

            }


            
if (sender==this.radioButton7) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.VAlign=VAlign.Bottom;
                }

            }

            
if (sender==this.radioButton8) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.VAlign=VAlign.Middle;
                }

            }

            
if (sender==this.radioButton9) {
                r
=(RadioButton)sender;
                
if (r.Checked==true{
                    
this.label.VAlign=VAlign.Top;
                }

            }

        }


        
private void label_Click(object sender, EventArgs e) {
            MessageBox.Show(
"This is a label than can be able to click!");
        }

    }

}

 

posted @ 2006-09-30 10:37  吴东雷  阅读(958)  评论(0)    收藏  举报