SendMessage问题

时间:2010-01-07 18:37:03

标签: c# shell sendmessage postmessage

我正在开发一个使用C#的应用程序,它具有类似于Windows的复制,粘贴功能。 我添加了菜单项并与各自的应用程序链接。

请查看以下图片以获得更多想法。

Items added to shell menu http://softwaregenius.net/myimages/menu.jpg

就像我们在Windows资源管理器中选择多个项目一样,您需要选择多个文件和/或文件夹,然后选择OS Util-> FastCopy。打开表单,如下所示

Form shown on FastCopy http://softwaregenius.net/myimages/fastcopy1.jpg

该应用程序运行正常。这里的主要问题是在选择文件之后,所有这些文件都在各自的软件中打开。也就是说,如果我选择了word文档,那么文件名将添加到FastCopy表单中,但也会在Word中打开。

当我调查时,我发现此问题是由于SendMessage造成的。我必须使用PostMessage而不是SendMessage。但是当我这样做时,应用程序无效。

以下是我在C#2005中的主要功能编码

static class Program
{
    static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE92}");
    [STAThread]
    static void Main(string[] args)
    {
        string fileName = "";
        if (args.Length > 0)
        {
            fileName = args[0];
        }
        if (mutex.WaitOne(TimeSpan.Zero, true))
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            frmFastCopy frm = new frmFastCopy();
            frm.AddItemToList(fileName);
            Application.Run(frm);

        }
        else
        {
            //The following message is sent just to show up the form
            NativeMethods.PostMessage(
                    (IntPtr)NativeMethods.HWND_BROADCAST,
                    NativeMethods.WM_SHOWME,
                    IntPtr.Zero,
                    IntPtr.Zero);

            //Send the filename
            SendFileName(fileName);
        }
    }

    static void SendFileName(string s)
    {
        Win32.CopyDataStruct cds = new Win32.CopyDataStruct();

        cds.cbData = (s.Length + 1) * 2;
        cds.lpData = Win32.LocalAlloc(0x40, cds.cbData);
        Marshal.Copy(s.ToCharArray(), 0, cds.lpData, s.Length);
        cds.dwData = (IntPtr)1;
        Win32.SendMessage((IntPtr)NativeMethods.HWND_BROADCAST, Win32.WM_COPYDATA, IntPtr.Zero, ref cds);
        //NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, Win32.WM_COPYDATA, cds.lpData, IntPtr.Zero);
    }
}

}


以下是WndProc的副本和表格中的其他代码

公共部分类frmFastCopy:表单     {         委托void AddItemToListDelegate(string itm);

    public frmFastCopy()
    {
        InitializeComponent();
    }

    public void AddItemToList(string itm)
    {
        if (lvFilesAndFolders.InvokeRequired)
        {
            AddItemToListDelegate m = new AddItemToListDelegate(AddItemToList);
            this.Invoke(m, new object[] { itm });
        }
        else
        {
            lvFilesAndFolders.Items.Add(itm);
        }
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg==NativeMethods.WM_SHOWME)
        {
                ShowMe();
        }
        if (m.Msg==Win32.WM_COPYDATA)
        {
                //string s = Marshal.PtrToStringUni(m.LParam);
                MessageBox.Show("Got message");

                Win32.CopyDataStruct st = (Win32.CopyDataStruct)Marshal.PtrToStructure(m.LParam, typeof(Win32.CopyDataStruct));
                string strData = Marshal.PtrToStringUni(st.lpData);
                AddItemToList(strData);
        }
        base.WndProc(ref m);
    }
    private void ShowMe()
    {
        this.Show();
        if (WindowState == FormWindowState.Minimized)
        {
            WindowState = FormWindowState.Normal;
        }
        // get our current "TopMost" value (ours will always be false though)
        bool top = TopMost;
        // make our form jump to the top of everything
        TopMost = true;
        // set it back to whatever it was
        TopMost = top;
    }

这是NativeCode类

internal class NativeMethods
{
    public const int HWND_BROADCAST = 0xffff;
    public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
    [DllImport("user32")]
    public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
    [DllImport("user32")]
    public static extern int RegisterWindowMessage(string message);

}

我知道你们都是天才。有人可以告诉我应该在哪里更改所选文件应该打开,或者我应该如何使用postmessage。

感谢您分享宝贵的时间。

此致

伊尔凡

1 个答案:

答案 0 :(得分:1)

请查看我的评论(我想知道你为什么不在这里使用剪贴板课程)。但忽略了这一点:你为什么要广播这条消息?

您可以找到您的应用程序(按名称,窗口类别等),只将消息发送到您自己的应用程序吗?


详细说明消息处理: 您在以下评论中说HWND_BROADCAST:

  

除了全球手柄之外别无他物   我的申请。

不,不是。它是一个特殊值,告诉Windows“此消息适用于所有应用程序”。您正在向所有应用程序发送WM_SHOWME。这就是我问你为什么要那样做的原因?

请参阅the old new things blog关于消息广播的帖子。