none
Close message box programmatically

    Question

  • Hello,

    I want close a MessageBox programmatically after some background process in Windows CE(C#.NET v 3.5).

    Please anyone help me with sample code.

    Thanks in advance.
    Friday, January 09, 2009 10:41 AM

Answers

  • Hi,
    better way is own dialog with custom behavior.

    But here is the dirty and quick way. :) In your project add reference to the assembly Microsoft.WindowsCE.Forms.

    using System;  
    using System.Linq;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Text;  
    using System.Windows.Forms;  
    using System.Threading;  
    using System.Runtime.InteropServices;  
    using Microsoft.WindowsCE.Forms;  
     
    namespace MessagBoxClose  
    {  
        public partial class Form1 : Form  
        {  
            private bool m_killHim;  
            private bool  m_threadAlive;  
            private Thread m_killThread;  
     
            private const uint GW_HWNDFIRST = 0;  
            private const int WM_CLOSE = 0x0010;  
     
            [DllImport("coredll.dll", EntryPoint="FindWindowW", SetLastError=true)]  
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
     
     
     
            public Form1()  
            {  
                m_killHim = false;  
                m_threadAlive = true;  
                InitializeComponent();  
            }  
     
            private void Form1_Load(object sender, EventArgs e)  
            {  
                ThreadStart myThread = () =>  
                                        {  
                                            while (m_threadAlive)  
                                            {  
                                                Thread.Sleep(200);  
                                                if (m_killHim)  
                                                {  
                                                    IntPtr nextW = FindWindow(null"MyDialogConstant");  
                                                    if (nextW != IntPtr.Zero)  
                                                          
                                                    {  
                                                        Message closeMessage = Message.Create(nextW, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);  
                                                        MessageWindow.SendMessage(ref closeMessage);  
                                                          
                                                    }  
                                                }  
           
                                            }  
                                          
                                          
                                        };  
     
                m_killThread = new Thread(myThread);  
                m_killThread.Start();  
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                m_killHim = true;  
                MessageBox.Show("MyT""MyDialogConstant");  
                m_killHim = false;  
            }  
     
     
        }  

    HTH
    Rene Stein
    http://blog.renestein.net
    Friday, January 09, 2009 11:37 AM

All replies

  • Hi,
    better way is own dialog with custom behavior.

    But here is the dirty and quick way. :) In your project add reference to the assembly Microsoft.WindowsCE.Forms.

    using System;  
    using System.Linq;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Text;  
    using System.Windows.Forms;  
    using System.Threading;  
    using System.Runtime.InteropServices;  
    using Microsoft.WindowsCE.Forms;  
     
    namespace MessagBoxClose  
    {  
        public partial class Form1 : Form  
        {  
            private bool m_killHim;  
            private bool  m_threadAlive;  
            private Thread m_killThread;  
     
            private const uint GW_HWNDFIRST = 0;  
            private const int WM_CLOSE = 0x0010;  
     
            [DllImport("coredll.dll", EntryPoint="FindWindowW", SetLastError=true)]  
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
     
     
     
            public Form1()  
            {  
                m_killHim = false;  
                m_threadAlive = true;  
                InitializeComponent();  
            }  
     
            private void Form1_Load(object sender, EventArgs e)  
            {  
                ThreadStart myThread = () =>  
                                        {  
                                            while (m_threadAlive)  
                                            {  
                                                Thread.Sleep(200);  
                                                if (m_killHim)  
                                                {  
                                                    IntPtr nextW = FindWindow(null"MyDialogConstant");  
                                                    if (nextW != IntPtr.Zero)  
                                                          
                                                    {  
                                                        Message closeMessage = Message.Create(nextW, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);  
                                                        MessageWindow.SendMessage(ref closeMessage);  
                                                          
                                                    }  
                                                }  
           
                                            }  
                                          
                                          
                                        };  
     
                m_killThread = new Thread(myThread);  
                m_killThread.Start();  
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                m_killHim = true;  
                MessageBox.Show("MyT""MyDialogConstant");  
                m_killHim = false;  
            }  
     
     
        }  

    HTH
    Rene Stein
    http://blog.renestein.net
    Friday, January 09, 2009 11:37 AM
  • For me, it display a message box from another dll and the header also varies depends on which driver being selected. How to close those message boxes from program? Please help
    Wednesday, May 19, 2010 11:53 AM
  • Basically the same way as in the above sample - using the FindWindow API.

    You have to call 'FindWindow' multiple times - once for each message box you'd expect to appear.


    MVP Windows Embedded
    Wednesday, May 19, 2010 3:14 PM
  • If Message box has Yes n No buttons , its not working. Is there any way to do ?
    Thursday, May 20, 2010 10:36 AM
  • Hi

    If Message box has Yes n No buttons , its not working. Is there any way to do ?


    You can use the API PostMessage to send ID_OK or ID_CANCEL, it is available in OpenNETCF.Win32.Win32Window.PostMessage, like this 

    Thursday, May 20, 2010 5:24 PM