尝试在不使用我的应用程序时使用SendMessage关闭控件父窗体时,它将关闭其他应用程序 [英] Trying to close control parent form using SendMessage while not working in my app it will close other apps

查看:66
本文介绍了尝试在不使用我的应用程序时使用SendMessage关闭控件父窗体时,它将关闭其他应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试关闭父窗体,并且无法正常工作.如果我发送完全相同的内容说记事本",则它可以正常工作并关闭窗口,但在我的解决方案中不起作用.这是我的代码:

Hi All,

I am trying to get the parent form to close and its not working. If I send the exact same thing to say Notepad it works fine and closes the window but not in my solution. Here is my code:

protected override void OnMouseClick(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        if (aaaRectClose.Contains(e.Location) && this.Parent != null)
        {            
            IntPtr frmHandle = (this.Parent is Form) ? this.Parent.Handle : 0;
            if(frmHandle > 0)
            {
                SendMessage(frmHandle, ((uint)0x0112), ((int)0xF060), 0);  
            }          
        }
    }
}

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);



有什么想法为什么这适用于其他应用程序但不适用于我自己的应用程序?任何帮助或建议,将不胜感激!

谢谢!



Any ideas why this works for other applications but not my own? Any help or suggestions would be greatly appreciated!

Thank you!

推荐答案

简单的方法

Look easy way

public partial class Form2 : Form
    {
        private Form _ParentForm;           

        public Form2(Form form)
        {
            InitializeComponent();
            this._ParentForm = form;            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this._ParentForm.Close();
        }
    }



现在要使用此功能



Now to use do this

Form2 form = new Form2(this);
            form.Show();


好吧,经过数小时的MSDN检查并使用Telerik拨弄了System.Windows.Forms.Form之后,我终于有了一个可行的解决方案,在这里" s我得到的是:

Ok, after hours of checking MSDN and poking around System.Windows.Forms.Form using Telerik I finally have a working solution and here''s what I got:

if (aaaRectClose.Contains(e.Location) && aaaParentForm != null)
{
    if (aaaRectClose.Contains(e.Location) && this.Parent != null)
    {
        IntPtr frmHandle = (this.Parent is Form) ? this.Parent.Handle : IntPtr.Zero;
        if (frmHandle != IntPtr.Zero)
        {
            SendMessage(frmHandle, 16, 0, 0);
        }
    }
}



基本上,这就是我要完成的工作:自定义控件可以以任何形式包含在项目中.该控件包含多个虚拟"按钮,它们位于图像的一部分. aaaRectClose是一个矩形,其中包含关闭"按钮的边界.当按下它时,需要关闭该窗体,就像按下标准Windows窗体右上角的"X"一样.包含此关闭按钮的原因是某些表格可能是无边界的.

无论如何,感谢所有阅读我的问题并提出解决方案的人,感谢您的努力.

上面的代码确实适用于那些最终可能对那些比自己更高的人提出相同要求的愚蠢要求,而这些要求却对您没有任何帮助.否则,只需发出"this.Parent.Close()"语句即可,因为它要简单得多.

虽然我找不到或没有有关"SendMessage(IntPtr,16,0,0)"的文档,但它与MSDN上的内容完全相同,但它确实起作用.

-Phil



Basically here is what I was trying to accomplish: A custom control could be included in any form in the project. This control contained several "Virtual" buttons which where part of an image. aaaRectClose was a Rectangle that contained the bounds for the "Close" button. When pressed it needed to close the form as if pressing the "X" in the upper right corner of standard Windows forms. The reason for including this close button is some of the forms may be borderless.

Anyways thank you to everyone who read my question and proposed a solution your efforts have been appreciated.

The above code does work for anyone who may eventually have the same silly requirements thrown in their lap with someone higher than yourself demanding things that make no since to you. Otherwise simply issue the "this.Parent.Close()" statement as its much simpler.

Whilst I couldn''t find or there is no documentation on "SendMessage(IntPtr, 16, 0, 0)" nor does it coincide with what is on MSDN it does work.

-Phil


这根本没有道理.子窗体打开时,您的父窗体不会关闭,而您拥有父窗体,它就位于您的应用程序内部,为什么不告诉它关闭,或告诉应用程序关闭,而不使用SendMessage?仅在必要时才应编写这样的混淆代码.
This makes no sense at all. Your parent form won''t close while a child is open, and as you have the parent form, it''s inside your app, why not just tell it to close, or tell the Application to close, instead of using SendMessage ? You should write obsfucated code like this only when you have to.


这篇关于尝试在不使用我的应用程序时使用SendMessage关闭控件父窗体时,它将关闭其他应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆