Solved

Kill all Messagebox in Visual studio 2008

Posted on 2009-07-11
7
569 Views
Last Modified: 2013-11-27
I want to kill all messagebox from external program , or kill all open messagebox  in windows. thanks a lot
0
Comment
Question by:Gknrsy
  • 6
7 Comments
 

Author Comment

by:Gknrsy
ID: 24831502
i found some c# codes but i don't know how in vb2008

// showbox.cs

using System;
using System.Windows.Forms;

class Program
{
    static void Main()
    {
       Application.Run(new ShowBox());      
    }
}

class ShowBox : Form
{
    private Button btn;

    public ShowBox()
    {
       btn = new Button();
       btn.Text = "Press me";
       btn.Click += new EventHandler(button_Click);
       this.Controls.Add(btn);
    }

    private void button_Click(object sender, EventArgs e)
    {
       MessageBox.Show(this, "Hello from Show Box", "Greeting");
    }
}  

// capturebox.cs

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

class CaptureBox
{
    [DllImport("user32.dll")]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);


    static IntPtr hwndParent;

    static void Main()
    {
        Process[] procs = Process.GetProcessesByName("showbox");

        if (procs.Length == 0)
        {
            Console.WriteLine("Showbox isn't running");
            return;
        }
     
        hwndParent = procs[0].MainWindowHandle;

        bool hasAppeared = false;

        do        
        {
            if (!procs[0].HasExited)
            {
               hasAppeared = CheckMessageBox();
               Thread.Sleep(1000); // check every second
            }
            else
            {
               Console.WriteLine("Showbox has exited");
               break;
            }
        }          
        while(!hasAppeared);
    }

    static bool CheckMessageBox()
    {
        // IntPtr hwndMB = FindWindowEx(hwndParent, IntPtr.Zero, "#32770", "Greeting");
        // Apparently MessageBox is always top level in .NET
        IntPtr hwndMB = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Greeting");
        if (hwndMB == IntPtr.Zero)
        {
           Console.WriteLine("MessageBox not showing");
           return false;
        }
        else
        {          
           Console.WriteLine("MessageBox has appeared");
           return true;
        }
    }
}
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 300 total points
ID: 24832307
Below is a VB translation of the code you posted. For the class CaptureBox, I started a VB Console Application project and put the code inside a module file.
''''''''''''''''''''''''''''''''''''''''''''

'' Showbox code

''''''''''''''''''''''''''''''''''''''''''''

Public Class ShowBox

    Private btn As Button
 

    Public Sub New()

        ' This call is required by the Windows Form Designer.

        InitializeComponent()
 

        ' Add any initialization after the InitializeComponent() call.

        btn = New Button()

        btn.Text = "Press me"

        AddHandler btn.Click, AddressOf button_Click

        Controls.Add(btn)

    End Sub
 

    Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)

        MessageBox.Show(Me, "Hello from Show Box", "Greeting")

    End Sub
 

End Class
 

''''''''''''''''''''''''''''''''''''''''''''

'' CaptureBox code

''''''''''''''''''''''''''''''''''''''''''''

Imports System.Runtime.InteropServices

Imports System.Diagnostics

Imports System.Threading
 

Module Module1

    Dim hwndParent As IntPtr
 

    Sub Main()

        Dim procs() As Process = Process.GetProcessesByName("showbox")
 

        If procs.Length = 0 Then

            Console.WriteLine("Showbox isn't running.");

            Return

        End If
 

        hwndParent = procs(0).MainWindowHandle
 

        Dim hasAppeared As Boolean = False
 

        Do

            If Not procs(0).HasExited Then

                hasAppeared = CheckMessageBox()

                Thread.Sleep(1000)

            Else

                Console.WriteLine("Showbox has exited")

                Exit Do

            End If

        Loop While Not hasAppeared

    End Sub
 

    Function CheckMessageBox() As Boolean

        Dim hwndMB As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Greeting")
 

        If hwndMB = IntPtr.Zero Then

            Console.WriteLine("MessageBox not showing.")

            Return False

        Else

            Console.WriteLine("MessageBox has appeared")

            Return True

        End If

    End Function
 

    <DllImport("user32.dll")> _

    Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr

    End Function
 
 

End Module

Open in new window

0
 

Author Comment

by:Gknrsy
ID: 24833242
i just found this code in internet. is it working??
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Gknrsy
ID: 24833244
i asked because just tomarrow i will try :)
0
 

Author Comment

by:Gknrsy
ID: 24837022
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Threading
Module Module1



    Dim hwndParent As IntPtr



    Function CheckMessageBox() As Boolean
        Dim hwndMB As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Greeting!")

        If hwndMB = IntPtr.Zero Then
            MsgBox("MessageBox not showing.")
            Return False
        Else
            MsgBox("MessageBox has appeared")
            Return True
        End If
    End Function

    <DllImport("user32.dll")> _
    Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    End Function


End Module

with this code i am checking messagebox working or not .

now if messagebox working how i will close this message box?


0
 

Author Comment

by:Gknrsy
ID: 24837068

    Function CheckMessageBox() As Boolean
        Dim hwndMB As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Hello")

        If hwndMB = IntPtr.Zero Then
            Exit Function
            Return False
        Else
            Dim procs() As Process = Process.GetProcessesByName("ABC")
            AppActivate(procs(0).Id)
            SendKeys.Send("{enter}") ' this sends F5 to the window
            Return True
        End If
    End Function

i wite this code , but this code working with focus i mean when sending enter key to messagebox it becomes active. i want to send enter key or just kill message box background , i don't want to it brings front :) sorry for bad explanation.
0
 

Accepted Solution

by:
Gknrsy earned 0 total points
ID: 24837225
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Threading
Module Module1

    Dim hwndParent As IntPtr

    Function CheckMessageBox() As Boolean
        Dim hwndMB As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "")

        If hwndMB = IntPtr.Zero Then
            Exit Function
            Return False
        Else
           SendMessage(hwndMB, WM_CLOSE, 0, 0)
            Return True
        End If
    End Function

    <DllImport("user32.dll")> _
   Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, ByVal wParam As Long, _
    ByVal lParam As Long) As Long

    End Function

    Private Const WM_CLOSE = &H10


    <DllImport("user32.dll")> _
    Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    End Function


End Module


This is working very well :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access Web appliction 7 48
Help with Query not working in client's PC 1 35
Get id from json Data with NewtonSoft 3 34
Entity Framework 7 32
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now