2008年10月6日

A little program used to Custom Line Cap As DiamondAnchor:

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

namespace NJude
{
    public partial class NJude : Form
    {
        private const int Length = 20;
        private const int width = 15;
        Point startPoint = new Point();
        Point endPoint = new Point();
        bool flag = false;

        public NJude()
        {
            InitializeComponent();
        }

        private Point[] GetsPointPath(Point startPoint, Point endPoint)
        {
            Point[] points = new Point[4];
            Point p1 = new Point();
            Point p2 = new Point();
            Point p3 = new Point();

            //if (startPoint.X <= endPoint.X)
            //{
                p1.X = startPoint.X + Convert.ToInt32((Length * (endPoint.X - startPoint.X)) / Distance(startPoint, endPoint));
                p1.Y = startPoint.Y + Convert.ToInt32((Length * (endPoint.Y - startPoint.Y)) / Distance(startPoint, endPoint));
            //}
            //else
            //{
                //p1.X = startPoint.X - Convert.ToInt32((Length * (endPoint.X - startPoint.X)) / Distance(startPoint, endPoint));
                //p1.Y = startPoint.Y - Convert.ToInt32((Length * (endPoint.Y - startPoint.Y)) / Distance(startPoint, endPoint));
            //}

            p2.X = Convert.ToInt32((startPoint.X + p1.X) / 2) + Convert.ToInt32((width * (endPoint.Y - startPoint.Y)) / (2 * Distance(startPoint, endPoint)));
            p2.Y = Convert.ToInt32((startPoint.Y + p1.Y) / 2) - Convert.ToInt32((width * (endPoint.X - startPoint.X)) / (2 * Distance(startPoint, endPoint)));

            p3.X = Convert.ToInt32((startPoint.X + p1.X) / 2) - Convert.ToInt32((width * (endPoint.Y - startPoint.Y)) / (2 * Distance(startPoint, endPoint)));
            p3.Y = Convert.ToInt32((startPoint.Y + p1.Y) / 2) + Convert.ToInt32((width * (endPoint.X - startPoint.X)) / (2 * Distance(startPoint, endPoint)));

            points[0] = startPoint;
            points[1] = p1;
            points[2] = p2;
            points[3] = p3;

            return points;
        }

        private int Distance(Point startPoint, Point endPoint)
        {
            return Convert.ToInt32(Math.Sqrt((endPoint.X - startPoint.X) * (endPoint.X - startPoint.X)
                            + (endPoint.Y - startPoint.Y) * (endPoint.Y - startPoint.Y)));
        }

        private void NJude_MouseClick(object sender, MouseEventArgs e)
        {
            if (flag)
            {
                endPoint = e.Location;
                Graphics g = CreateGraphics();
                Pen blackPen = new Pen(Color.Black, 1);
                SolidBrush blackBrush = new SolidBrush(Color.Black);
                GraphicsPath path = new GraphicsPath();
                path.AddLine(GetsPointPath(startPoint, endPoint)[0], GetsPointPath(startPoint, endPoint)[2]);
                path.AddLine(GetsPointPath(startPoint, endPoint)[2], GetsPointPath(startPoint, endPoint)[1]);
                path.AddLine(GetsPointPath(startPoint, endPoint)[1], GetsPointPath(startPoint, endPoint)[3]);
                path.AddLine(GetsPointPath(startPoint, endPoint)[3], GetsPointPath(startPoint, endPoint)[0]);

                //g.DrawLine(blackPen, GetsPointPath(startPoint, endPoint)[0], GetsPointPath(startPoint, endPoint)[2]);
                //g.DrawLine(blackPen, GetsPointPath(startPoint, endPoint)[2], GetsPointPath(startPoint, endPoint)[1]);
                //g.DrawLine(blackPen, GetsPointPath(startPoint, endPoint)[1], GetsPointPath(startPoint, endPoint)[3]);
                //g.DrawLine(blackPen, GetsPointPath(startPoint, endPoint)[3], GetsPointPath(startPoint, endPoint)[0]);

                g.DrawPath(blackPen, path);
                g.FillPath(blackBrush, path);
                Pen penForLine = new Pen(Color.Black, 1);
                penForLine.CustomEndCap = new System.Drawing.Drawing2D.AdjustableArrowCap((float)(penForLine.Width * 10), (float)(penForLine.Width * 15), false);
                g.DrawLine(penForLine, GetsPointPath(startPoint, endPoint)[1], endPoint);
                startPoint = new Point();
                flag = false;

                g.Dispose();
                blackPen.Dispose();
                penForLine.Dispose();
                blackBrush.Dispose();
            }
            else
            {
                startPoint = e.Location;
                flag = true;
            }
        }
    }
}

Hope useful for somebody.

posted @ 2008-10-06 00:48 龙啸 阅读(223) 评论(0) 推荐(0)

2008年9月21日

摘要: // Project -- DocToolkit // DocManager.cs #region Using directives using System; using System.Windows.Forms; using System.Diagnostics; using System.IO; using System.Globalization; using System.Runtime... 阅读全文

posted @ 2008-09-21 10:06 龙啸 阅读(872) 评论(1) 推荐(0)

点击右上角即可分享
微信分享提示