Control.Location / Control.Size / Control.ClientRectangle / Control.DisplayRectangle在.Net 4.6.1中被破坏了! [英] Control.Location / Control.Size / Control.ClientRectangle / Control.DisplayRectangle are broken in .Net 4.6.1!

查看:78
本文介绍了Control.Location / Control.Size / Control.ClientRectangle / Control.DisplayRectangle在.Net 4.6.1中被破坏了!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些属性存在严重缺陷,并且无法检查光标是否在控件上。

There are crippling bugs with these properties and checking whether the cursor is on a control is impossible.

ClientRectangle和  DisplayRectangle总是远离左侧和顶部控件和使用位置和尺寸更准确,它在某种程度上失败了控制的外部区域。

ClientRectangle and DisplayRectangle are always far off to the left and top of the control and while using the Location and Size is more accurate, it somehow fails towards the outer area of the control.

这是100%可以在机器上重现,我测试了它具有各种控制。

This  is 100% reproducible across machines and I have tested it with various controls.

以下是它发生的一个例子:

Here is an example of it happening:

表单设计器 

Form Designer 

namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <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.listBox1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(75, 73);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(187, 173);
            this.listBox1.TabIndex = 0;
            this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(343, 324);
            this.Controls.Add(this.listBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ListBox listBox1;
    }
}

表格代码

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            CheckCursor(e.Location);
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            CheckCursor(e.Location);
        }

        private void CheckCursor(Point p)
        {
            listBox1.Items.Clear();
            Graphics g = this.CreateGraphics();
            Rectangle lbrect = new Rectangle(listBox1.Location, listBox1.Size);
            g.DrawRectangle(Pens.Red, lbrect);
            g.DrawRectangle(Pens.Blue, listBox1.ClientRectangle);
            g.DrawRectangle(Pens.Green, listBox1.DisplayRectangle);

            Rectangle prect = new Rectangle(p, new Size(0, 0));

            listBox1.Items.Add("intersects (bounds): " + lbrect.IntersectsWith(prect));
            listBox1.Items.Add("intersects (client): " + listBox1.ClientRectangle.IntersectsWith(prect));
            listBox1.Items.Add("intersect (display): " + listBox1.DisplayRectangle.IntersectsWith(prect));
        }
    }
}

推荐答案

您好Ishiirou,

Hi Ishiirou,

感谢您发布此处。

根据您的问题与Winform更相关,我将其移至                               list-style-type:none; font-size:12.9037px; font-weight:inherit; font-style:inherit"> Windows
Forms General
  论坛以获得合适的支持。

According to your question is more related to Winform, I will move it to  Windows Forms General forum for suitable support.

Visual C#讨论并询问C#编程语言,IDE,库,示例和工具。

The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

如果你有一些语法或代码错误, 请随时与我们联系。我们将尽力为您提供解决方案。

If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

感谢您的理解与合作。

最诚挚的问候,

Wendy 

Wendy 


这篇关于Control.Location / Control.Size / Control.ClientRectangle / Control.DisplayRectangle在.Net 4.6.1中被破坏了!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆