社区
C#
帖子详情
如何拖动一个控件?
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);
}
}
控件
拖动
拖拽效果 拖拽
控件
c# 拖拽效果 拖拽
控件
c# 拖拽效果 拖拽
控件
c# 拖拽效果 拖拽
控件
c# 拖拽效果 拖拽
控件
wpf
控件
拖动
,
控件
换位置
wpf界面上
控件
拖动
,以及
控件
之间的位置对换
labVIEW 实现树形
控件
的值拖拽的另
一个
树形
控件
中,任意形成节点
labVIEW 实现树形
控件
的值拖拽的另
一个
树形
控件
中,任意形成节点
C#多种
控件
的拖拽示例(源码)
C#多种
控件
的拖拽示例 包含了多种
控件
的拖拽示例的源码 有典型的TreeView,ListView,DataGridView ...
Winform ToolStrip
控件
左右拖拽移动效果实现
* 增加 添加、删除 按钮,点击时可增删
一个
ToolStripButton,方便
拖动
效果可见(ToolStrip
控件
中子
控件
超出屏幕) 与不可见(ToolStrip
控件
中子
控件
可以在屏幕上完全显示时)的演示。 *
拖动
鼠标离开ToolStrip...
C#
110,567
社区成员
642,567
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章