System Modal Message Boxes

G

Guest

Hi,

I am wondering why the .NET Framework is quite different from Win32 API when
it comes to displaying system modal message boxes.

Consider the four following types of system modal message boxes (please see
associated source code below):
1) Win32 API with context ("btnWin32WithContext_Click")
2) .NET Framework with context ("btnFrameworkWithContext_Click")
3) Win32 API withOUT context ("btnWin32WithOUTContext_Click")
4) .NET Framework withOUT context ("btnFrameworkWithOUTContext_Click")

Message box (1) will display on the same screen as the caller.
Message box (2) will throw an exception.
Message box (3) will display on the same screen as the caller.
Message box (4) will NOT display on the same screen as the caller.

I would like to use the .NET Framework to display a system modal message box
on the same screen as the caller. Any ideas why this doesn't work in (2) or
(4)?

Thanks,
Nate



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

namespace DifferentTypesOfSystemModalMessageBoxes
{
public partial class Form1 : Form
{
/// <summary>
/// Standard Windows point
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
/// <summary>
/// x
/// </summary>
public long x;
/// <summary>
/// y
/// </summary>
public long y;
};

/// <summary>
/// From winuser.h
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct HELPINFO
{
/// <summary>
///
/// </summary>
public uint cbSize;
/// <summary>
///
/// </summary>
public int iContextType;
/// <summary>
///
/// </summary>
public int iCtrlId;
/// <summary>
///
/// </summary>
public IntPtr hItemHandle;
/// <summary>
///
/// </summary>
public IntPtr dwContextId;
/// <summary>
///
/// </summary>
public POINT MousePos;

/// <summary>
/// Unmarshal the helpinfo out of the given intptr, which is
presumably the lParam received
/// in a WM_HELP message.
/// </summary>
/// <param name="lParam"></param>
/// <returns></returns>
public static HELPINFO UnmarshalFrom(IntPtr lParam)
{
return (HELPINFO)Marshal.PtrToStructure(lParam,
typeof(HELPINFO));
}
};

/// <summary>
/// Delegate declaration for a callback that gets called when the
help button is pushed.
/// </summary>
public delegate void MsgBoxCallback(HELPINFO lpHelpInfo);

/// <summary>
/// From winuser.h
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MSGBOXPARAMS
{
/// <summary>
///
/// </summary>
public uint cbSize;
/// <summary>
///
/// </summary>
public IntPtr hwndOwner;
/// <summary>
///
/// </summary>
public IntPtr hInstance;
/// <summary>
///
/// </summary>
public String lpszText;
/// <summary>
///
/// </summary>
public String lpszCaption;
/// <summary>
///
/// </summary>
public uint dwStyle;
/// <summary>
///
/// </summary>
public IntPtr lpszIcon;
/// <summary>
///
/// </summary>
public IntPtr dwContextHelpId;
/// <summary>
///
/// </summary>
public MsgBoxCallback lpfnMsgBoxCallback;
/// <summary>
///
/// </summary>
public uint dwLanguageId;
};

/// <summary>
/// The actual MessageBoxIndirect API declaration.
/// </summary>
/// <param name="msgboxParams"></param>
/// <returns></returns>
[DllImport("user32", EntryPoint = "MessageBoxIndirect")]
private static extern int _MessageBoxIndirect(ref MSGBOXPARAMS
msgboxParams);

public Form1()
{
InitializeComponent();
}

private void btnWin32WithContext_Click(object sender, EventArgs e)
{
MSGBOXPARAMS parms = new MSGBOXPARAMS();
parms.dwStyle = 0x00000000 | 0x00001000; // MB_OK |
MB_SYSTEMMODAL
parms.lpszText = "testing";
parms.lpszCaption = "test caption";
parms.hwndOwner = this.Handle; // Window context
parms.hInstance = IntPtr.Zero;
parms.cbSize = (uint)Marshal.SizeOf(typeof(MSGBOXPARAMS));
parms.lpszIcon = IntPtr.Zero;

DialogResult dr = (DialogResult)_MessageBoxIndirect(ref parms);
}

private void btnFrameworkWithContext_Click(object sender, EventArgs e)
{
MessageBox.Show(
this,
"testing",
"test caption",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}

private void btnWin32WithOUTContext_Click(object sender, EventArgs e)
{
MSGBOXPARAMS parms = new MSGBOXPARAMS();
parms.dwStyle = 0x00000000 | 0x00001000; // MB_OK |
MB_SYSTEMMODAL
parms.lpszText = "testing";
parms.lpszCaption = "test caption";
parms.hwndOwner = IntPtr.Zero; // Window context
parms.hInstance = IntPtr.Zero;
parms.cbSize = (uint)Marshal.SizeOf(typeof(MSGBOXPARAMS));
parms.lpszIcon = IntPtr.Zero;

DialogResult dr = (DialogResult)_MessageBoxIndirect(ref parms);
}

private void btnFrameworkWithOUTContext_Click(object sender,
EventArgs e)
{
MessageBox.Show(
"testing",
"test caption",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Nathan,

Can you post a more complete example? You are missing all of the code
in InitializeComponent.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Wiegman said:
Hi,

I am wondering why the .NET Framework is quite different from Win32 API
when
it comes to displaying system modal message boxes.

Consider the four following types of system modal message boxes (please
see
associated source code below):
1) Win32 API with context ("btnWin32WithContext_Click")
2) .NET Framework with context ("btnFrameworkWithContext_Click")
3) Win32 API withOUT context ("btnWin32WithOUTContext_Click")
4) .NET Framework withOUT context ("btnFrameworkWithOUTContext_Click")

Message box (1) will display on the same screen as the caller.
Message box (2) will throw an exception.
Message box (3) will display on the same screen as the caller.
Message box (4) will NOT display on the same screen as the caller.

I would like to use the .NET Framework to display a system modal message
box
on the same screen as the caller. Any ideas why this doesn't work in (2)
or
(4)?

Thanks,
Nate



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

namespace DifferentTypesOfSystemModalMessageBoxes
{
public partial class Form1 : Form
{
/// <summary>
/// Standard Windows point
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
/// <summary>
/// x
/// </summary>
public long x;
/// <summary>
/// y
/// </summary>
public long y;
};

/// <summary>
/// From winuser.h
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct HELPINFO
{
/// <summary>
///
/// </summary>
public uint cbSize;
/// <summary>
///
/// </summary>
public int iContextType;
/// <summary>
///
/// </summary>
public int iCtrlId;
/// <summary>
///
/// </summary>
public IntPtr hItemHandle;
/// <summary>
///
/// </summary>
public IntPtr dwContextId;
/// <summary>
///
/// </summary>
public POINT MousePos;

/// <summary>
/// Unmarshal the helpinfo out of the given intptr, which is
presumably the lParam received
/// in a WM_HELP message.
/// </summary>
/// <param name="lParam"></param>
/// <returns></returns>
public static HELPINFO UnmarshalFrom(IntPtr lParam)
{
return (HELPINFO)Marshal.PtrToStructure(lParam,
typeof(HELPINFO));
}
};

/// <summary>
/// Delegate declaration for a callback that gets called when the
help button is pushed.
/// </summary>
public delegate void MsgBoxCallback(HELPINFO lpHelpInfo);

/// <summary>
/// From winuser.h
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MSGBOXPARAMS
{
/// <summary>
///
/// </summary>
public uint cbSize;
/// <summary>
///
/// </summary>
public IntPtr hwndOwner;
/// <summary>
///
/// </summary>
public IntPtr hInstance;
/// <summary>
///
/// </summary>
public String lpszText;
/// <summary>
///
/// </summary>
public String lpszCaption;
/// <summary>
///
/// </summary>
public uint dwStyle;
/// <summary>
///
/// </summary>
public IntPtr lpszIcon;
/// <summary>
///
/// </summary>
public IntPtr dwContextHelpId;
/// <summary>
///
/// </summary>
public MsgBoxCallback lpfnMsgBoxCallback;
/// <summary>
///
/// </summary>
public uint dwLanguageId;
};

/// <summary>
/// The actual MessageBoxIndirect API declaration.
/// </summary>
/// <param name="msgboxParams"></param>
/// <returns></returns>
[DllImport("user32", EntryPoint = "MessageBoxIndirect")]
private static extern int _MessageBoxIndirect(ref MSGBOXPARAMS
msgboxParams);

public Form1()
{
InitializeComponent();
}

private void btnWin32WithContext_Click(object sender, EventArgs e)
{
MSGBOXPARAMS parms = new MSGBOXPARAMS();
parms.dwStyle = 0x00000000 | 0x00001000; // MB_OK |
MB_SYSTEMMODAL
parms.lpszText = "testing";
parms.lpszCaption = "test caption";
parms.hwndOwner = this.Handle; // Window context
parms.hInstance = IntPtr.Zero;
parms.cbSize = (uint)Marshal.SizeOf(typeof(MSGBOXPARAMS));
parms.lpszIcon = IntPtr.Zero;

DialogResult dr = (DialogResult)_MessageBoxIndirect(ref parms);
}

private void btnFrameworkWithContext_Click(object sender, EventArgs
e)
{
MessageBox.Show(
this,
"testing",
"test caption",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}

private void btnWin32WithOUTContext_Click(object sender, EventArgs
e)
{
MSGBOXPARAMS parms = new MSGBOXPARAMS();
parms.dwStyle = 0x00000000 | 0x00001000; // MB_OK |
MB_SYSTEMMODAL
parms.lpszText = "testing";
parms.lpszCaption = "test caption";
parms.hwndOwner = IntPtr.Zero; // Window context
parms.hInstance = IntPtr.Zero;
parms.cbSize = (uint)Marshal.SizeOf(typeof(MSGBOXPARAMS));
parms.lpszIcon = IntPtr.Zero;

DialogResult dr = (DialogResult)_MessageBoxIndirect(ref parms);
}

private void btnFrameworkWithOUTContext_Click(object sender,
EventArgs e)
{
MessageBox.Show(
"testing",
"test caption",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
}
}
 
G

Guest

Hi Nicholas,

The UI is just a simple form with four buttons. Here is the .resx file and
the designer generated source.

Thanks,
Nate

Form1.resx

<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader,
System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter,
System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is
a comment</comment></data>
<data name="Color1" type="System.Drawing.Color,
System.Drawing">Blue</data>
<data name="Bitmap1"
mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing"
mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of
the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns=""
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0"
msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0"
msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required"
msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string"
msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string"
msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0"
msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
Form1.Designer.cs

namespace DifferentTypesOfSystemModalMessageBoxes
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.grpWithWindowContext = new System.Windows.Forms.GroupBox();
this.lblInstructions = new System.Windows.Forms.Label();
this.btnWin32WithContext = new System.Windows.Forms.Button();
this.btnFrameworkWithContext = new System.Windows.Forms.Button();
this.grpWithoutWindowContext = new
System.Windows.Forms.GroupBox();
this.btnFrameworkWithOUTContext = new
System.Windows.Forms.Button();
this.btnWin32WithOUTContext = new System.Windows.Forms.Button();
this.grpWithWindowContext.SuspendLayout();
this.grpWithoutWindowContext.SuspendLayout();
this.SuspendLayout();
//
// grpWithWindowContext
//

this.grpWithWindowContext.Controls.Add(this.btnFrameworkWithContext);
this.grpWithWindowContext.Controls.Add(this.btnWin32WithContext);
this.grpWithWindowContext.Location = new
System.Drawing.Point(12, 53);
this.grpWithWindowContext.Name = "grpWithWindowContext";
this.grpWithWindowContext.Size = new System.Drawing.Size(146, 80);
this.grpWithWindowContext.TabIndex = 0;
this.grpWithWindowContext.TabStop = false;
this.grpWithWindowContext.Text = "With Window Context";
//
// lblInstructions
//
this.lblInstructions.Location = new System.Drawing.Point(12, 9);
this.lblInstructions.Name = "lblInstructions";
this.lblInstructions.Size = new System.Drawing.Size(146, 41);
this.lblInstructions.TabIndex = 1;
this.lblInstructions.Text = "Click buttons to show system modal
message boxes with different options";
this.lblInstructions.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// btnWin32WithContext
//
this.btnWin32WithContext.Location = new System.Drawing.Point(6,
19);
this.btnWin32WithContext.Name = "btnWin32WithContext";
this.btnWin32WithContext.Size = new System.Drawing.Size(134, 23);
this.btnWin32WithContext.TabIndex = 2;
this.btnWin32WithContext.Text = "Win32 API";
this.btnWin32WithContext.UseVisualStyleBackColor = true;
this.btnWin32WithContext.Click += new
System.EventHandler(this.btnWin32WithContext_Click);
//
// btnFrameworkWithContext
//
this.btnFrameworkWithContext.Location = new
System.Drawing.Point(6, 48);
this.btnFrameworkWithContext.Name = "btnFrameworkWithContext";
this.btnFrameworkWithContext.Size = new System.Drawing.Size(134,
23);
this.btnFrameworkWithContext.TabIndex = 3;
this.btnFrameworkWithContext.Text = "Dot Net 2.0";
this.btnFrameworkWithContext.UseVisualStyleBackColor = true;
this.btnFrameworkWithContext.Click += new
System.EventHandler(this.btnFrameworkWithContext_Click);
//
// grpWithoutWindowContext
//

this.grpWithoutWindowContext.Controls.Add(this.btnFrameworkWithOUTContext);

this.grpWithoutWindowContext.Controls.Add(this.btnWin32WithOUTContext);
this.grpWithoutWindowContext.Location = new
System.Drawing.Point(12, 139);
this.grpWithoutWindowContext.Name = "grpWithoutWindowContext";
this.grpWithoutWindowContext.Size = new System.Drawing.Size(146,
80);
this.grpWithoutWindowContext.TabIndex = 2;
this.grpWithoutWindowContext.TabStop = false;
this.grpWithoutWindowContext.Text = "Without Window Context";
//
// btnFrameworkWithOUTContext
//
this.btnFrameworkWithOUTContext.Location = new
System.Drawing.Point(6, 48);
this.btnFrameworkWithOUTContext.Name =
"btnFrameworkWithOUTContext";
this.btnFrameworkWithOUTContext.Size = new
System.Drawing.Size(134, 23);
this.btnFrameworkWithOUTContext.TabIndex = 3;
this.btnFrameworkWithOUTContext.Text = "Dot Net 2.0";
this.btnFrameworkWithOUTContext.UseVisualStyleBackColor = true;
this.btnFrameworkWithOUTContext.Click += new
System.EventHandler(this.btnFrameworkWithOUTContext_Click);
//
// btnWin32WithOUTContext
//
this.btnWin32WithOUTContext.Location = new
System.Drawing.Point(6, 19);
this.btnWin32WithOUTContext.Name = "btnWin32WithOUTContext";
this.btnWin32WithOUTContext.Size = new System.Drawing.Size(134,
23);
this.btnWin32WithOUTContext.TabIndex = 2;
this.btnWin32WithOUTContext.Text = "Win32 API";
this.btnWin32WithOUTContext.UseVisualStyleBackColor = true;
this.btnWin32WithOUTContext.Click += new
System.EventHandler(this.btnWin32WithOUTContext_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(173, 232);
this.Controls.Add(this.grpWithoutWindowContext);
this.Controls.Add(this.lblInstructions);
this.Controls.Add(this.grpWithWindowContext);
this.Name = "Form1";
this.Text = "MessageBox";
this.grpWithWindowContext.ResumeLayout(false);
this.grpWithoutWindowContext.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.GroupBox grpWithWindowContext;
private System.Windows.Forms.Button btnFrameworkWithContext;
private System.Windows.Forms.Button btnWin32WithContext;
private System.Windows.Forms.Label lblInstructions;
private System.Windows.Forms.GroupBox grpWithoutWindowContext;
private System.Windows.Forms.Button btnFrameworkWithOUTContext;
private System.Windows.Forms.Button btnWin32WithOUTContext;

}
}

Nicholas Paldino said:
Nathan,

Can you post a more complete example? You are missing all of the code
in InitializeComponent.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Wiegman said:
Hi,

I am wondering why the .NET Framework is quite different from Win32 API
when
it comes to displaying system modal message boxes.

Consider the four following types of system modal message boxes (please
see
associated source code below):
1) Win32 API with context ("btnWin32WithContext_Click")
2) .NET Framework with context ("btnFrameworkWithContext_Click")
3) Win32 API withOUT context ("btnWin32WithOUTContext_Click")
4) .NET Framework withOUT context ("btnFrameworkWithOUTContext_Click")

Message box (1) will display on the same screen as the caller.
Message box (2) will throw an exception.
Message box (3) will display on the same screen as the caller.
Message box (4) will NOT display on the same screen as the caller.

I would like to use the .NET Framework to display a system modal message
box
on the same screen as the caller. Any ideas why this doesn't work in (2)
or
(4)?

Thanks,
Nate



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

namespace DifferentTypesOfSystemModalMessageBoxes
{
public partial class Form1 : Form
{
/// <summary>
/// Standard Windows point
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
/// <summary>
/// x
/// </summary>
public long x;
/// <summary>
/// y
/// </summary>
public long y;
};

/// <summary>
/// From winuser.h
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct HELPINFO
{
/// <summary>
///
/// </summary>
public uint cbSize;
/// <summary>
///
/// </summary>
public int iContextType;
/// <summary>
///
/// </summary>
public int iCtrlId;
/// <summary>
///
/// </summary>
public IntPtr hItemHandle;
/// <summary>
///
/// </summary>
public IntPtr dwContextId;
/// <summary>
///
/// </summary>
public POINT MousePos;

/// <summary>
/// Unmarshal the helpinfo out of the given intptr, which is
presumably the lParam received
/// in a WM_HELP message.
/// </summary>
/// <param name="lParam"></param>
/// <returns></returns>
public static HELPINFO UnmarshalFrom(IntPtr lParam)
{
return (HELPINFO)Marshal.PtrToStructure(lParam,
typeof(HELPINFO));
}
};

/// <summary>
/// Delegate declaration for a callback that gets called when the
help button is pushed.
/// </summary>
public delegate void MsgBoxCallback(HELPINFO lpHelpInfo);

/// <summary>
/// From winuser.h
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MSGBOXPARAMS
{
/// <summary>
///
/// </summary>
public uint cbSize;
/// <summary>
///
/// </summary>
public IntPtr hwndOwner;
/// <summary>
///
/// </summary>
public IntPtr hInstance;
/// <summary>
///
/// </summary>
public String lpszText;
/// <summary>
///
/// </summary>
public String lpszCaption;
/// <summary>
///
/// </summary>
public uint dwStyle;
/// <summary>
///
/// </summary>
public IntPtr lpszIcon;
/// <summary>
///
/// </summary>
public IntPtr dwContextHelpId;
/// <summary>
///
/// </summary>
public MsgBoxCallback lpfnMsgBoxCallback;
/// <summary>
///
/// </summary>
public uint dwLanguageId;
};

/// <summary>
/// The actual MessageBoxIndirect API declaration.
/// </summary>
/// <param name="msgboxParams"></param>
/// <returns></returns>
[DllImport("user32", EntryPoint = "MessageBoxIndirect")]
private static extern int _MessageBoxIndirect(ref MSGBOXPARAMS
msgboxParams);

public Form1()
{
InitializeComponent();
}

private void btnWin32WithContext_Click(object sender, EventArgs e)
{
MSGBOXPARAMS parms = new MSGBOXPARAMS();
parms.dwStyle = 0x00000000 | 0x00001000; // MB_OK |
MB_SYSTEMMODAL
parms.lpszText = "testing";
parms.lpszCaption = "test caption";
parms.hwndOwner = this.Handle; // Window context
parms.hInstance = IntPtr.Zero;
parms.cbSize = (uint)Marshal.SizeOf(typeof(MSGBOXPARAMS));
parms.lpszIcon = IntPtr.Zero;

DialogResult dr = (DialogResult)_MessageBoxIndirect(ref parms);
}

private void btnFrameworkWithContext_Click(object sender, EventArgs
e)
{
MessageBox.Show(
this,
"testing",
"test caption",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}

private void btnWin32WithOUTContext_Click(object sender, EventArgs
e)
{
MSGBOXPARAMS parms = new MSGBOXPARAMS();
parms.dwStyle = 0x00000000 | 0x00001000; // MB_OK |
MB_SYSTEMMODAL
parms.lpszText = "testing";
parms.lpszCaption = "test caption";
parms.hwndOwner = IntPtr.Zero; // Window context
parms.hInstance = IntPtr.Zero;
parms.cbSize = (uint)Marshal.SizeOf(typeof(MSGBOXPARAMS));
parms.lpszIcon = IntPtr.Zero;

DialogResult dr = (DialogResult)_MessageBoxIndirect(ref parms);
}

private void btnFrameworkWithOUTContext_Click(object sender,
EventArgs e)
{
MessageBox.Show(
"testing",
"test caption",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
}
}
 
Top