如图,在鼠标选择窗体上的控件时,选择框在正常情况下应该处于按钮的上层,但我不知道应该怎样做,请各位帮我修改一下!下面是代码:Form4.Designer.cs
---------------------
namespace MapDemo
{
    partial class Form4
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }        #region Windows Form Designer generated code        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.AutoScroll = true;
            this.panel1.Controls.Add(this.button2);
            this.panel1.Controls.Add(this.button1);
            this.panel1.Controls.Add(this.pictureBox1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(295, 173);
            this.panel1.TabIndex = 3;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = global::MapDemo.Properties.Resources.四号线地图;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(575, 767);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox1.TabIndex = 3;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(53, 52);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(56, 36);
            this.button1.TabIndex = 4;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(53, 94);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(56, 29);
            this.button2.TabIndex = 5;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // Form4
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(295, 173);
            this.Controls.Add(this.panel1);
            this.Name = "Form4";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form4";
            this.Load += new System.EventHandler(this.Form4_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;    }
}

解决方案 »

  1.   

    Form4.cs
    -------------------
    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;namespace MapDemo
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
            }
            private Point PTStart = Point.Empty; //框选起始点坐标
            private void button1_MouseDown(object sender, MouseEventArgs e)
            {
                //this.DoDragDrop(button1, DragDropEffects.Move);
            }        private void panel1_DragEnter(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Move;
            }        private void Form4_Load(object sender, EventArgs e)
            {
                // this.pictureBox1.BringToFront();
            }        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    //框选起始位置
                    this.PTStart = e.Location;
                }
            }        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int x = Math.Min(this.panel1.ClientSize.Width - 1, Math.Max(0, e.X));
                    int y = Math.Min(this.panel1.ClientSize.Height - 1, Math.Max(0, e.Y));
                    int width = PTStart.X - x;
                    int height = PTStart.Y - y;
                    if (width < 0)
                    {
                        x = PTStart.X;
                        width = Math.Abs(width);
                    }
                    if (height < 0)
                    {
                        y = PTStart.Y;
                        height = Math.Abs(height);
                    }                int checkX = PointToClient(Control.MousePosition).X;
                    int checkY = PointToClient(Control.MousePosition).Y;                if (checkX <= 2)
                    {
                        if (this.panel1.HorizontalScroll.Value - 10 <= 0)
                            this.panel1.HorizontalScroll.Value = 0;
                        else
                            this.panel1.HorizontalScroll.Value -= 10;
                    }
                    else if (checkX + 20 >= this.panel1.ClientSize.Width)
                    {
                        this.panel1.HorizontalScroll.Value += 10;
                    }                if (checkY <= 2)
                    {
                        if (this.panel1.VerticalScroll.Value - 10 <= 0)
                            this.panel1.VerticalScroll.Value = 0;
                        else this.panel1.VerticalScroll.Value -= 10;
                    }
                    else if (checkY + 20 >= panel1.ClientSize.Height)
                    {
                        this.panel1.VerticalScroll.Value += 10;
                    }
                    this.pictureBox1.Refresh();
                    Graphics g = this.pictureBox1.CreateGraphics();
                    
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    Pen pen = new Pen(Color.Blue, 1);
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                    Rectangle GraphicsRectangle = new Rectangle(x, y, width, height);
                    g.DrawRectangle(pen, GraphicsRectangle);
                    g.Dispose();
                }
            }        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                this.pictureBox1.Refresh();
            }
        }
    }
      

  2.   

    to gisfarmer 
    不是GIS,就是想实现在控件上框选的效果,不知道该如何做
      

  3.   

    这个难了 兄弟!
    就算实现 也不理想给你个例子
     这样的绘制能勉强覆盖子控件 
             [DllImport("user32.dll")]
            static extern IntPtr GetWindowDC(IntPtr hWnd);        [DllImport("user32.dll")]
            static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);         public Form6()
            {
                InitializeComponent();
            }
            protected override void OnPaint(PaintEventArgs e)
            { 
              
                base.OnPaint(e);
                //这里的绘制能覆盖子控件,但是子控件也会自己绘制哦,希望对你有帮助
                IntPtr vDC = GetWindowDC(this.Handle);
                Graphics vGraphics = Graphics.FromHdc(vDC);
                vGraphics.FillRectangle(Brushes.Red, 60, 60, 55, 70);
                vGraphics.DrawRectangle(Pens.Red, 90, 60, 25, 70); 
            }
      

  4.   

    确实很麻烦啊,你这样paint的顺序必须是背景->按钮->鼠标,可是第一个和第三个的paint函数是放在一起的……
    要是能控制这些绘制函数调用的先后顺序就好了。
      

  5.   

    to hiddkiller 
    效果不太好,和用 “ControlPaint.DrawReversibleFrame(GraphicsRectangle, Color.Yellow, FrameStyle.Dashed);” 方法画出的效果是一样的,没有办法处理低图在滚动条更改的情况下正常显示绘出的矩形。
      

  6.   

    如果lz只是达到用鼠标绘制一个矩形选择框,然后显示出处于该矩形框内按钮的功能的话,可以试一下以下代码Public Class Form1
        Dim PtStart, pt As Point
        Dim RectSize As Size    Dim WithEvents Pic1 As New PictureBox    Private Sub Pic1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then PtStart = New Point(e.X, e.Y)
        End Sub    Private Sub Pic1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
                If pt <> Nothing Then ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
                pt = PointToScreen(New Point(e.X, e.Y)) : pt.Offset(Pic1.Location)
                ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
            End If
        End Sub    Private Sub Pic1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseUp
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
                ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
                Call ControlIsInRect(PtStart, New Point(e.X, e.Y))
                pt = Nothing
            End If
        End Sub    Private Sub ControlIsInRect(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
            Pic1.Refresh()
            If mPoint2.X > mPoint1.X And mPoint2.Y > mPoint1.Y Then
                PtStart = New Point(mPoint1.X, mPoint1.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint2.Y - mPoint1.Y)
            ElseIf mPoint2.X > mPoint1.X And mPoint2.Y < mPoint1.Y Then
                PtStart = New Point(mPoint1.X, mPoint2.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint1.Y - mPoint2.Y)
            ElseIf mPoint2.X < mPoint1.X And mPoint2.Y > mPoint1.Y Then
                PtStart = New Point(mPoint2.X, mPoint1.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint2.Y - mPoint1.Y)
            ElseIf mPoint2.X < mPoint1.X And mPoint2.Y < mPoint1.Y Then
                PtStart = New Point(mPoint2.X, mPoint2.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint1.Y - mPoint2.Y)
            End If        Dim rect As New Rectangle(PtStart, RectSize)
            For Each btn As MyButton In Pic1.Controls
                If rect.Contains(btn.Left, btn.Top) OrElse rect.Contains(btn.Right, btn.Bottom) Then
                    btn.IsInRect = True
                Else
                    btn.IsInRect = False
                End If
            Next
        End Sub    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
            Dim btn As New MyButton
            btn.Text = "Test"
            btn.SetBounds(100, 60, 60, 30)
            Pic1.Controls.Add(btn)
            Pic1.Dock = DockStyle.Fill
            Me.Controls.Add(Pic1)
        End SubEnd Class
    Public Class MyButton
        Inherits System.Windows.Forms.Button    Private _IsInRect As Boolean
        Public Property IsInRect() As Boolean
            Get
                Return _IsInRect
            End Get
            Set(ByVal value As Boolean)
                _IsInRect = value
                Me.Invalidate()
            End Set
        End Property    Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(pevent)
            If IsInRect Then
                pevent.Graphics.DrawRectangle(Pens.Red, New Rectangle(0, 0, 10, 10))
                pevent.Graphics.DrawRectangle(Pens.Red, New Rectangle(Me.Width - 10, 0, 10, 10))
                pevent.Graphics.DrawRectangle(Pens.Red, New Rectangle(0, Me.Height - 10, 10, 10))
                pevent.Graphics.DrawRectangle(Pens.Red, New Rectangle(Me.Width - 10, Me.Height - 10, 10, 10))
            End If
        End SubEnd Class
      

  7.   

    如果用ControlPaint.DrawReversibleFrame的话,很难处理当图片大于窗体出现滚动条后,在画矩形框时移动滚动条位置的问题,这个问题我以前问过但一直没有解决:
    http://topic.csdn.net/u/20080516/18/f6f82ccd-cc6e-4e65-acb5-b610ee0e3b82.html