首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

GDI+画图 并在Form窗口拖动的有关问题

2012-01-06 
GDI+画图 并在Form窗口拖动的问题刚学GDI+写了一段程序,在form窗口画个矩形,然后可以拖动.现在拖动是没有

GDI+画图 并在Form窗口拖动的问题
刚学GDI+写了一段程序,在form窗口画个矩形,然后可以拖动.现在拖动是没有问题的.就是拖动的时候不能太快.一旦快了,鼠标就划出矩形区域了.着个问题要怎么解决啊.以下是拖动代码.

  private   void   Form1_MouseMove(object   sender,   MouseEventArgs   e)
                {
                          //是否选中空间
                        if   (this.isSelect   ==   true)
                        {
                             
                                if   (e.Button   ==   MouseButtons.Left)
                                {
                                        if   (this.Rect.Contains(e.Location))
                                        {
                                                Point   point   =   e.Location;
                                                _mouseEnd   =   point;
                                                int   vix   =   _mouseEnd.X   -   _mouseStart.X;
                                                int   viy   =   _mouseEnd.Y   -   _mouseStart.Y;
                                                int   positionX   =   this.Location1.X   +   vix;
                                                int   positionY   =   this.Location1.Y   +   viy;
                                                Point   location   =   new   Point(positionX,   positionY);
                                                this.Location1   =   location;
                                                this.Invalidate(this.Rect);
                                                this.Refresh();
                                                _mouseStart   =   _mouseEnd;


                                                Pain(this.Graphics);
                                        }
                                }
                        }
                }

[解决办法]
设个bool 值 判断是拖动 还是划出矩形区域
[解决办法]
MouseMove也是每隔一段時間執行一次的,所以太快的話,MouseMove是反應不過來的。
[解决办法]
我有一个代码,楼主试试(VS2005的环境):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestApp
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private Rectangle m_Rect = Rectangle.Empty;
private Point m_StartPoint = Point.Empty;
private bool m_canMove;

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.m_Rect = new Rectangle(10, 10, 100, 80);
this.m_StartPoint = m_Rect.Location;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.drawRect(e.Graphics);
}
private void drawRect(Graphics graphics)
{
BufferedGraphicsContext context = BufferedGraphicsManager.Current;
BufferedGraphics bg = context.Allocate(graphics, this.ClientRectangle);
bg.Graphics.Clear(this.BackColor);
bg.Graphics.FillRectangle(SystemBrushes.ControlText, m_Rect);
bg.Render();
bg.Dispose();
bg = null;
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (this.m_Rect.Contains(e.Location))
{
m_canMove = true;
this.m_StartPoint = e.Location;
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
m_canMove = false;
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left && m_canMove)
{
this.m_Rect.Offset(e.X - this.m_StartPoint.X, e.Y - this.m_StartPoint.Y);
this.m_StartPoint = e.Location;
this.drawRect(this.CreateGraphics());
}
}
}
}
[解决办法]
应该在MouseDown里判断是否在矩形内,记录个变量,不要在MouseMove里判断鼠标的位置

热点排行