我想作一个象OutlookXP的那种“我的快捷方式”的拖拽方式:把某一个Exe,或者快捷方式拖拽到面板上,就会形成相应的按钮,点击后就启动相应的应用程序!这种拖拽怎么做?????

解决方案 »

  1.   

    gz实现按钮启动程序比较简单,关键如何实现获取拖入的exe的地址
      

  2.   

    简单的实现托放一个文件或快捷方式:
    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);
    }
    }             
    }
    }
      

  3.   

    来个完整的吧^_^
    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);
    }
    }             
    }
    }