locked
ChangeWindowMessageFilter on Windows 8.1 RRS feed

  • Question

  • Did windows 8.1 blocked ChangeWindowMessageFilter?

    This custom control works on windows 7, but I have problem on windows 8.1

    using System;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace Admin_Accelerator
    {
        class DragDropPictureBox : PictureBox
        {
            public DragDropPictureBox()
            {
                DragAcceptFiles(Handle, true);
    
                ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
                ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);
                ChangeWindowMessageFilter(WM_COPYGLOBALDATA, MSGFLT_ADD);
            }
    
            const int WM_DROPFILES = 0x233;
            const int WM_COPYDATA = 0x004A;
            const int WM_COPYGLOBALDATA = 0x0049;
    
            [DllImport("shell32.dll", CharSet = CharSet.Ansi)]
            internal static extern void DragAcceptFiles(IntPtr hWnd, bool fAccept);
    
            [DllImport("user32.dll", SetLastError = true)]
            internal static extern bool ChangeWindowMessageFilter(uint message, int dwFlag);
    
            [DllImport("shell32.dll", CharSet = CharSet.Auto)]
            internal static extern uint DragQueryFile(IntPtr hDrop, uint iFile, StringBuilder lpszFile, uint cch);
    
            [DllImport("shell32.dll")]
            internal static extern void DragFinish(IntPtr hDrop);
    
            const int MSGFLT_ADD = 1;
    
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                switch (m.Msg)
                {
                    case WM_DROPFILES:
                        StringBuilder sFile = new StringBuilder(256);
                        uint result = DragQueryFile(m.WParam, 0, sFile, (uint)sFile.Capacity);
                        DragFinish(m.WParam);
                        OnDragDrop(new DragEventArgs(new FileDropDataObject(sFile.ToString(), result != 0), 2,
                                                     Cursor.Position.X, Cursor.Position.Y, DragDropEffects.All,
                                                     DragDropEffects.All));
                        break;
                }
            }
    
            protected override void OnDragDrop(DragEventArgs e)
            {
                base.OnDragDrop(e);
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    Text = (string)e.Data.GetData(DataFormats.FileDrop);
                }
            }
    
            private class FileDropDataObject : IDataObject
            {
                private readonly string _dropfile;
                private readonly bool _dataPresent;
    
                public FileDropDataObject(string file, bool dataPresent)
                {
                    _dropfile = file;
                    _dataPresent = dataPresent;
                }
    
                public object GetData(Type format)
                {
                    throw new NotImplementedException();
                }
    
                // IMPORTANT
                public object GetData(string format)
                {
                    if (format == DataFormats.FileDrop)
                    {
                        return _dropfile;
                    }
                    return null;
                }
    
                public object GetData(string format, bool autoConvert)
                {
                    throw new NotImplementedException();
                }
    
                public bool GetDataPresent(Type format)
                {
                    throw new NotImplementedException();
                }
    
                // IMPORTANT
                public bool GetDataPresent(string format)
                {
                    if (format == DataFormats.FileDrop)
                    {
                        return _dataPresent;
                    }
                    return false;
                }
    
                public bool GetDataPresent(string format, bool autoConvert)
                {
                    throw new NotImplementedException();
                }
    
                public string[] GetFormats()
                {
                    throw new NotImplementedException();
                }
    
                public string[] GetFormats(bool autoConvert)
                {
                    throw new NotImplementedException();
                }
    
                public void SetData(object data)
                {
                    throw new NotImplementedException();
                }
    
                public void SetData(Type format, object data)
                {
                    throw new NotImplementedException();
                }
    
                public void SetData(string format, object data)
                {
                    throw new NotImplementedException();
                }
    
                public void SetData(string format, bool autoConvert, object data)
                {
                    throw new NotImplementedException();
                }
            }
        }
    }


    • Edited by KKristijan Sunday, September 13, 2015 2:09 PM
    Sunday, September 13, 2015 2:09 PM

Answers

  • I have this sample code:

    class DragDropPictureBox : PictureBox
    {
        const uint MSGFLT_ALLOW = 1;
    
        [DllImport( "user32", SetLastError = true )]
        static extern bool ChangeWindowMessageFilterEx( IntPtr hWnd, uint msg, uint action, IntPtr unused );
    
        protected override void OnHandleCreated( EventArgs e )
        {
            base.OnHandleCreated( e );
    
            bool b = ChangeWindowMessageFilterEx( Handle, WM_COPYDATA, MSGFLT_ALLOW, IntPtr.Zero );
        }
    
        . . . .
    

    • Marked as answer by KKristijan Monday, September 14, 2015 8:02 PM
    Monday, September 14, 2015 7:12 PM

All replies

  • The possible unavailability of ChangeWindowMessageFilter is also mentioned in documentation. Consider ChangeWindowMessageFilterEx instead.

    • Proposed as answer by DotNet Wang Monday, September 14, 2015 7:17 AM
    Sunday, September 13, 2015 4:40 PM
  • The possible unavailability of ChangeWindowMessageFilter is also mentioned in documentation. Consider ChangeWindowMessageFilterEx instead.

    Do you have some sample codes which shows how to implement ChangeWindowMessageFilterEx  in controls?
    Monday, September 14, 2015 6:36 PM
  • I have this sample code:

    class DragDropPictureBox : PictureBox
    {
        const uint MSGFLT_ALLOW = 1;
    
        [DllImport( "user32", SetLastError = true )]
        static extern bool ChangeWindowMessageFilterEx( IntPtr hWnd, uint msg, uint action, IntPtr unused );
    
        protected override void OnHandleCreated( EventArgs e )
        {
            base.OnHandleCreated( e );
    
            bool b = ChangeWindowMessageFilterEx( Handle, WM_COPYDATA, MSGFLT_ALLOW, IntPtr.Zero );
        }
    
        . . . .
    

    • Marked as answer by KKristijan Monday, September 14, 2015 8:02 PM
    Monday, September 14, 2015 7:12 PM