我的问题是,即使我想在panel_MouseMove()上移动绘制的矩形,所有这些矩形都会闪烁。我不知道为什么。这是我的面板代码_MouseMove方法:
private void panel_MouseMove(object sender, MouseEventArgs e)
{
bool flag = false;
if (_guiParserMgr != null)
{
if (e.Button == MouseButtons.Left)
{
foreach (GuiModulModel gmm in _guiParserMgr.Moduls)
{
log.Debug("Modul: XPos:" + gmm.getXPos() + " YPos:" + gmm.getYPos() + " WidthPos:" + ((int)gmm.getXPos() + (int)gmm.getWidth()) + " HeightPos:" + ((int)gmm.getYPos() + (int)gmm.getHeight()));
if (flag == false)
{
foreach (GuiEntityModel gem in gmm.Entitys)
{
log.Debug("Entity: XPos:" + gem.getXPos() + " YPos:" + gem.getYPos() + " WidthPos:" + ((int)gem.getXPos() + (int)gem.getWidth()) + " HeightPos:" + ((int)gem.getYPos() + (int)gem.getHeight()));
if (gem.getXPos() < e.X && gem.getXPos() + gem.getWidth() > e.X && gem.getYPos() < e.X && gem.getYPos() + gem.getHeight() > e.Y)
{
// Increment rectangle-location by mouse-location delta.
gem.setXPos(e.X /*+ _firstPoint.X*/);
gem.setYPos(e.Y /*+ _firstPoint.Y*/);
// Re-calibrate on each move operation.
_firstPoint = new MovePoint { X = e.X, Y = e.Y };
//panel.Invalidate();
foreach (GuiFieldModel gfm in gem.Fields)
{
// Increment rectangle-location by mouse-location delta.
gfm.setXPos(e.X/*+ _firstPoint.X*/);
gfm.setYPos(e.Y + 15/*+ _firstPoint.Y*/);
// Re-calibrate on each move operation.
_firstPoint = new MovePoint { X = e.X, Y = e.Y };
//panel.Invalidate();
}
flag = true;
}
}
}
if (flag == false)
{
foreach (GuiEnumTypeModel getm in gmm.EnumTypes)
{
log.Debug("EnumType: XPos:" + getm.getXPos() + " YPos:" + getm.getYPos() + " WidthPos:" + ((int)getm.getXPos() + (int)getm.getWidth()) + " HeightPos:" + ((int)getm.getYPos() + (int)getm.getHeight()));
if (getm.getXPos() < e.X && getm.getXPos() + getm.getWidth() > e.X && getm.getYPos() < e.X && getm.getYPos() + getm.getHeight() > e.Y)
{
// Increment rectangle-location by mouse-location delta.
getm.setXPos(e.X /*+ _firstPoint.X*/);
getm.setYPos(e.Y /*+ _firstPoint.Y*/);
// Re-calibrate on each move operation.
_firstPoint = new MovePoint { X = e.X, Y = e.Y };
//panel.Invalidate();
flag = true;
}
}
}
if (flag == false)
{
foreach (GuiServiceModel gsm in gmm.Services)
{
log.Debug("Service: XPos:" + gsm.getXPos() + " YPos:" + gsm.getYPos() + " WidthPos:" + ((int)gsm.getXPos() + (int)gsm.getWidth()) + " HeightPos:" + ((int)gsm.getYPos() + (int)gsm.getHeight()));
if (gsm.getXPos() < e.X && gsm.getXPos() + gsm.getWidth() > e.X && gsm.getYPos() < e.X && gsm.getYPos() + gsm.getHeight() > e.Y)
{
// Increment rectangle-location by mouse-location delta.
gsm.setXPos(e.X /*+ _firstPoint.X*/);
gsm.setYPos(e.Y /*+ _firstPoint.Y*/);
// Re-calibrate on each move operation.
_firstPoint = new MovePoint { X = e.X, Y = e.Y };
//panel.Invalidate();
flag = true;
}
}
}
if (flag == false)
{
foreach (GuiWebMvcModel gwm in gmm.WebMvcs)
{
log.Debug("WebMVC: XPos:" + gwm.getXPos() + " YPos:" + gwm.getYPos() + " WidthPos:" + ((int)gwm.getXPos() + (int)gwm.getWidth()) + " HeightPos:" + ((int)gwm.getYPos() + (int)gwm.getHeight()));
if (gwm.getXPos() < e.X && gwm.getXPos() + gwm.getWidth() > e.X && gwm.getYPos() < e.X && gwm.getYPos() + gwm.getHeight() > e.Y)
{
// Increment rectangle-location by mouse-location delta.
gwm.setXPos(e.X /*+ _firstPoint.X*/);
gwm.setYPos(e.Y /*+ _firstPoint.Y*/);
// Re-calibrate on each move operation.
_firstPoint = new MovePoint { X = e.X, Y = e.Y };
//panel.Invalidate();
flag = true;
}
}
}
if (flag == false)
{
if (gmm.getXPos() < e.X && gmm.getXPos() + gmm.getWidth() > e.X && gmm.getYPos() < e.X && gmm.getYPos() + gmm.getHeight() > e.Y)
{
// Increment rectangle-location by mouse-location delta.
gmm.setXPos(e.X /*+ _firstPoint.X*/);
gmm.setYPos(e.Y /*+ _firstPoint.Y*/);
// Re-calibrate on each move operation.
_firstPoint = new MovePoint { X = e.X, Y = e.Y };
//panel.Invalidate();
flag = true;
}
}
panel.Invalidate();
}
}
}
}
请帮帮我!:D谢谢:)
我的示例代码适用于PictureBox,但也适用于其他控件和形状。它工作时不会闪烁。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace BigBrother
{
public class SizeablePictureBox : PictureBox
{
private const int grab = 16;
public Point PBstartLocation;
public Point PBcornerStart;
public SizeablePictureBox()
{
this.ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var rc = new Rectangle(this.ClientSize.Width - grab, this.ClientSize.Height - grab, grab, grab);
ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x84)
{
var pos = this.PointToClient(new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16));
if (pos.X >= this.ClientSize.Width - grab && pos.Y >= this.ClientSize.Height - grab)
m.Result = new IntPtr(17);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PBstartLocation = e.Location;
PBcornerStart = this.Location;
this.BringToFront();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Left = e.X + this.Left - PBstartLocation.X;
this.Top = e.Y + this.Top - PBstartLocation.Y;
}
}
}
更好的例子,来自评论中的链接:https://mega.co.nz/#!cghECSLZ!KVzfbzksPMuonBVTsRok1Milz1cbbGW9bJEmTQQnsY