百度空间 | 百度首页 
 
查看文章
 
GDI+的Pen 属性 (代码直击)
2009年05月20日 星期三 20:31

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Pen pen1;
        Pen pen;
        Pen pen2;
        Graphics g;
        Rectangle re;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            pen = new Pen(Color.Red, 10);
            pen1 = new Pen(Color.Black, 5);
            pen2 = new Pen(Color.White, 0);
            re = new Rectangle(200, 180, 390, 390);
         
            switch (comboBox1.SelectedItem.ToString())
            {
                case "Center":
                    pen1.Alignment = PenAlignment.Center;
                    break;
                case "Inset":
                    pen1.Alignment = PenAlignment.Inset;
                    break;
                case "Left":
                    pen1.Alignment = PenAlignment.Left;
                    break;
                case "Outset":
                    pen1.Alignment = PenAlignment.Outset;
                    break;
                case "Right":
                    pen1.Alignment = PenAlignment.Right;
                    break;

            }

            switch (this.comboBox2.SelectedItem.ToString())
            {
                case "Custom":
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
                    break;
                case "Dash":
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                    break;
                case "DashDot":
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                    break;
                case "DashDotDot":
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
                    break;
                case "Dot":
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                    break;
                case "Solid":
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                    break;

            }// pen.Dispose();
            switch (this.comboBox3.SelectedItem.ToString())
            {
                case "AnchorMask":
                    pen2.EndCap = LineCap.AnchorMask;
                    break;
                case "ArrowAnchor":
                    pen2.EndCap = LineCap.ArrowAnchor;
                    break;
                case "SquareAnchor":
                    pen2.EndCap = LineCap.SquareAnchor;
                    break;
                case "NoAnchor":
                    pen2.EndCap = LineCap.NoAnchor;
                    break;
                case "DiamondAnchor":
                    pen2.EndCap = LineCap.DiamondAnchor;
                    break;
                case "RoundAnchor":
                    pen2.EndCap = LineCap.RoundAnchor;
                    break;

            }Bitmap bit=new Bitmap

            g.DrawLine(pen1, 0, 200, 700, 200);
            g.DrawLine(pen2, 0, 550, 870, 550);
            g.DrawRectangle(pen, re);
            pen.Dispose();
            pen1.Dispose();
            pen2.Dispose();
            g.Dispose();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            #region 加载复选框内容
            this.comboBox1.Items.Add(PenAlignment.Center);
            this.comboBox1.Items.Add(PenAlignment.Inset);
            this.comboBox1.Items.Add(PenAlignment.Left);
            this.comboBox1.Items.Add(PenAlignment.Outset);
            this.comboBox1.Items.Add(PenAlignment.Right);
            this.comboBox2.Items.Add(DashStyle.Custom);
            this.comboBox2.Items.Add(DashStyle.Dash);
            this.comboBox2.Items.Add(DashStyle.DashDot);
            this.comboBox2.Items.Add(DashStyle.DashDotDot);
            this.comboBox2.Items.Add(DashStyle.Dot);
            this.comboBox2.Items.Add(DashStyle.Solid);
            this.comboBox3.Items.Add(System.Drawing.Drawing2D.LineCap.AnchorMask);
            this.comboBox3.Items.Add(System.Drawing.Drawing2D.LineCap.ArrowAnchor);
            this.comboBox3.Items.Add(System.Drawing.Drawing2D.LineCap.SquareAnchor);
            this.comboBox3.Items.Add(System.Drawing.Drawing2D.LineCap.NoAnchor);
            this.comboBox3.Items.Add(System.Drawing.Drawing2D.LineCap.DiamondAnchor);
            this.comboBox3.Items.Add(System.Drawing.Drawing2D.LineCap.RoundAnchor);
            #endregion
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.SelectedIndex = 0;
            this.comboBox3.SelectedIndex = 0;

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Invalidate();
        }

    }
}


类别:c# winform | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu