日期:2014-05-18  浏览次数:20851 次

C#通过消息关闭另一程序的窗体,为什么原有的关闭时的提示也没有了。
之前手动点关闭时有个提示,你的数据还没有保存,保存吗?但通过 SendMessage(process.MainWindowHandle, 16, IntPtr.Zero, IntPtr.Zero);来关闭。。这个提示就没有了。。。。我现在想要这个提示怎么办??

我想做的功能是:我打开这个软件如果已经是打开过的了。就发条消息给他正常的关闭上一次打开的,并提示保存数据后,按用户操作 再决定是否重新打开。  

但现在我发送过来的消息他直接把窗关闭了怎么办,,大家帮帮我。项目很紧呀。。,谢谢了。

------解决方案--------------------
请看MSDN 关于SendMessage

------解决方案--------------------
没看见……
你为什么要发WM_CLOSE而不是SC_CLOSE?
------解决方案--------------------
C# code
        [DllImport("coredll.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        private void button1_Click(object sender, EventArgs e)
        {
            PostMessage(this.Handle, 16, IntPtr.Zero, IntPtr.Zero);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SendMessage(this.Handle, 16, IntPtr.Zero, IntPtr.Zero);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            MessageBox.Show("FormClosed");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("FormClosing");
        }

------解决方案--------------------
建议在窗体的closing事件里写,就不需要在WndProc里写了
C# code

        protected override void OnClosing(CancelEventArgs e)
        {
            if (MessageBox.Show("你的数据还没有保存,保存吗?", "", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                e.Cancel = true;
            }
            base.OnClosing(e);
        }

------解决方案--------------------
const int SC_CLOSE = 0xF060;

sendmessage SC_CLOSE看
------解决方案--------------------
发WM_COPYDATA,自己定义COPYDATASTRUCT
------解决方案--------------------
探讨
引用:

系统消息没用,你就发个自定义消息,让 WndProc 接收自定消息,调用你弹出的确认对话框



这个方法更好。。因为可以控制,不是发的消息就不弹出。。问题是自定义消息,怎么个自定义呢?

------解决方案--------------------
MessageBox.Show("你的数据还没有保存,保存吗?", "", MessageBoxButtons.YesNo) != DialogResult.Yes