MsgBox does not come to foreground

Heike

Regular
Joined
Jun 5, 2002
Messages
56
There is an application that calls an EXE that I developped in VB.NET
This EXE opens a word application:

Code:
objWordInstance = GetObject(, "Word.Application")

If objWordInstance Is Nothing Then
    objWordInstance = CreateObject("Word.Application")
End If
After that I try to show a MessageBox but it is always hidden behind my word application. Any idea what to do?
 
Hi

One of the overloaded parameters in the Messagebox.show method, takes in a 'owner' parameter of type: IWin32Window.
This will render the messagebox infront of this specified window (Your word doc)

You will need to get the Handle on this window from your exe to be used by your messagebox.

Cheers
 
Heike

You can check, while debugging if the ActiveWindow property implement IWin32Window (You can use the command window)

But, I've got another solution:

C#:
// Stephen Toub
// [email]stoub@microsoft.com[/email]
//
// ActiveWindow.cs v1.0.0
// 8/28/02
//
// Implements IWin32Window where Handle returns the handle of the system's foreground window.
// Can be used with MessageBox to display the box in front of the active window, such as:
//		MessageBox.Show(ActiveWindow.Active, "Hello, World!");

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Toub.Windows.Forms
{
	/// <summary>Used to get an IWin32Window for the active window.</summary>
	public class ActiveWindow : IWin32Window
	{
		#region Private Members
		/// <summary>Static instance to use for factory pattern.</summary>
		private static ActiveWindow _window = new ActiveWindow();
		#endregion

		#region Construction
		/// <summary>Prevent external instantiation.</summary>
		private ActiveWindow() {}
		#endregion

		#region Public Properties
		/// <summary>Gets an IWin32Window for the active window.</summary>
		public static IWin32Window Active { get { return _window; } }
		#endregion

		#region Private Functions
		/// <summary>Finds the frontmost window using the Win32 API.</summary>
		[DllImport("user32.dll")]
		private static extern int GetForegroundWindow();

		/// <summary>Gets a handle to the active window.</summary>
		System.IntPtr IWin32Window.Handle
		{
			get { return new System.IntPtr(GetForegroundWindow()); }
		}
		#endregion
	}
}

above is a class sample, downloaded of gotdotnet site. It will get the IWin32Window object by using a API.

It's in C# but I'm sure you'll get the idea..:)
Let me know if you must have the vb version

Cheers
 
Indeed, I'm getting the idea but don't know exactly what to do. I copied the source and tried to convert it to vb but the first error I get is "classes can inherit only from other classes".

Therefore I would be obliged if you could give me a vb version.

Thanks
 
short and sweet..... :

Visual Basic:
Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class ActiveWindow
    Implements IWin32Window

    Private Sub New()

    End Sub

    Private Declare Function GetForegroundWindow Lib "user32.dll" () As Integer

    Private Shared _window As ActiveWindow = New ActiveWindow()

    Public Shared ReadOnly Property Active() As IWin32Window
        Get
            Return _window
        End Get
    End Property


    Public ReadOnly Property Handle() As IntPtr Implements System.Windows.Forms.IWin32Window.Handle
        Get
            Return New System.IntPtr(GetForegroundWindow())
        End Get
    End Property
End Class

Cheers
 
Thank you very much.
I only made one try, yet, but it seems to work.
I will test it more intensely but I'm sure my problem will be solved.
 
Back
Top