码迷,mamicode.com
首页 > 其他好文 > 详细

(转)在 ListViewItem 上拖动进行框选

时间:2014-10-09 01:46:57      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   for   sp   art   

public partial class Form1 : Form
{
        private bool IsMouseDown = false;
        Rectangle MouseRect = Rectangle.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            IsMouseDown = true;
            DrawStart(e.Location); 
        }

        private void listView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown)
            {
                ResizeToRectangle(e.Location);
                ListViewItem item = listView1.HitTest(e.X, e.Y).Item;
                if (item != null)
                {
                    item.Selected = true;
                }

                foreach (ListViewItem listViewItem in listView1.SelectedItems)
                {
                    if ((listViewItem.Bounds.Top > MouseRect.Top && listViewItem.Bounds.Bottom > MouseRect.Top)
                        || (listViewItem.Bounds.Bottom < MouseRect.Bottom && listViewItem.Bounds.Top < MouseRect.Bottom))
                    {
                        listViewItem.Selected = false;
                    }
                }
            }
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            this.Capture = false;
            Cursor.Clip = Rectangle.Empty;
            IsMouseDown = false;
            DrawRectangle();
            MouseRect = Rectangle.Empty;
        }

        private void ResizeToRectangle(Point p)
        {
            DrawRectangle();
            MouseRect.Width = p.X - MouseRect.Left;
            MouseRect.Height = p.Y - MouseRect.Top;
            DrawRectangle();
        }

        private void DrawRectangle()
        {
            Rectangle rect = listView1.RectangleToScreen(MouseRect);
            ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
        }

        private void DrawStart(Point StartPoint)
        {
            this.Capture = true;
            Cursor.Clip = listView1.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));
            MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
        }
    }

转自: http://social.msdn.microsoft.com/Forums/silverlight/zh-CN/5685ef2c-7cfa-4f03-822c-4992bf6bce1c/-listviewitem-?forum=visualcshartzhchs

(转)在 ListViewItem 上拖动进行框选

标签:style   http   color   io   os   ar   for   sp   art   

原文地址:http://www.cnblogs.com/Impulse/p/4011767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!