catt_231
October 6th, 2003, 07:12 AM
Hi, I am trying to create a shortcut manager using visual studio.
I would like to be able to drag icons from my windows desktop into a windows form app which would then create an icon in the application.
Using drag and drop at the moment I can copy an icon or graphic and get them to display but when I try to drag on a shortcut I get an "Out of Memory" error...
Any help would be great!!!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ShortcutManager
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private Image picture;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.ListBox listBox1;
private Point pictureLocation;
/// <summary>
/// Required designer variable.
/// </summary>
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "Shortcut Manager";
this.notifyIcon1.Visible = true;
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Exit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(384, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(176, 277);
this.listBox1.TabIndex = 1;
//
// Form1
//
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 277);
this.Controls.Add(this.listBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Shortcut Manager";
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);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
//If there is an image and it has a location paint it when the form is repainted
base.OnPaint(e);
if (this.picture !=null && this.pictureLocation !=Point.Empty)
{
e.Graphics.DrawImage(this.picture, this.pictureLocation);
}
}
private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Minimized;
}
this.Activate();
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
// FileDrop Data Handling
if(e.Data.GetDataPresent("FileDrop",true))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
try
{
this.picture = Image.FromFile(files[0]);
this.pictureLocation = this.PointToClient(new Point(e.X,e.Y));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
//Bitmap Data Handling
if(e.Data.GetDataPresent(DataFormats.Bitmap))
{
try
{
this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
this.pictureLocation = this.PointToClient(new Point(e.X,e.Y));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
this.Invalidate();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
listBox1.Items.AddRange(e.Data.GetFormats());
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}
I would like to be able to drag icons from my windows desktop into a windows form app which would then create an icon in the application.
Using drag and drop at the moment I can copy an icon or graphic and get them to display but when I try to drag on a shortcut I get an "Out of Memory" error...
Any help would be great!!!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ShortcutManager
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private Image picture;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.ListBox listBox1;
private Point pictureLocation;
/// <summary>
/// Required designer variable.
/// </summary>
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "Shortcut Manager";
this.notifyIcon1.Visible = true;
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Exit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(384, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(176, 277);
this.listBox1.TabIndex = 1;
//
// Form1
//
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 277);
this.Controls.Add(this.listBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Shortcut Manager";
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);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
//If there is an image and it has a location paint it when the form is repainted
base.OnPaint(e);
if (this.picture !=null && this.pictureLocation !=Point.Empty)
{
e.Graphics.DrawImage(this.picture, this.pictureLocation);
}
}
private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Minimized;
}
this.Activate();
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
// FileDrop Data Handling
if(e.Data.GetDataPresent("FileDrop",true))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
try
{
this.picture = Image.FromFile(files[0]);
this.pictureLocation = this.PointToClient(new Point(e.X,e.Y));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
//Bitmap Data Handling
if(e.Data.GetDataPresent(DataFormats.Bitmap))
{
try
{
this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
this.pictureLocation = this.PointToClient(new Point(e.X,e.Y));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
this.Invalidate();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
listBox1.Items.AddRange(e.Data.GetFormats());
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}