?
Solved

VB.NET: Win32 Hwnd to IWin32Window for use with Form.Show(Owner)

Posted on 2010-04-04
9
Medium Priority
?
4,038 Views
Last Modified: 2012-05-09
I'm converting an Excel COM Addin that was written in VB6 to VB.NET 2008. I have a form that I would like to make TopMost but only to Excel. I also have Excel's Win32 Hwnd but I get it using late binding so I'm not sure if it's OK to store it as an integer.

Dim xlHwnd As Integer = oAddin.ExcelApp.Hwnd ' (I'm using Addin Express 2009)
Dim myForm = New frmOneOfMyWindowsForms
myForm.TopMost = True
myForm.Show(xlHwnd) ' ***

*** This won't compile because I'm not allowed to convert an Integer (xlHwnd) to an IWin32Window in the myForm.Show statement.

How do I convert an Hwnd to an IWin32Window?

I suspect I have to pass an object that implements IWin32Window to Form.Show() but I have been unable to find any examples in VB.NET.

Thanks.
0
Comment
Question by:ou81aswell
  • 5
  • 4
9 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29701305
0
 

Author Comment

by:ou81aswell
ID: 29716686
Thanks!

I'm looking for VB.net examples. Those are C#.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29717367
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:ou81aswell
ID: 29718525
Thanks again!
That link produces a 404 type error for me.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29718958
Their site is down i think. Try this

http://www.dotnetspider.com/Convert/Csharp-To-Vb.aspx
0
 

Author Comment

by:ou81aswell
ID: 29751161
Thanks.

I used that tool to convert the following wrapper class from C# to VB:

public class WindowWrapper : System.Windows.Forms.IWin32Window
{
    public WindowWrapper(IntPtr handle)
    {
        _hwnd = handle;
    }

    public IntPtr Handle
    {
        get { return _hwnd; }
    }

    private IntPtr _hwnd;
}

The resulting VB.NET code was:

Public Class WindowWrapper
         Inherits System.Windows.Forms.IWin32Window
    Public  Sub New(ByVal handle As IntPtr)
        _hwnd = handle
    End Sub

    Public ReadOnly Property Handle() As IntPtr
        Get
                 Return _hwnd
        End Get
    End Property

    Private _hwnd As IntPtr
End Class

This class won't compile because of an error with the statement:

         Inherits System.Windows.Forms.IWin32Window

"Classes can inherit only from other classes."

I'm guessing that I have to define an Interface and an implementation.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 29751362
Change the word inherits to "Implements"
0
 

Author Comment

by:ou81aswell
ID: 29765266
Thanks. This is the resulting class.

Imports System.Windows.Forms

Public Class Win32WindowWrapper

    Implements System.Windows.Forms.IWin32Window

    Private _hwnd As IntPtr

    Public Sub New(ByVal handle As IntPtr)
        _hwnd = handle
    End Sub

    Public ReadOnly Property Handle() As IntPtr Implements IWin32Window.Handle
        Get
            Return _hwnd
        End Get
    End Property

End Class

I can use it as follows:

' xlHwnd is Excel's Application.Hwnd (Integer)
Dim owner as New Win32WindowWrapper(xlHwnd)
myForm.Show(owner)

This answers my question.

Many thanks.

Now I have to figure out how to toggle the state of "always on top of owner" when a form been shown with myForm.Show(owner), from code within that form. I presume I would have a checkbox on my Form that allows the user to toggle this state.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29789191
Glad to help :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

600 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