| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 762 人关注过本帖
标题:选中窗体内的直线
只看楼主 加入收藏
anyebeishang
Rank: 1
来 自:黑龙江
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-7-16
收藏
 问题点数:0 回复次数:5 
选中窗体内的直线
我绘制了一条直线  如何通过点击鼠标来选中直线
搜索更多相关主题的帖子: 如何 
2014-07-16 09:40
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
把直线做成控件

站在春哥的肩膀上
2014-07-16 10:59
anyebeishang
Rank: 1
来 自:黑龙江
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-7-16
收藏
得分:0 
回复 2 楼 xydddaxia
那我要如何做呀?我新学的C#  不太会用!
2014-07-16 11:52
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
using System;
using System.Collections.Generic;
using using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class StraightLine : Control
    {
        private Enums.StraightLineTypes lineType;
        private bool antiAlias;
        private int thickness;
        protected Pen pen;
        public StraightLine()
        {
            lineType = Enums.StraightLineTypes.水平;
            thickness = 1;
            antiAlias = true;
            this.Paint += StraightLine_Paint;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && pen != null)
            {
                pen.Dispose();
            }
            base.Dispose(disposing);
        }
        [Category("连线属性"), DefaultValue(typeof(Enums.StraightLineTypes), "StraightLineTypes.Horizontal")]
        public Enums.StraightLineTypes LineType
        {
            get
            {
                return lineType;
            }
            set
            {
                lineType = value;
                Invalidate();
            }
        }

        [Category("连线属性"), DefaultValue(1)]
        public int Thickness
        {
            get
            {
                return thickness;
            }
            set
            {
                thickness = value;
                Invalidate();
            }
        }
        [Category("连线属性"), DefaultValue(true)]
        public bool AntiAlias
        {
            get
            {
                return antiAlias;
            }
            set
            {
                antiAlias = value;
                Invalidate();
            }
        }
        /// <summary>
        /// 重写画线部分GDI实现
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void StraightLine_Paint(object sender, PaintEventArgs e)
        {
            pen = new Pen(ForeColor, Thickness);

            if (AntiAlias)
            {
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            }

            switch (lineType)
            {
                case Enums.StraightLineTypes.水平:
                    DrawCenteredHorizontalLine(e.Graphics);
                    break;
                case Enums.StraightLineTypes.垂直:
                    DrawCenteredVerticalLine(e.Graphics);
                    break;
                case Enums.StraightLineTypes.斜线:
                    DrawCenteredDiagonalAscendingLine(e.Graphics);
                    break;
                case Enums.StraightLineTypes.反斜线:
                    DrawCenteredDiagonalDescendingLine(e.Graphics);
                    break;
                default: break;
            }
        }

        #region 画线函数

        private void DrawCenteredHorizontalLine(Graphics g)
        {
            g.DrawLine(pen, 0, Height / 2, Width, Height / 2);
            Graphics graphics = this.CreateGraphics();
            Point[] mypoints = { new Point(0, Height / 2 + 1), new Point(0, Height / 2 - 1), new Point(Width, Height / 2 - 1), new Point(Width, Height / 2 + 1) };
            RegionControl(mypoints);
        }

        private void DrawCenteredVerticalLine(Graphics g)
        {
            g.DrawLine(pen, Width / 2, 0, Width / 2, Height);
            Point[] mypoints = { new Point(Width / 2 - 1, 0), new Point(Width / 2 + 1, 0), new Point(Width / 2 + 1, Height), new Point(Width / 2 - 1, Height) };
            RegionControl(mypoints);
        }

        private void DrawCenteredDiagonalAscendingLine(Graphics g)
        {
            g.DrawLine(pen, 0, Height, Width, 0);
            Point[] mypoints = { new Point(1, Height), new Point(0, Height - 1), new Point(Width - 1, 0), new Point(Width, 1) };
            RegionControl(mypoints);
        }

        private void DrawCenteredDiagonalDescendingLine(Graphics g)
        {
            g.DrawLine(pen, 0, 0, Width, Height);
            Point[] mypoints = { new Point(0, 1), new Point(1, 0), new Point(Width, Height - 1), new Point(Width - 1, Height) };
            RegionControl(mypoints);
        }

        /// <summary>
        /// 按照点集合生成异形窗体
        /// </summary>
        /// <param name="points"></param>
        private void RegionControl(Point[] points)
        {
            GraphicsPath mygraphicsPath = new GraphicsPath();
            mygraphicsPath.AddPolygon(points);
            Region myregion = new Region(mygraphicsPath);
            this.Region = myregion;
        }
        #endregion
    }
    public class Enums
    {
        public enum StraightLineTypes
        {
            //Auto, 
            水平,
            垂直,
            反斜线,
            斜线,
        }
    }
}

站在春哥的肩膀上
2014-07-16 15:28
anyebeishang
Rank: 1
来 自:黑龙江
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-7-16
收藏
得分:0 
回复 4 楼 xydddaxia
非常感谢!
2014-07-21 18:31
anyebeishang
Rank: 1
来 自:黑龙江
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-7-16
收藏
得分:0 
回复 4 楼 xydddaxia
WindowForm和WPF的应该区别不大吧!我的是WPF的
2014-07-21 18:34
快速回复:选中窗体内的直线
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027613 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved