简体   繁体   English

Visual Studio面板映射

[英]Visual Studio Panel mapping

I have a program, where I can draw simple shapes on a panel , and it tracks my mouse movement. 我有一个程序,可以在panel上绘制简单的形状,它可以跟踪鼠标的移动。

 private void DrawingPanel_MouseMove(object sender, MouseEventArgs e)
 {
     toolStripStatusLabel1.Text= e.X + "," + e.Y;
 }

I was wondering if there is some way that I can "map" my panel. 我想知道是否可以通过某种方式“映射”面板。 By mapping I mean creating areas (with rectangle coords fe) that will show some message or pop-up information on my cursor when I hover over this area. 映射是指创建区域(具有rectangle coords fe),当我将鼠标悬停在该区域时,该区域将在光标上显示一些消息或弹出信息。

Is it possible to do something like that? 可以做这样的事情吗?

EDIT: To be clear - I dont use mouseEvent to draw shape on my panel. 编辑:要清楚-我不使用mouseEvent在面板上绘制形状。 I draw them with buttons created on side menu. 我用侧面菜单上创建的按钮绘制它们。

EDIT2: Example: I drew rectangle (10, 10, 20, 20). EDIT2:示例:我画了矩形(10,10,20,20)。 When i hover my mouse on those coords i would like to get a pop-up at my cursor. 当我将鼠标悬停在这些坐标上时,我想在光标处弹出一个窗口。 Somethinkg like: 像这样的东西:

System.Windows.Forms.ToolTip Rect = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(this.Rect, "Reactangle 100 m2");

If you want to, you can create individual 'viewports' for your drawing panel. 如果需要,可以为绘图面板创建单独的“视口”。

For this your Paint event needs to set up the Graphics object and can then draw into those 'ports'. 为此,您的Paint事件需要设置Graphics对象,然后可以绘制到这些“端口”中。

Here is a minimal example..: 这是一个最小的示例:

在此处输入图片说明

The Paint event draws the current ms into three different viewports: Paint事件将当前ms绘制到三个不同的视口中:

private void drawPanel1_Paint(object sender, PaintEventArgs e)
{
    foreach (var v in ViewPorts)
    {
        int i = ViewPorts.IndexOf(v);
        e.Graphics.ResetClip();
        e.Graphics.ResetTransform();

        e.Graphics.SetClip(v);
        e.Graphics.TranslateTransform(v.X, v.Y);
        e.Graphics.Clear(colors[i]);
        e.Graphics.DrawString(DateTime.Now.Millisecond + "' - " +i, 
                              Font, Brushes.Black, 0,0);
    }
}

Note how I always can draw to coordinate (0,0) ! 请注意,如何始终绘制坐标(0,0)

It uses a few variables: 它使用一些变量:

List<Rectangle> ViewPorts = new List<Rectangle>();
List<Color> colors = new List<Color>();

Here is how I set up the lists and the animation timer: 这是我设置列表和动画计时器的方法:

colors.Add(Color.LightCoral);
colors.Add(Color.LightCyan);
colors.Add(Color.LightGreen);
ViewPorts.Add(new Rectangle(  0,   0, 200, 250));
ViewPorts.Add(new Rectangle(100, 100, 100,  50));
ViewPorts.Add(new Rectangle(210, 200, 100,  50));

Timer t = new Timer();
t.Interval = 250;
t.Tick += (ss, ee) => { drawPanel1.Invalidate(); };
t.Start();

You will not use a Timer but draw by changing viewport specific data and calling Invalidate on the Panel whenever the data change .. 您将不使用Timer而是通过更改视口特定的数据在数据更改时调用Panel上的Invalidate进行绘制。

I have added a BackColor , which, just as ForeColor , Fonts and then some, should better be part of a ViewPort class; 我添加了一个BackColor ,就像ForeColorFonts以及某些一样,它最好是ViewPort类的一部分。 such a class could also hold a Draw method which you would call from the panel's Paint event, passing out the e.Graphics parameter.. 这样的类还可以包含Draw方法,您可以从面板的Paint事件中调用该方法,并传递e.Graphics参数。

If you don't want/need your output to persist , which may well be the case for tooltips, you can skip the Paint event; 如果您不希望/不需要保留输出,这可能是工具提示的情况,则可以跳过 Paint事件; for this case I would pass out the viewport, have a reference to the target panel in it and use CreateGraphics with pretty much the same code as above.. 在这种情况下,我将通过视口,在其中引用目标面板,并使用与上述代码几乎相同的CreateGraphics

I figured that the easiest way to make a "map" of ma panel is to create areas in MouseMove event , and if a mouse move over that area the label with text is created. 我认为制作ma面板“地图”的最简单方法是在MouseMove event创建区域,如果鼠标移过该区域,则会创建带有文本的标签。 For example one area has coords of 100,150,200,250 and it looks like this: 例如,一个区域的坐标为100,150,200,250,它看起来像这样:

 public void DrawingPanel2_MouseMove(object sender, MouseEventArgs e) 

    {
            if (e.X >= 100 && e.X <= 150 && e.Y >= 200 && e.Y <= 250)
                {
                    toolStripStatusLabel1.Text = "Point A";

                    Label lblA = new Label();
                    lblA.Text = "Point A";
                    lblA.Location = new System.Drawing.Point(e.X, e.Y);

                    lblA.AutoSize = true;
                    DrawingPanel2.Controls.Add(lblA);
                }

                else
                {
                    toolStripStatusLabel1.Text = e.X + "," + e.Y;
                    DrawingPanel2.Controls.Clear();
                }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM