zoukankan      html  css  js  c++  java
  • 应用程序实现文件拖拉

    本来没准备发这个基本的东西的.但有好多网友问这个问题,就写了个例子.以此抛砖引玉.

    一个图片浏览器,可以实现在程序运行当中,直接拉入图片文件到程序窗口中打开.也可以在未打开的程序,将文件直接拖到程序图标上打开该图片文件.

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Data;

    namespace AllowDragFormTest
    {
     /// <summary>
     /// 图片浏览器
    ///程序:房客
    ///网址:http://www.cnblogs.com/sxlfybb/
    ///转载请保留,谢谢
     /// </summary>
     public class Form1 : System.Windows.Forms.Form
     {
      /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      private System.ComponentModel.Container components = null;
      private System.Windows.Forms.MainMenu mainMenu1;
      private System.Windows.Forms.MenuItem menuItem1;
      private System.Windows.Forms.MenuItem menuItem2;
      private System.Windows.Forms.MenuItem menuItem3;
      private System.Windows.Forms.MenuItem menuItem4;
      private System.Windows.Forms.OpenFileDialog openFileDialog1;
      private string strFilePath = string.Empty;
      private System.Windows.Forms.MenuItem menuItem5;
      private System.Windows.Forms.MenuItem menuItem6;
      private Image image = null;

      public Form1(string str)
      {
       //
       // Windows 窗体设计器支持所必需的
       //
       InitializeComponent();

       this.strFilePath = str;
       SetPicToBground();

       //
       // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
       //
      }

      public Form1()
      {
       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()
      {
       this.mainMenu1 = new System.Windows.Forms.MainMenu();
       this.menuItem1 = new System.Windows.Forms.MenuItem();
       this.menuItem2 = new System.Windows.Forms.MenuItem();
       this.menuItem3 = new System.Windows.Forms.MenuItem();
       this.menuItem4 = new System.Windows.Forms.MenuItem();
       this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
       this.menuItem5 = new System.Windows.Forms.MenuItem();
       this.menuItem6 = new System.Windows.Forms.MenuItem();
       //
       // mainMenu1
       //
       this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                           this.menuItem1,
                           this.menuItem5});
       //
       // menuItem1
       //
       this.menuItem1.Index = 0;
       this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                           this.menuItem2,
                           this.menuItem3,
                           this.menuItem4});
       this.menuItem1.Text = "文件(&F)";
       //
       // menuItem2
       //
       this.menuItem2.Index = 0;
       this.menuItem2.Text = "打开(&O)";
       this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
       //
       // menuItem3
       //
       this.menuItem3.Index = 1;
       this.menuItem3.Text = "-";
       //
       // menuItem4
       //
       this.menuItem4.Index = 2;
       this.menuItem4.Text = "退出(&X)";
       this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
       //
       // menuItem5
       //
       this.menuItem5.Index = 1;
       this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                           this.menuItem6});
       this.menuItem5.Text = "帮助(&H)";
       //
       // menuItem6
       //
       this.menuItem6.Index = 0;
       this.menuItem6.Text = "关于(&A)";
       this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
       //
       // Form1
       //
       this.AllowDrop = true;
       this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
       this.ClientSize = new System.Drawing.Size(296, 245);
       this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
       this.MaximizeBox = false;
       this.Menu = this.mainMenu1;
       this.Name = "Form1";
       this.Text = "图片浏览器";

      }
      #endregion

      /// <summary>
      /// 应用程序的主入口点。
       ///直接拖拉文件到程序的图标上
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
       if( args.Length >0 )
       {
        //如果是拉入文件打开的程序
        Application.Run(new Form1(args[0]));
       }
       else
       //直接打开程序
        Application.Run(new Form1());
      }

      private void menuItem2_Click(object sender, System.EventArgs e)
      {
       openFileDialog1.Filter = "图像文件|*.jpg;*.jpeg;*.bmp;*.gif";
       if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
       {
        this.strFilePath = openFileDialog1.FileName;
        SetPicToBground();
       }
      }

      private void SetPicToBground()
      {
       try
       {
        FileInfo file = new FileInfo(strFilePath);
        if (file.Exists)
        {
         if (image != null)
          image = null;
         image = Image.FromFile(strFilePath,true);
          this.Width = image.Width;
          this.Height = image.Height;
          this.BackgroundImage = image;
        }
       }
       catch( Exception ex)
       {
        MessageBox.Show(ex.Message);
       }
      }

    //正在运行的程序中,接收拖拉入的文件.

      protected override void OnDragEnter(DragEventArgs e)
      {
       if( e.Data.GetDataPresent(DataFormats.Bitmap,true) || e.Data.GetDataPresent(DataFormats.FileDrop))
       {
        e.Effect = DragDropEffects.Link;
       }
       else
       {
        e.Effect = DragDropEffects.None;
       }
      }

      protected override void OnDragDrop(DragEventArgs e)
      {
       if( e.Data.GetDataPresent(DataFormats.FileDrop) )
       {
        string[] strs = (string[])e.Data.GetData(DataFormats.FileDrop);
        if( strs.Length >0 )
        {
         this.strFilePath = strs[0];
         SetPicToBground();
        }

       }
      }

      private void menuItem4_Click(object sender, System.EventArgs e)
      {
       Application.Exit();
      }

      private void menuItem6_Click(object sender, System.EventArgs e)
      {
       string  str = "图片浏览器 Ver1.0" ;
       str += System.Environment.NewLine;
       str += System.Environment.NewLine;
       str += "程序:房客 " + "\t" + "QQ:83849123";

       MessageBox.Show( this,str,"信息",MessageBoxButtons.OK,MessageBoxIcon.Information );

      }
     }
    }

  • 相关阅读:
    FlasCC发布说明
    FlasCC例子研究之Animation
    FlasCC例子研究之HelloWorld
    FlasCC例子研究之Drawing补充
    FlasCC Windows下开发环境搭建
    关于FlasCC(Adobe Flash C/C++ Compiler)
    FlasCC例子研究之c++interop
    魔兽世界客户端数据研究(四):M2文件头分析
    魔兽世界客户端数据研究(二)
    魔兽世界客户端数据研究(三)
  • 原文地址:https://www.cnblogs.com/sxlfybb/p/381522.html
Copyright © 2011-2022 走看看