请教关于GDI+画选择框时出现的一个问题
如图,在鼠标选择窗体上的控件时,选择框在正常情况下应该处于按钮的上层,但我不知道应该怎样做,请各位帮我修改一下!
下面是代码:
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;
}
}
[解决办法]
up
[解决办法]
搞GIS的???莫非是同行?
地图选择做过,控件选择没有做过。帮你顶起~
[解决办法]
关注
[解决办法]
这个难了 兄弟!
就算实现 也不理想
给你个例子
这样的绘制能勉强覆盖子控件
[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);
}
[解决办法]
确实很麻烦啊,你这样paint的顺序必须是背景->按钮->鼠标,可是第一个和第三个的paint函数是放在一起的……
要是能控制这些绘制函数调用的先后顺序就好了。
[解决办法]
比较帅...
[解决办法]
起
[解决办法]
如果lz只是达到用鼠标绘制一个矩形选择框,然后显示出处于该矩形框内按钮的功能的话,可以试一下以下代码
- VB.NET code
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 ClassPublic 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