现在的位置: 首页 > 综合 > 正文

向窗体中拖放图片并显示

2012年01月27日 ⁄ 综合 ⁄ 共 3864字 ⁄ 字号 评论关闭

Frm_Main.cs

View Code

 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.IO;//添加的命名空间,对文件进行操作
10 using System.Threading;//线程序的命名空间
11
12 namespace DragImageToForm
13 {
14 public partial class Frm_Main : Form
15 {
16 public Frm_Main()
17 {
18 InitializeComponent();
19 }
20
21 public static bool Var_Style = true;
22 public static string tempstr="";
23
24 /// <summary>
25 /// 在窗体背景中显示被拖放的图片
26 /// </summary>
27 /// <param Frm="Form">窗体</param>
28 /// <param e="DragEventArgs">DragDrop、DragEnter 或 DragOver 事件提供数据</param>
29 public void SetDragImageToFrm(Form Frm, DragEventArgs e)
30 {
31 if (Var_Style == true)
32 {
33 e.Effect = DragDropEffects.Copy;//设置拖放操作中目标放置类型为复制
34 String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true);//检索数据格式相关联的数据
35 string tempstr;
36 Bitmap bkImage;//定义Bitmap变量
37 tempstr = str_Drop[0];//获取拖放文件的目录
38 try
39 {
40 bkImage = new Bitmap(tempstr);//存储拖放的图片
41 //根据图片设置窗体的大小
42 Frm.Size = new System.Drawing.Size(bkImage.Width + 6, bkImage.Height + 33);
43 Frm.BackgroundImage = bkImage;//在窗体背景中显示图片
44 }
45 catch { }
46 }
47 }
48
49 private void Frm_Main_DragEnter(object sender, DragEventArgs e)
50 {
51 SetDragImageToFrm(this, e);//在窗体中显示拖放到窗体上的图片
52 }
53 }
54 }

Frm_Main.designer.cs

View Code

 1 namespace DragImageToForm
2 {
3 partial class Frm_Main
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows 窗体设计器生成的代码
24
25 /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.components = new System.ComponentModel.Container();
32 this.panel_face = new System.Windows.Forms.Panel();
33 this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
34 this.Tool_Ima = new System.Windows.Forms.ToolStripMenuItem();
35 this.Tool_File = new System.Windows.Forms.ToolStripMenuItem();
36 this.contextMenuStrip1.SuspendLayout();
37 this.SuspendLayout();
38 //
39 // panel_face
40 //
41 this.panel_face.ContextMenuStrip = this.contextMenuStrip1;
42 this.panel_face.Dock = System.Windows.Forms.DockStyle.Fill;
43 this.panel_face.Location = new System.Drawing.Point(0, 0);
44 this.panel_face.Name = "panel_face";
45 this.panel_face.Size = new System.Drawing.Size(391, 238);
46 this.panel_face.TabIndex = 0;
47 this.panel_face.Visible = false;
48 //
49 // contextMenuStrip1
50 //
51 this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
52 this.Tool_Ima,
53 this.Tool_File});
54 this.contextMenuStrip1.Name = "contextMenuStrip2";
55 this.contextMenuStrip1.Size = new System.Drawing.Size(137, 48);
56 //
57 // Tool_Ima
58 //
59 this.Tool_Ima.Name = "Tool_Ima";
60 this.Tool_Ima.Size = new System.Drawing.Size(136, 22);
61 this.Tool_Ima.Tag = "1";
62 this.Tool_Ima.Text = "拖放图片";
63 //
64 // Tool_File
65 //
66 this.Tool_File.Name = "Tool_File";
67 this.Tool_File.Size = new System.Drawing.Size(136, 22);
68 this.Tool_File.Tag = "2";
69 this.Tool_File.Text = "拖放文件夹";
70 //
71 // Frm_Main
72 //
73 this.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
74 this.AllowDrop = true;
75 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
76 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
77 this.ClientSize = new System.Drawing.Size(391, 238);
78 this.ContextMenuStrip = this.contextMenuStrip1;
79 this.Controls.Add(this.panel_face);
80 this.Name = "Frm_Main";
81 this.Text = "向窗体中拖放图片并显示";
82 this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Frm_Main_DragEnter);
83 this.contextMenuStrip1.ResumeLayout(false);
84 this.ResumeLayout(false);
85
86 }
87
88 #endregion
89
90 private System.Windows.Forms.Panel panel_face;
91 private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
92 private System.Windows.Forms.ToolStripMenuItem Tool_Ima;
93 private System.Windows.Forms.ToolStripMenuItem Tool_File;
94
95 }
96 }

抱歉!评论已关闭.