如何让鼠标滑动的轨迹显示出来?急!

alicezc 2003-08-20 09:59:03
我想在窗口中鼠标按下后到鼠标放开前,所移动的轨迹即一个矩形框显示出来,到鼠标放开后,该轨迹就消失,如何实现呢?
非常感谢!
...全文
496 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangjs720 2003-08-20
  • 打赏
  • 举报
回复
[DllImport("user32.dll")]
public static extern bool SetCapture(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("gdi32.dll", EntryPoint="SetPixel")]
public static extern int SetPixel (
IntPtr hdc,
int x,
int y,
int crColor
);
[DllImport("user32.dll", EntryPoint="GetDC")]
public static extern IntPtr GetDC (
IntPtr hwnd
);
//-------------------------------------------------------
bool bCapturing = false;


private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(bCapturing)
{
Point pt = PointToScreen(new Point(e.X,e.Y));
IntPtr hDC = GetDC(IntPtr.Zero);
SetPixel(hDC,pt.X,pt.Y,255);
}
}

private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
bCapturing = false;
}

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
SetCapture(this.Handle);
bCapturing = true;
}
alicezc 2003-08-20
  • 打赏
  • 举报
回复
没有人知道么?请大帮帮忙!
万分感谢!
alicezc 2003-08-20
  • 打赏
  • 举报
回复
为什么没人回复呢?是不是我没说明白?
我的意思是如果大家在操作系统桌面上点击鼠标,向右下方移动鼠标,就出现一个虚线表示的矩形框,我想在自己的窗口中也做个类似的,如何实现呢?
非常非常感谢!
firejie 2003-08-20
  • 打赏
  • 举报
回复
mark
alicezc 2003-08-20
  • 打赏
  • 举报
回复
我试了,的确能实现。非常非常感谢大家。只可惜我设的分值太少了。请多包涵!
dahuzizyd 2003-08-20
  • 打赏
  • 举报
回复
这个地址:
http://www.c-sharpcorner.com/Code/2002/Mar/RubberBandLinesNShapes.asp
把他的原代码改动一下,就是你要的效果:
在Form1_Paint事件里:
Pen pen = new Pen(Color.Black);
在这一句的后面加上:
float[] dashValues = {1, 1, 1, 1};
pen.DashPattern = dashValues;
ok!
alicezc 2003-08-20
  • 打赏
  • 举报
回复
非常感谢,我先看看。
Jim3 2003-08-20
  • 打赏
  • 举报
回复
上面的那个类不是我写的,从树上抄的,在窗体的MouseMove,MouseDown,MouseUp加入一下代码即可


private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (rubberband != null)
rubberband.ResizeTo(new Point(e.X,e.Y));
}


private void Form1_MouseDown(object sender, MouseEventArgs e)
{
this.rubberband = new Rubberband(this,new Point(e.X,e.Y));

}

private void Form1_MouseUp(object sender,MouseEventArgs e)
{
if (rubberband != null)
{

rubberband.End();
rubberband = null;

}

}
Jim3 2003-08-20
  • 打赏
  • 举报
回复
public class Rubberband
{
protected Control parent;
protected Rectangle rect = Rectangle.Empty;

public Rubberband(Control theParent, Point theStartingPoint)
{
parent = theParent;
parent.Capture = true;
Cursor.Clip = parent.RectangleToScreen(parent.ClientRectangle);
rect = new Rectangle(theStartingPoint.X, theStartingPoint.Y, 0, 0);
}

public void End()
{
Cursor.Clip = Rectangle.Empty;
parent.Capture = false;

// erase the rubberband
Draw();
rect = Rectangle.Empty;
}

public void ResizeTo(Point thePoint)
{
// erase the old rubberband
Draw();

// get the new size of the rubberband
rect.Width = thePoint.X - rect.Left;
rect.Height = thePoint.Y - rect.Top;

// draw the new rubberband
Draw();
}

public Rectangle Bounds()
{
// return a normalized rectangle, i.e. a rect
// where (left <= right) and (top <= bottom)
if ( (rect.Left > rect.Right) || (rect.Top > rect.Bottom) )
{
int left = Math.Min(rect.Left, rect.Right);
int right = Math.Max(rect.Left, rect.Right);
int top = Math.Min(rect.Top, rect.Bottom);
int bottom = Math.Max(rect.Top, rect.Bottom);
return Rectangle.FromLTRB(left, top, right, bottom);
}
return rect;
}

// Reversible drawing method
// Calling theis method the first time draws the rubberband.
// Calling it a second time with the same rect erases the rubberband
protected void Draw()
{
Rectangle r = parent.RectangleToScreen(rect);
ControlPaint.DrawReversibleFrame(r, Color.White, FrameStyle.Dashed);
}
}
silverduck 2003-08-20
  • 打赏
  • 举报
回复
该程序从鼠标点击到放开,出现一个矩形框,放开时用黑色填充矩形
你可以适当的改这个程序,满足你的要求

//---------------------------------------
// BlockOut.cs ?2001 by Charles Petzold
//---------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class BlockOut: Form
{
bool bBlocking, bValidBox;
Point ptBeg, ptEnd;
Rectangle rectBox;

public static void Main()
{
Application.Run(new BlockOut());
}
public BlockOut()
{
Text = "Blockout Rectangle with Mouse";
BackColor = SystemColors.Window;
ForeColor = SystemColors.WindowText;
}
protected override void OnMouseDown(MouseEventArgs mea)
{
if (mea.Button == MouseButtons.Left)
{
ptBeg = ptEnd = new Point(mea.X, mea.Y);

Graphics grfx = CreateGraphics();
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
grfx.Dispose();

bBlocking = true;
}
}
protected override void OnMouseMove(MouseEventArgs mea)
{
if (bBlocking)
{
Graphics grfx = CreateGraphics();
grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
ptEnd = new Point(mea.X, mea.Y);
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
grfx.Dispose();
Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs mea)
{
if (bBlocking && mea.Button == MouseButtons.Left)
{
Graphics grfx = CreateGraphics();
rectBox = Rect(ptBeg, new Point(mea.X, mea.Y));
grfx.DrawRectangle(new Pen(ForeColor), rectBox);
grfx.Dispose();

bBlocking = false;
bValidBox = true;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;

if (bValidBox)
grfx.FillRectangle(new SolidBrush(ForeColor), rectBox);

if (bBlocking)
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
}
Rectangle Rect(Point ptBeg, Point ptEnd)
{
return new Rectangle(Math.Min(ptBeg.X, ptEnd.X),
Math.Min(ptBeg.Y, ptEnd.Y),
Math.Abs(ptEnd.X - ptBeg.X),
Math.Abs(ptEnd.Y - ptBeg.Y));
}
}
Jim3 2003-08-20
  • 打赏
  • 举报
回复
刚完成,等一下贴出来
jackie615 2003-08-20
  • 打赏
  • 举报
回复
我知道你要的是类似:
在windows桌面上按下鼠标拖动出现的虚线框

应该不难啊,处理方法跟上面画线的类似,改画框不就是
帮你UP
ekeen 2003-08-20
  • 打赏
  • 举报
回复
把line 设为dot line
wangjs720的方法可以
alicezc 2003-08-20
  • 打赏
  • 举报
回复
我试过了,是往窗口上画鼠标移动的轨迹。不是我想要的那种。
我想要的是:从鼠标点击到放开,出现一个虚线框,就象画图里面的选定功能。
有人知道么?能帮帮我么?感谢!
alicezc 2003-08-20
  • 打赏
  • 举报
回复
请问听雨舟,[DllImport("user32.dll")]应该放在C#程序的什么位置?怎么编译也通不过呀?谢谢。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧