如何拖动一个控件?

csengine 2003-02-25 11:31:20
请有代码实现.
...全文
38 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bob 2003-02-26
  • 打赏
  • 举报
回复
//这种是把一个外部的东西拖到程序的
//这个可以把一个图片拖到窗口显示

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

class ImageDrop: Form
{
bool bIsTarget;
protected string strProgName;
protected string strFileName;
protected Image image;

public static void Main()
{
Application.Run(new ImageDrop());
}
public ImageDrop()
{
Text = strProgName = "Image Drop";

ResizeRedraw = true;
AllowDrop = true;
}
protected override void OnDragOver(DragEventArgs dea)
{
if (dea.Data.GetDataPresent(DataFormats.FileDrop) ||
dea.Data.GetDataPresent(typeof(Metafile)) ||
dea.Data.GetDataPresent(typeof(Bitmap)))
{
if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
dea.Effect = DragDropEffects.Move;

if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) &&
((dea.KeyState & 0x08) != 0)) // Ctrl key
dea.Effect = DragDropEffects.Copy;
}
}
protected override void OnDragDrop(DragEventArgs dea)
{
if (dea.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] astr = (string[])
dea.Data.GetData(DataFormats.FileDrop);
try
{
image = Image.FromFile(astr[0]);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, Text);
return;
}
strFileName = astr[0];
Text = strProgName + " - " + Path.GetFileName(strFileName);
Invalidate();
}
else
{
if (dea.Data.GetDataPresent(typeof(Metafile)))
image = (Image) dea.Data.GetData(typeof(Metafile));

else if (dea.Data.GetDataPresent(typeof(Bitmap)))
image = (Image) dea.Data.GetData(typeof(Bitmap));

bIsTarget = true;
strFileName = "DragAndDrop";
Text = strProgName + " - " + strFileName;
Invalidate();
}
}
protected override void OnMouseDown(MouseEventArgs mea)
{
if (image != null)
{
bIsTarget = false;

DragDropEffects dde = DoDragDrop(image,
DragDropEffects.Copy | DragDropEffects.Move);

if (dde == DragDropEffects.Move && !bIsTarget)
image = null;
}
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;

if (image != null)
grfx.DrawImage(image, 0, 0);
}
}
aaxu 2003-02-26
  • 打赏
  • 举报
回复
UP
TheAres 2003-02-25
  • 打赏
  • 举报
回复
To csengine (C#引擎):
简单的拖动这样就可以实现.

private int x,y,x1,y1;
private bool BeginDrag = false;

private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
x = e.X;
y = e.Y;
BeginDrag = true;
}

private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
if(BeginDrag){
button1.Left += e.X - x;
button1.Top += e.Y - y;
BeginDrag = false;
}
}

private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
if(BeginDrag){
x1 = button1.Left + e.X - x;
y1 = button1.Top + e.Y - y;
Invalidate();
}
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
if(BeginDrag){
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
e.Graphics.DrawRectangle(myPen,x1,y1,button1.Width,button1.Height);
}
}

110,567

社区成员

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

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

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