www.pudn.com > GDILines.rar > Main.cs, change:2013-06-29,size:3100b


using System; 
using System.Drawing; 
using System.Windows.Forms; 
 
namespace GDILines 
{ 
    public partial class Main : Form 
    { 
        public Main() 
        { 
            InitializeComponent(); 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            //设置图片框的背景图片 
            using (OpenFileDialog ofd = new OpenFileDialog()) 
            { 
                ofd.Filter = "All Pic|*.jpg;*.png;*.gif;*.bmp"; 
                if (ofd.ShowDialog() == DialogResult.OK) 
                    pictureBox1.BackgroundImage = Image.FromFile(ofd.FileName); 
            } 
        } 
        Color pencolor = Color.Black; 
        private void button2_Click(object sender, EventArgs e) 
        { 
            //设置笔的颜色 
            using (ColorDialog cd = new ColorDialog()) 
                if (cd.ShowDialog() == DialogResult.OK) 
                    pencolor = cd.Color; 
        } 
 
        Point start = new Point(); Point end = new Point(); bool draw = false; 
 
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
        { 
            //判断是否点击 
            if (e.Button == MouseButtons.Left) 
            { 
                //起始点 
                start = e.Location; 
                //显示位置 
                label3.Text = string.Format("Staus: StartPoint[X:{0},Y:{1}] , EndPoint[X:-1 Y:-1]", start.X, start.Y); 
                draw = true; 
            } 
        } 
 
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
        { 
            //判断是否点击 
            if (e.Button == MouseButtons.Left) 
            { 
                //结束点 
                end = e.Location; 
                //显示位置 
                label3.Text = string.Format("Staus: StartPoint[X:{0},Y:{1}] , EndPoint[X:{2} Y:{3}]", start.X, start.Y, end.X, end.Y); 
                //利用自身绘画画线 
                pictureBox1.Refresh(); 
            } 
        } 
 
        private void pictureBox1_Paint(object sender, PaintEventArgs e) 
        { 
            //绘画 
            if (draw) 
                e.Graphics.DrawLine(new Pen(pencolor), start, end); 
        } 
 
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
        { 
            draw = false; 
        } 
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            //设置背景图片的布局 
            switch (comboBox1.SelectedIndex) 
            { 
                case 0: pictureBox1.BackgroundImageLayout = ImageLayout.None; break; 
                case 1: pictureBox1.BackgroundImageLayout = ImageLayout.Tile;break; 
                case 2:pictureBox1.BackgroundImageLayout = ImageLayout.Center; break; 
                case 3:pictureBox1.BackgroundImageLayout = ImageLayout.Stretch; break; 
                case 4:pictureBox1.BackgroundImageLayout = ImageLayout.Zoom; break; 
            } 
        } 
 
        private void Main_Load(object sender, EventArgs e) 
        { 
            comboBox1.SelectedIndex = 0; 
        } 
    } 
}