Windows Develop Bookmark and Share   
 index > Windows Forms General > Make Form modal to external hWnd
 

Make Form modal to external hWnd

In a screensaver, when the Options button is clicked in control panel, it calls the screen saver with the command line argument /c:1234567 (for example) where 1234567 is the window handle of the control panel dialog that my Options form needs to be made modal to.

How can I do this? I tried Control.FromHandle but it returned null.
Aero.NET  Tuesday, November 22, 2005 7:50 PM

You have to create a class that implements IWin32Window:


using System;
using System.Windows.Forms;

public class Window : IWin32Window
{
 private IWin32Window _Handle;

 public Window(IntPtr handle)
 {
  _Handle = handle;
 }
 
 public IntPtr Handle
 {
  get { return _Handle; }
 }
}


 



You can then do this:


using (Form form = new Form())
{
      form.ShowDialog(new Window((IntPtr)1234567));
}

 

Where 1234567 is the handle that is passed to you.

David M. Kean  Tuesday, November 22, 2005 8:07 PM

You have to create a class that implements IWin32Window:


using System;
using System.Windows.Forms;

public class Window : IWin32Window
{
 private IWin32Window _Handle;

 public Window(IntPtr handle)
 {
  _Handle = handle;
 }
 
 public IntPtr Handle
 {
  get { return _Handle; }
 }
}


 



You can then do this:


using (Form form = new Form())
{
      form.ShowDialog(new Window((IntPtr)1234567));
}

 

Where 1234567 is the handle that is passed to you.

David M. Kean  Tuesday, November 22, 2005 8:07 PM
Hey, there,

Any idea why this code, when translated to VB.NET, causes a compiler error? IntPtr cannot be converted to IWin32Window and vice versa.

Thanks!
Graham Charles  Friday, December 09, 2005 9:02 PM

I have the same problem, and I tried your solution. but the form does not become modal, the parentform can still be selected and closed.

Any other suggestions ?

(Sorry if I am thread-necromancing with this, it's just that I did a search and this thread came up)

Thanks in advance,

Ben Blok.

boran_blok_edan  Thursday, January 18, 2007 4:32 PM

You can use google to search for other answers

Custom Search

More Threads

• Customizing the controls in the Print Dialog to my desired location
• Edit Files
• Common Language Runtime Debugging Services
• .Net application via Terminal Service
• Clickable ErrorProvider
• Exception on application close?
• Dictionary for using Date Comparison values
• DateTimePicker Control value not changing
• Web Browser Control question!
• Strange RectangleF behaviour