首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >winforms面板上的位置不准确

winforms面板上的位置不准确
EN

Stack Overflow用户
提问于 2020-12-18 17:57:26
回答 1查看 142关注 0票数 0

我一直在使用VisualStudio2019版本使用WinForms应用程序,并且在我的代码中遇到了坐标系统的准确性问题。我有一些图形元素(实际上是标签),你可以点击一个,点击另一个地方,它会被移到那个地方,当我只是在应用程序的背景上这样做的时候,它会相当准确地工作。但是,当我在图形元素后面添加一个面板时,它不能正常工作,而是稍微向上移动标签。我在面板上画了几条线来说明这一点。下面是我的代码,用于当它在面板(称为键合器)内单击时:

代码语言:javascript
复制
private void Bonder_Click(object sender, MouseEventArgs e)
        {
            mousePosInBonder = e.Location;

            foreach (GraphicalElement el in elementsInBonder)
            {
                if (el.Dragging)
                {
                    while (el.Dragging)
                    {
                        if (!(mousePos.X > el.Location.X && mousePos.X < el.Location.X + el.Size.Width && mousePos.Y > el.Location.Y && mousePos.Y < el.Location.Y + el.Size.Height))
                        {
                            el.Location = mousePosInBonder;
                            el.Dragging = false;
                        }
                    }
                    break;
                }
            }
        }

这是我绘制线条的代码(并单击):

代码语言:javascript
复制
private void GraphicalElement_Drag(object sender, MouseEventArgs e)
        {
            GraphicalElement el = (GraphicalElement)sender;

            if (e.Button == MouseButtons.Left)
            {
                if (el.Dragging)
                {
                    el.Dragging = false;
                }
                else if (!el.Dragging)
                {
                    el.Dragging = true;
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (el.Bonding)
                {
                    if (BONDING_ELEMENTS)
                    {
                        g = Bonder.CreateGraphics();
                        g.DrawLine(bonder, el.Location, LAST_BONDED_ELEMENT.Location);
                        BONDING_ELEMENTS = false;
                    }
                    else if (!BONDING_ELEMENTS)
                    {
                        LAST_BONDED_ELEMENT = el;
                        BONDING_ELEMENTS = true;
                    }
                }
            }
        }

这是在单击元素时调用的(因为标签不能被拖动)。这是显示元素的GraphicalElement类的代码。

代码语言:javascript
复制
public partial class GraphicalElement:Label
    {
        // Attributes
        private bool dragging = false;
        private bool bonding = false;
        private string info = "";

        public GraphicalElement() { }

        public GraphicalElement(Element el)
        {
            Tag = el.ToString();
            Text = el.ChemicalFormula;
            AutoSize = true;
            Location = new Point(100, 100);
            
            info = el.Info;

            if (el.Type == "alkali metal")
            {
                BackColor = Color.Crimson;
            }
            else if (el.Type == "alkaline earth")
            {
                BackColor = Color.DarkOrange;
            }
            else if (el.Type == "transition metal")
            {
                BackColor = Color.Yellow;
            }
            else if (el.Type == "basic metal")
            {
                BackColor = Color.Green;
            }
            else if (el.Type == "semimetal")
            {
                BackColor = Color.Cyan;
            }
            else if (el.Type == "nonmetal")
            {
                BackColor = Color.Blue;
            }
            else if (el.Type == "halogen")
            {
                BackColor = Color.Pink;
            }
            else if (el.Type == "noble gas")
            {
                BackColor = Color.DarkMagenta;
            }
            else if (el.Type == "lathanide")
            {
                BackColor = Color.HotPink;
            }
            else if (el.Type == "actinide")
            {
                BackColor = Color.Maroon;
            }
        }

        // Getters and setters
        public bool Dragging
        {
            get { return dragging; }
            set { dragging = value; }
        }
        public bool Bonding
        {
            get { return bonding; }
            set { bonding = value; }
        }
        public string Info
        {
            get { return info; }
            set { info = value; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

    }

这就是它看起来的样子:

我该如何解决这个问题,因为它很误导人。

编辑--这是将元素添加到elementsInBonder的位置:

代码语言:javascript
复制
private void GraphicalElement_Bond(object sender, MouseEventArgs e)
        {
            GraphicalElement el = (GraphicalElement)sender;
            
            if (el.Bonding)
            {
                elementsInBonder.Remove(el);
                elementsOnScreen.Add(el);
                el.Location = new Point(1011, 43);
                el.BringToFront();
                el.Bonding = false;
            }
            else if (!el.Bonding && BONDER_ON)
            {
                elementsOnScreen.Remove(el);
                elementsInBonder.Add(el);
                el.Location = new Point(12, 43);
                el.BringToFront();
                el.Bonding = true;
            }
        }

这种情况发生在双击事件中。

我在一开始就是这样说的。List<GraphicalElement> elementsInBonder;。GraphicalElement不是Panel的子类,因为图形元素可以在两个列表之间跳过: elementsOnScreen,从而在键合面板中上下移动。

EN

回答 1

Stack Overflow用户

发布于 2020-12-19 09:19:35

我主要是通过反复试验才找到答案的。面板稍微偏离左上角,所以我想知道GraphicalElements是否与面板的距离相同,在这个例子中,右边是12个像素,下面是43个像素。所以,我改变了代码来表示它。

对于Bonder_click()方法,我更改了mousePosInBonder,使其表示出一些像素,如下所示:

代码语言:javascript
复制
private void Bonder_Click(object sender, MouseEventArgs e)
        {
            mousePosInBonder = new Point(e.Location.X+12, e.Location.Y+43);

            foreach (GraphicalElement el in elementsInBonder)
            {
                if (el.Dragging)
                {
                    while (el.Dragging)
                    {
                        if (!(mousePos.X > el.Location.X && mousePos.X < el.Location.X + el.Size.Width && mousePos.Y > el.Location.Y && mousePos.Y < el.Location.Y + el.Size.Height))
                        {
                            el.Location = mousePosInBonder;
                            el.Dragging = false;
                        }
                    }
                    break;
                }
            }
        }
    }

我更改的行是mousePosInBonder = new Point(e.Location.X+12, e.Location.Y+43);,而以前是mousePosInBonder = e.Location;

在画线的时候,我做了一件类似的事情:

代码语言:javascript
复制
private void GraphicalElement_Drag(object sender, MouseEventArgs e)
        {
            GraphicalElement el = (GraphicalElement)sender;

            if (e.Button == MouseButtons.Left)
            {
                if (el.Dragging)
                {
                    el.Dragging = false;
                }
                else if (!el.Dragging)
                {
                    el.Dragging = true;
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (el.Bonding)
                {
                    if (BONDING_ELEMENTS)
                    {
                        g = Bonder.CreateGraphics();
                        g.DrawLine(convalentBonder, new Point(el.Location.X-12, el.Location.Y-43), new Point(LAST_BONDED_ELEMENT.Location.X-12, LAST_BONDED_ELEMENT.Location.Y-43));
                        BONDING_ELEMENTS = false;
                    }
                    else if (!BONDING_ELEMENTS)
                    {
                        LAST_BONDED_ELEMENT = el;
                        BONDING_ELEMENTS = true;
                    }
                }
            }
        }

在这方面,我更改的行是g.DrawLine(convalentBonder, new Point(el.Location.X-12, el.Location.Y-43), new Point(LAST_BONDED_ELEMENT.Location.X-12, LAST_BONDED_ELEMENT.Location.Y-43));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65361785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档