突出的矩形区域的同时拖动它 [英] Highlight the Rectangular Area while Dragging it

查看:131
本文介绍了突出的矩形区域的同时拖动它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个图像浏览器类型的应用程序。我在Windows上使用的.Net

I am creating a image viewer sort of application. I am on Windows and using .Net

在我的应用程序,我想突出显示特定区域的同时拖动。
我已经创建了一个长方形

In my app, I am trying to highlight a Particular area while dragging. I have created a Rectangle.

Rectangle areaRect = new Rectangle(100,100, 300, 300);
Point ptOld = new Point(0, 0);
Pen rectPen = new Pen(Brushes.White, 3);

protected override void OnPaint(PaintEventArgs e)
{
  Graphics dcPaint = e.Graphics;
  dcPaint.DrawRectangle(rectPen, areaRect);
}

现在我拖着我的鼠标移动沿着这个矩形区域。

Now I am dragging this rectangular area along with my mouse movements.

protected override void OnMouseMove(MouseEventArgs e)
{
 Point ptNew = new Point(e.X, e.Y);
 int dx = ptNew.X - ptOld.X;
 int dy = ptNew.Y - ptOld.Y;
 areaRect.Offset(dx, dy);
 MoveRect(ptNew);
 ptOld = ptNew;
}



在这里,我想用我的鼠标一起移动这个矩形

Here I am trying to move this rect along with my mouse

void MoveRect(Point point)
{
 Graphics grfxClient = CreateGraphics();
 Rectangle tempRectangle = new Rectangle(areaRect.Left, areaRect.Top, areaRect.Width,   areaRect.Height);
 grfxClient.DrawRectangle(rectPen, tempRectangle);
 this.Invalidate();
 grfxClient.Dispose();
}



我的代码,直到这点是工作的罚款。
现在我想变暗逆阻力区,我的意思是这是此矩形内拖动时应该被突出的区域(也就是阻力区域以外的区域)。

My Code till this point is working fine. Now I would like to darken the INVERSE drag Area (The area which is outside the drag region), I mean the area which is within this Rectangle should gets highlighted while dragging.

不知道如何继续。

感谢。

-Pankaj

推荐答案

我想你可以通过创建覆盖的外部的地区对象做矩形,半透明 SolidBrush 填充它,使它看起来昏暗了。

I suppose you can do it by creating a Region object that covers the outside of the rectangle and fill it with a semi-transparent SolidBrush to make it look darkened.

您还没有创建一个图形,并在的OnMouseMove 画事件,而只是转移矩形和您绘制在控制表面失效。

You also don't have to create a graphics and draw in OnMouseMove event, but just shift the rectangle and invalidate the surface of the control you are drawing on.

我以前看起来或多或少像这样的代码:

The code I used looks more or less like this:

Rectangle areaRect = new Rectangle(100,100, 300, 300);
Point ptOld = new Point(0, 0);
Pen rectPen = new Pen(Brushes.White, 3);

//A new field with a semi-transparent brush to paint the outside of the rectangle
Brush dimmingBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 0));

protected override void OnPaint(PaintEventArgs e)
{
    Region outsideRegion = new System.Drawing.Region(e.ClipRectangle);
    outsideRegion.Exclude(areaRect);
    Graphics dcPaint = e.Graphics;
    dcPaint.FillRegion(dimmingBrush, outsideRegion);
    dcPaint.DrawRectangle(rectPen, areaRect);
}

protected override void OnMouseMove(MouseEventArgs e)
{
    Point ptNew = new Point(e.X, e.Y);
    int dx = ptNew.X - ptOld.X;
    int dy = ptNew.Y - ptOld.Y;
    areaRect.Offset(dx, dy);
    ptOld = ptNew;
    this.Invalidate();
}



命名的方法 MoveRect 是不必要的。

现在看来工作,因为你想它。

It now seems to work as you wanted it to.

建议

我也有一些建议。你不必使用它们,也许他们将是对你有所帮助。

I also have some suggestions. You don't have to use them, maybe they will be helpful for you.

您还没有写你使用什么样的控制上绘制(或重写表格方法和绘画,直接就可以了),但我建议你使用图片框控制,创造派生的自定义控制从它并覆盖其事件。这应该使绘画过程平稳,防止闪烁。要做到这一点是这样的:

You haven't written what kind of control you are using to draw on (or overriding Form methods and painting directly on it), but I suggest you to use a PictureBox control, create a custom control derived from it and override its events. This should make the painting process smooth and prevent flickering. To do it this way:


  • 创建通过选择新的用户控件的添加的和的用户控制。 .. 的命名一个新的控制即的MyPictureBox

  • 更改父类的控制,因此它现在应该包含该行:

  • Create a new user control by selecting Add and User Control... and name a new control i.e. MyPictureBox
  • change the parent class of the control, so it should now contain the line:

public partial class MyPictureBox : PictureBox


  • 打开文件的 MyPictureBox.Designer.cs 的并注释掉这些行:

    //this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    


  • 复制我张贴在这个答案的代码,并添加行基地.OnPaint(E); 和年初的OnPaint

    编译项目

    现在你应该可以打开你的主窗体设计器,拖的MyPictureBox 控制从工具箱中,使用它而不需要额外的代码

    now you should be able to open designer of your main form, drag MyPictureBox control from the toolbox and use it without additional code needed

    您也可能会考虑更改高亮区域的行为,因此,鼠标光标在它的中心。我想这将是更直观的用户。

    You also might consider changing the behaviour of the highlighted area, so mouse cursor was in the center of it. I suppose it would be more intuitive to the user.

    如果您有任何代码的问题,只是把它写在评论,我会尽力帮助:)

    If you have any issues with the code, just write it in the comments and I'll try to help :).

    这篇关于突出的矩形区域的同时拖动它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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