我想作一个象OutlookXP的那种“我的快捷方式”的拖拽方式:把某一个Exe,或者快捷方式拖拽到面板上,就会形成相应的按钮,点击后就启动相应的应用程序!这种拖拽怎么做?????
解决方案 »
- 【分享】自己用C#做的一个qq截图的自动框选功能
- 利用operator声明且仅声明了“==”,有什么错误 么 ?
- 请问vs2008中,ajax母版页和母版页有什么区别?
- Windows窗体如何在订阅的其它类事件中对自己窗体中的Label内容进行改变.
- 请教:自定义控件,继承至TextBox, 怎样让 ToolStripTextBox中的TextBox = 自定义的TextBox呢
- net reactor 加密后的DLL中 **_nav.dll是干什么用的
- 谁能传给IEWebcontrol.exe给我,在线等,马上结贴
- 如何用excel制作工资单的问题?
- Window Service如何运用
- 问2个简单的问题
- 求盛大的面试经验
- 请问谁有压缩过web Service 的xml传输的成功方案
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;namespace 拖拽成按钮
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 285);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
this.AllowDrop = true;
this.p = new Point(1,1);
}
/// <summary>
/// 文件路径
/// </summary>
string strFileName;
Point p;
private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
String[] str_Drop=(String[])e.Data.GetData(DataFormats.FileDrop,true);
this.strFileName = str_Drop[0];
} private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
Button btn = new Button();
btn.Text = "快捷方式";
btn.Click +=new EventHandler(btn_Click);
this.Controls.Add(btn);
btn.Location = this.p;
}
private void btn_Click(object sender, System.EventArgs e)
{
ProcessStartInfo Info=new ProcessStartInfo();
try
{
Info.FileName = this.strFileName;;
Process.Start(Info);
}
catch(Win32Exception win32ex) {
MessageBox.Show("出错原因:"+win32ex.Message,"出错",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;namespace 拖拽成按钮
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 285);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
this.AllowDrop = true;
this.p = new Point(1,1);
}
/// <summary>
/// 文件路径
/// </summary>
string strFileName;
/// <summary>
/// btn的起始位置
/// </summary>
Point p;
//托拽文件或快捷方式进入窗体是记录它的路径
private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
String[] str_Drop=(String[])e.Data.GetData(DataFormats.FileDrop,true);
this.strFileName = str_Drop[0];
}
//推拽释放时生成按钮
private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
Button btn = new Button();
//保存文件的路径
btn.Tag = this.strFileName;
btn.Text =this.strFileName.Substring(this.strFileName.LastIndexOf(@"\"));
btn.Click +=new EventHandler(btn_Click);
btn.Location = this.p;
this.p.X += 100;
if(this.p.X > this.Width)
{
this.p.X = 1;
this.p.Y += 30;
}
this.Controls.Add(btn);
}
private void btn_Click(object sender, System.EventArgs e)
{
ProcessStartInfo Info=new ProcessStartInfo();
try
{
Info.FileName = ((Button)sender).Tag.ToString();
Process.Start(Info);
}
catch(Win32Exception win32ex) {
MessageBox.Show("出错原因:"+win32ex.Message,"出错",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}