簡單控件制作實例

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

namespace Control1
{
 /// <summary>
 /// Flip Mode for displaying images.
 /// </summary>
 public enum FlipModeStyle
 {
  NoFlip = 0,
  FlipX = 1,
  FlipY = 2,
  FlipXY=3
 }
   
 ///<summary>
 ///Event data class事件數據類
 ///</summary>
 public class DisplaySizeChangedEventArgs : EventArgs
 {
    public int width;
    public int heigth;
  public DisplaySizeChangedEventArgs()
  {
  }
 }
 ///<summary>
 ///Event Delegate for DisplaySizeChanged Event
 ///</summary>
public delegate void DisplaySizeChangedEventHandler(object sender,DisplaySizeChangedEventArgs e);


 /// <summary>
 /// ImageZoomerControl 的摘要描述。
 /// </summary>
 public class ImageZoomerControl : System.Windows.Forms.Control
 {
  /// <summary>
  /// 設計工具所需的變數。
  /// </summary>
  private System.ComponentModel.Container components = null;
  private int width;
  private int height;
  private System.Drawing.Bitmap bitmap;
        private FlipModeStyle flip;     
  private DisplaySizeChangedEventHandler eventHandler;
 
 

  public ImageZoomerControl()
  {
   // 此為 Windows.Forms 表單設計工具所需的呼叫。
   InitializeComponent();

   // TODO: 在 InitComponent 呼叫之後加入任何初始設定
   width = this.width;
   height = this.height;
   bitmap = null;
            flip = FlipModeStyle.NoFlip;
   eventHandler = null;
  }

  /// <summary>
  /// 清除任何使用中的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if( components != null )
     components.Dispose();
   }
   base.Dispose( disposing );
  }

  #region 元件設計工具產生的程式碼
  /// <summary>
  /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
  /// 這個方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   components = new System.ComponentModel.Container();
  }
  #endregion
  /// <summary>
  /// 定義屬性
  /// </summary>
  [
  Category("Control1"),
  Description("The displayed image width.")
  ]
 
  public int DisplayWidth
  {
   get
   {
     return width;
   }
   set
   {
    if(value >= 0)
    {
      width = value;
     //刷新窗口
     Invalidate(this.ClientRectangle);
    }
   }
  }

  [Category("Control1"),Description("The displayed image height.")]
  public int DisplayHeigth
  {
   get
   {
     return height;
   }
   set
   {
    if(value >= 0 )
    {
       height = value;
     //刷新窗口
     Invalidate(this.ClientRectangle);
    }
   }
  }

  [Category("Control1"),Description("The image displayed by this control"),DefaultValue(null)]
  public Bitmap DisplayImage
  {
   get
   {
      return bitmap;
   }
   set
   {
     bitmap = value;
    if(bitmap != null)
    {
     width = bitmap.Width;
     height = bitmap.Height;

    }
    else
    {
      width = this.width;
      height = this.height;
    }
    //刷新窗口
    Invalidate(this.ClientRectangle);
   }
  }

  [Category("Control1"),Description("Specify how the image will be fliped")]
  public FlipModeStyle FlipMode
  {
   get
   {
      return flip;
   }
   set
   {
      flip = value;
    //刷新窗口
    Invalidate(this.ClientRectangle);
   }
  }

  [Category("Control1"),Description("Occurs when the image size is changed.")]
  public event DisplaySizeChangedEventHandler DisplaySizeChanged
  {
   add
   {
     eventHandler += value;
   }
   remove
   {
     eventHandler -=value;
   }
  }

  protected virtual void OnDisplaySizeChanged(DisplaySizeChangedEventArgs e)
  {
   if(eventHandler != null)
   {
      Invoke(eventHandler,new object[]
       {
           this,e
       }
       );

   }
  }

  protected override void OnMouseDown(MouseEventArgs e)
  {
     //更新顯示尺寸
   width = e.X;
   height = e.Y;
   //更新屏幕顯示
   Invalidate(this.ClientRectangle);

   //構造事件數據對象
   DisplaySizeChangedEventArgs eventData = new DisplaySizeChangedEventArgs();
   //設置其中的成員
   eventData.width = width;
   eventData.heigth = height;
   //發出事件
   OnDisplaySizeChanged(eventData);
  }

  protected override void OnPaint(PaintEventArgs pe)
  {
   // TODO: 在此加入自訂繪圖程式碼
   if(bitmap == null)
   {
      return;
   }
   Graphics g = pe.Graphics;
   //轉換矩陣
   System.Drawing.Drawing2D.Matrix transform;
   if(flip.Equals(FlipModeStyle.FlipX))
   {
    //水平翻轉
    transform = new System.Drawing.Drawing2D.Matrix(-1,0,0,1,width,0);
   }
   else if(flip.Equals(FlipModeStyle.FlipY))
   {
    //垂直翻轉
    transform= new System.Drawing.Drawing2D.Matrix(1,0,0,-1,0,height);

   }
   else if(flip.Equals(FlipModeStyle.FlipXY))
   {
    //水平垂直翻轉
    transform = new System.Drawing.Drawing2D.Matrix(-1,0,0,-1,width,height);
   }
   else
   {
       //不翻轉
    transform = new System.Drawing.Drawing2D.Matrix (1,0,0,1,0,0);
   }

   //設置轉換矩陣
   g.Transform = transform;
   //繪制圖形
   g.DrawImage(bitmap,new Rectangle(0,0,width,height),0,0,bitmap.Width,bitmap.Height,GraphicsUnit.Pixel);

   //恢復繪圖平面
   g.ResetTransform();
   // 呼叫基底類別的 OnPaint
   base.OnPaint(pe);
  }
 }
}

The code of imageZoomerClient

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

namespace ImageZoomerClient
{
 /// <summary>
 /// Form1 的摘要描述。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 設計工具所需的變數。
  /// </summary>
  private System.ComponentModel.Container components = null;
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem menuItem1;
  private System.Windows.Forms.MenuItem menuItem2;
  private System.Windows.Forms.MenuItem menuItem3;
  private System.Windows.Forms.MenuItem menuItem4;
  private System.Windows.Forms.MenuItem menuItem5;
  private System.Windows.Forms.StatusBar statusBar1;
  private System.Windows.Forms.StatusBarPanel statusBarPanel1;
  private System.Windows.Forms.StatusBarPanel statusBarPanel2;
  private Control1.ImageZoomerControl imageZoomer;

  public Form1()
  {
   //
   // Windows Form 設計工具支援的必要項
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
   //
  }

  /// <summary>
  /// 清除任何使用中的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form 設計工具產生的程式碼
  /// <summary>
  /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
  /// 這個方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.imageZoomer = new Control1.ImageZoomerControl();
   this.mainMenu1 = new System.Windows.Forms.MainMenu();
   this.menuItem1 = new System.Windows.Forms.MenuItem();
   this.menuItem2 = new System.Windows.Forms.MenuItem();
   this.menuItem3 = new System.Windows.Forms.MenuItem();
   this.menuItem4 = new System.Windows.Forms.MenuItem();
   this.menuItem5 = new System.Windows.Forms.MenuItem();
   this.statusBar1 = new System.Windows.Forms.StatusBar();
   this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
   this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
   this.SuspendLayout();
   //
   // imageZoomer
   //
   this.imageZoomer.DisplayHeigth = 480;
   this.imageZoomer.DisplayImage = ((System.Drawing.Bitmap)(resources.GetObject("imageZoomer.DisplayImage")));
   this.imageZoomer.DisplayWidth = 640;
   this.imageZoomer.FlipMode = Control1.FlipModeStyle.NoFlip;
   this.imageZoomer.Location = new System.Drawing.Point(0, 0);
   this.imageZoomer.Name = "imageZoomer";
   this.imageZoomer.Size = new System.Drawing.Size(200, 200);
   this.imageZoomer.TabIndex = 0;
   this.imageZoomer.DisplaySizeChanged += new Control1.DisplaySizeChangedEventHandler(this.imageZoomer_DisplaySizeChanged);
   //
   // mainMenu1
   //
   this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem1});
   //
   // menuItem1
   //
   this.menuItem1.Index = 0;
   this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem2,
                       this.menuItem3,
                       this.menuItem4,
                       this.menuItem5});
   this.menuItem1.Text = "翻轉方式(&F)";
   //
   // menuItem2
   //
   this.menuItem2.Checked = true;
   this.menuItem2.Index = 0;
   this.menuItem2.RadioCheck = true;
   this.menuItem2.Text = "不翻轉(&N)";
   //
   // menuItem3
   //
   this.menuItem3.Index = 1;
   this.menuItem3.RadioCheck = true;
   this.menuItem3.Text = "水平翻轉(&H)";
   this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
   //
   // menuItem4
   //
   this.menuItem4.Index = 2;
   this.menuItem4.RadioCheck = true;
   this.menuItem4.Text = "垂直翻轉(&V)";
   this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
   //
   // menuItem5
   //
   this.menuItem5.Index = 3;
   this.menuItem5.RadioCheck = true;
   this.menuItem5.Text = "180旋轉(&R)";
   this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
   //
   // statusBar1
   //
   this.statusBar1.Location = new System.Drawing.Point(0, 251);
   this.statusBar1.Name = "statusBar1";
   this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                        this.statusBarPanel1,
                        this.statusBarPanel2});
   this.statusBar1.ShowPanels = true;
   this.statusBar1.Size = new System.Drawing.Size(292, 22);
   this.statusBar1.TabIndex = 1;
   this.statusBar1.Text = "statusBar1";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.statusBar1);
   this.Controls.Add(this.imageZoomer);
   this.Menu = this.mainMenu1;
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程式的主進入點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
  
  }

  private void menuItem3_Click(object sender, System.EventArgs e)
  {
   imageZoomer.FlipMode = FlipModeStyle.FlipX;
   this.menuItem2.Checked = true;
   this.menuItem3.Checked = false;
   this.menuItem4.Checked = false;
   this.menuItem5.Checked = false;
  }

  private void menuItem4_Click(object sender, System.EventArgs e)
  {
   imageZoomer.FlipMode = FlipModeStyle.FlipY;
   this.menuItem2.Checked = false;
   this.menuItem3.Checked = false;
   this.menuItem4.Checked = true;
   this.menuItem5.Checked = false;
  }

  private void menuItem5_Click(object sender, System.EventArgs e)
  {
   imageZoomer.FlipMode = FlipModeStyle.FlipXY;
   this.menuItem2.Checked = false;
   this.menuItem3.Checked = false;
   this.menuItem4.Checked = false;
   this.menuItem5.Checked = true;
  }

  private void imageZoomer_DisplaySizeChanged(object sender, Control1.DisplaySizeChangedEventArgs e)
  {
     this.statusBarPanel1.Text = "圖象寬度"+e.width;
   this.statusBarPanel2.Text = "圖象高度"+e.heigth;
         
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值