Click here to Skip to main content
15,897,273 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to retrieve image from local resource ? Pin
Luc Pattyn21-Feb-09 2:21
sitebuilderLuc Pattyn21-Feb-09 2:21 
GeneralRe: How to retrieve image from local resource ? Pin
hdv21221-Feb-09 2:54
hdv21221-Feb-09 2:54 
AnswerRe: How to retrieve image from local resource ? Pin
Anthony Mushrow21-Feb-09 3:03
professionalAnthony Mushrow21-Feb-09 3:03 
GeneralRe: How to retrieve image from local resource ? Pin
hdv21221-Feb-09 4:44
hdv21221-Feb-09 4:44 
QuestionPlace Form windows at certain position in MDI Application Pin
hardsoft21-Feb-09 1:48
hardsoft21-Feb-09 1:48 
AnswerRe: Place Form windows at certain position in MDI Application Pin
paas21-Feb-09 5:28
paas21-Feb-09 5:28 
QuestionHow determine redirect url Pin
Meysam Mahfouzi21-Feb-09 1:31
Meysam Mahfouzi21-Feb-09 1:31 
QuestionGenerate WM_DROPFILES and send it to a Control Pin
steffen_dec21-Feb-09 0:24
steffen_dec21-Feb-09 0:24 
Hey,

i want to generate a WM_DROPFILES Message and send it to a Control on my Form.

i found one sample in c++
http://groups.google.de/group/microsoft.public.platformsdk.shell/browse_frm/thread/5bbd1d67913ac2c7?hl=de&lr=&rnum=2&prev=/groups%3Fq%3Dsendmessage%2Bwm_dropfiles%26hl%3Dde%26lr%3D%26selm%3D03c301c3973d%25240433f8e0%2524a101280a%2540phx.gbl%26rnum%3D2

does anybody know how it sounds right in C# .Net 2.0?

here my trying, which doesnt work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace DropFile
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(341, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(142, 40);
            this.button1.TabIndex = 0;
            this.button1.Text = "Load Image";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(860, 546);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
            this.DragOver += new System.Windows.Forms.DragEventHandler(this.Form1_DragOver);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
                if (FileList.Length == 1)
                {
                    FileInfo fi = new FileInfo(FileList[0]);
                    if (fi.Extension == ".gif" | fi.Extension == ".bmp" | fi.Extension == ".jpg" | fi.Extension == ".jpeg")
                    {
                        e.Effect = DragDropEffects.Move;
                    }
                }
            }
            else
                e.Effect = DragDropEffects.None;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
                if (FileList.Length == 1)
                {
                    FileInfo fi = new FileInfo(FileList[0]);
                    if (fi.Extension == ".gif" | fi.Extension == ".bmp" | fi.Extension == ".jpg" | fi.Extension == ".jpeg")
                    {
                        this.BackgroundImage = Image.FromFile(FileList[0]);
                    }
                }
            }
            else
                e.Effect = DragDropEffects.None;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        //[return: MarshalAs(UnmanagedType.Bool)]
        //[DllImport("user32.dll", SetLastError = true)]
        //static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        //[DllImport("kernel32.dll")]
        //static extern IntPtr GlobalAlloc(uint uFlags, int dwBytes);
        //[DllImport("kernel32.dll")]
        //static extern IntPtr GlobalLock(IntPtr hMem);
        //[DllImport("kernel32.dll")]
        //[return: MarshalAs(UnmanagedType.Bool)]
        //static extern bool GlobalUnlock(IntPtr hMem);
        //[DllImport("kernel32.dll")]
        //static extern IntPtr GlobalFree(IntPtr hMem);

        public const uint GMEM_FIXED = 0x0000;
        public const uint GMEM_SHARE = 0x2000;
        public const uint WM_DROPFILES = 0x0233;

        //public struct POINT
        //{
        //    public int x, y;
        //    public POINT(int p1, int p2)
        //    {
        //        x = p1;
        //        y = p2;
        //    }
        //}

        //public struct DROPFILES
        //{
        //    public IntPtr pFiles;
        //    public POINT pt;
        //    public bool fNC;
        //    public bool fWide;
        //}

        //TEST
        [DllImport("Kernel32.dll", SetLastError = true)]
        public static extern int GlobalLock(IntPtr Handle);

        [DllImport("Kernel32.dll", SetLastError = true)]
        public static extern int GlobalUnlock(IntPtr Handle);

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
           IntPtr lParam);

        [Serializable]
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public Int32 X;
            public Int32 Y;

            public POINT(Int32 x, Int32 y)
            {
                this.X = x;
                this.Y = y;
            }
        }
        [Serializable]
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        class DROPFILES
        {
            public int size;    //<-- offset to filelist (this should be defined 20)
            public POINT pt;    //<-- where we "release the mouse button"
            public bool fND;    //<-- the point origins (0;0) (this should be false, if true, the origin will be the screens (0;0), else, the handle the the window we send in PostMessage)
            public bool WIDE;   //<-- ANSI or Unicode (should be false)
        }

        public static byte[] RawSerialize(object anything)
        {
            int rawsize = Marshal.SizeOf(anything);
            IntPtr buffer = Marshal.AllocHGlobal(rawsize);
            Marshal.StructureToPtr(anything, buffer, false);
            byte[] rawdatas = new byte[rawsize];
            Marshal.Copy(buffer, rawdatas, 0, rawsize);
            Marshal.FreeHGlobal(buffer);
            return rawdatas;
        }

        protected override void WndProc(ref Message m)
        {

            if (m.Msg == WM_DROPFILES)
            {
                bool bTest = true;

            }

            base.WndProc(ref m);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr hwnd = this.Handle;
            DROPFILES s = new DROPFILES();
            s.size = 20;                            //<-- 20 is the size of this struct in memory
            s.pt = new POINT(10, 10);    //<-- drop file 20 pixels from left, total height minus 40 from top
            s.fND = false;                          //<-- the point 0;0 will be in the window
            s.WIDE = false;                         //<-- ANSI

            string file = "D:\\test.bmp\0";         //<-- add null terminator at end
            Int32 filelen = Convert.ToInt32(file.Length);
            byte[] bytes = RawSerialize(s);
            int structlen = (int)bytes.Length;
            int size = structlen + filelen + 1;
            IntPtr p = Marshal.AllocHGlobal(size);  //<-- allocate memory and save pointer to p
            GlobalLock(p);                          //<-- lock p

            int i = 0;
            for (i = 0; i < structlen; i++)
            {
                Marshal.WriteByte(p, i, bytes[i]);
                Console.WriteLine("Wrote header byte " + i.ToString() + " of " + size.ToString());
            }
            byte[] b = ASCIIEncoding.ASCII.GetBytes(file); //<-- convert filepath to bytearray
            for (int k = 0; k < filelen; k++)
            {
                Marshal.WriteByte(p, i, b[k]);
                Console.WriteLine("Wrote filename byte " + i.ToString() + " of " + size.ToString());
                i++;
            }

            Marshal.WriteByte(p, i, 0);

            GlobalUnlock(p);
            PostMessage(hwnd, WM_DROPFILES, p, IntPtr.Zero);
            Marshal.FreeHGlobal(p);
        }
        
        
    }
}


thanks a lot

Steffen
AnswerRe: Generate WM_DROPFILES and send it to a Control Pin
pipicato18-Oct-11 23:48
pipicato18-Oct-11 23:48 
AnswerRe: Generate WM_DROPFILES and send it to a Control Pin
davidcole30-Sep-13 5:59
davidcole30-Sep-13 5:59 
QuestionEncoding issue Pin
George_George21-Feb-09 0:19
George_George21-Feb-09 0:19 
QuestionSome Question Pin
E_Gold20-Feb-09 23:28
E_Gold20-Feb-09 23:28 
AnswerRe: Some Question Pin
kevinuni17-Oct-16 11:05
kevinuni17-Oct-16 11:05 
GeneralRe: Some Question Pin
kevinuni17-Oct-16 11:08
kevinuni17-Oct-16 11:08 
QuestionClass Pin
mrithula820-Feb-09 22:49
mrithula820-Feb-09 22:49 
AnswerRe: Class Pin
harold aptroot20-Feb-09 22:59
harold aptroot20-Feb-09 22:59 
GeneralRe: Class Pin
Zoltan Balazs21-Feb-09 4:52
Zoltan Balazs21-Feb-09 4:52 
GeneralRe: Class Pin
harold aptroot21-Feb-09 4:58
harold aptroot21-Feb-09 4:58 
GeneralRe: Class Pin
Zoltan Balazs21-Feb-09 5:08
Zoltan Balazs21-Feb-09 5:08 
AnswerRe: Class Pin
Zoltan Balazs21-Feb-09 4:53
Zoltan Balazs21-Feb-09 4:53 
Questionneed help with windows application Pin
balukk20-Feb-09 22:45
balukk20-Feb-09 22:45 
AnswerRe: need help with windows application Pin
Deresen21-Feb-09 3:11
Deresen21-Feb-09 3:11 
AnswerRe: need help with windows application Pin
balukk21-Feb-09 3:56
balukk21-Feb-09 3:56 
GeneralRe: need help with windows application Pin
Deresen22-Feb-09 2:23
Deresen22-Feb-09 2:23 
GeneralRe: need help with windows application Pin
balukk23-Feb-09 5:44
balukk23-Feb-09 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.