摘要:測試貼程式碼的效果[2]以文找文
同樣是已發表在 MSN sharedllee 的文章, 這次使用進階編輯器, 不自動斷行, 程式使用 C2Html 轉換, 以 html 原始碼貼入。
/*
* Created by 記事本
* User: Lee, Dong-Liang <dllee@edirect168.com>
* Date: 2005/06/25
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace ItemOfComboBox
{
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox cBoxHatchStyle;
private System.Windows.Forms.Button SelectColor;
private System.Windows.Forms.Button SelectBKColor;
public MainForm()
{
// cBoxHatchStyle
this.cBoxHatchStyle = new System.Windows.Forms.ComboBox();
this.cBoxHatchStyle.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cBoxHatchStyle.Location = new System.Drawing.Point(20, 10);
this.cBoxHatchStyle.Name = "cBoxHatchStyle";
this.cBoxHatchStyle.Size = new System.Drawing.Size(200, 20);
this.cBoxHatchStyle.TabIndex = 16;
this.cBoxHatchStyle.SelectedIndexChanged += new System.EventHandler(this.CBoxHatchStyleSelectedIndexChanged);
// SelectColor
this.SelectColor = new System.Windows.Forms.Button();
this.SelectColor.BackColor = System.Drawing.Color.Blue;
this.SelectColor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.SelectColor.Location = new System.Drawing.Point(250, 10);
this.SelectColor.Name = "SelectColor";
this.SelectColor.TabIndex = 8;
this.SelectColor.Text = "Color1";
this.SelectColor.Click += new System.EventHandler(this.SelectColorClick);
// SelectBKColor
this.SelectBKColor = new System.Windows.Forms.Button();
this.SelectBKColor.BackColor = System.Drawing.Color.Yellow;
this.SelectBKColor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.SelectBKColor.Location = new System.Drawing.Point(350, 10);
this.SelectBKColor.Name = "SelectBKColor";
this.SelectBKColor.TabIndex = 15;
this.SelectBKColor.Text = "Color2";
this.SelectBKColor.Click += new System.EventHandler(this.SelectBKColorClick);
// MainForm
this.ClientSize = new System.Drawing.Size(600, 400);
this.Controls.Add(this.cBoxHatchStyle);
this.Controls.Add(this.SelectColor);
this.Controls.Add(this.SelectBKColor);
this.Name = "MainForm";
this.Text = "ItemOfComboBox : HatchSytle Example by http://dllee.ktop.com.tw";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form_Paint);
this.ActiveControl = this.cBoxHatchStyle;
// C# 的 comboBox 可以直接加入物件,它似乎?#124;自己取 .ToString() 的方法
// 得到字串作列表,而加入的物件之後可以直接取出使用。
// <2005-06-25> 使用列?#124;的方式取得列表。
Array hsList = Enum.GetValues( typeof( HatchStyle ) );
foreach ( HatchStyle hs in hsList )
this.cBoxHatchStyle.Items.Add(hs);
this.cBoxHatchStyle.SelectedIndex=1;
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
void SelectColorClick(object sender, System.EventArgs e)
{
ColorDialog clrDlg = new ColorDialog();
clrDlg.Color = this.SelectColor.BackColor;
if (clrDlg.ShowDialog() == DialogResult.OK)
{
this.SelectColor.BackColor = clrDlg.Color;
this.Invalidate();
}
clrDlg.Dispose();
}
void SelectBKColorClick(object sender, System.EventArgs e)
{
ColorDialog clrDlg = new ColorDialog();
clrDlg.Color = this.SelectBKColor.BackColor;
if (clrDlg.ShowDialog() == DialogResult.OK)
{
this.SelectBKColor.BackColor = clrDlg.Color;
this.Invalidate();
}
clrDlg.Dispose();
}
void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
// 物件轉換成其他類別
HatchStyle selectedHatchStyle = (HatchStyle)Convert.ChangeType(
this.cBoxHatchStyle.SelectedItem,typeof(HatchStyle));
// Create a hatch brush with selected hatch style and colors
HatchBrush brush = new HatchBrush(selectedHatchStyle,
this.SelectColor.BackColor, this.SelectBKColor.BackColor);
// Fill rectangle
g.FillRectangle(brush, this.ClientRectangle);
// Dispose of objects
brush.Dispose();
}
void CBoxHatchStyleSelectedIndexChanged(object sender, System.EventArgs e)
{
this.Invalidate();
}
}
}