我试图在C#中调用WinAPI DialogBox()函数(我无法访问System.Windows库以使用MessageBox)但是我无法确定用于参数的正确类型,但HWND是IntPtr 。 MSDN有:
INT_PTR WINAPI DialogBox(
_In_opt_ HINSTANCE hInstance,
_In_ LPCTSTR lpTemplate,
_In_opt_ HWND hWndParent,
_In_opt_ DLGPROC lpDialogFunc
);
到目前为止,我有:
[DllImport( "user32.dll" )]
static extern IntPtr DialogBox( ? hInstance, string lpTemplate, IntPtr hWnd, ? lpDialogFunc );
有人能帮助解决这个问题吗? (我认为HINSTANCE可能是一个int,但可以设置为NULL)。
答案 0 :(得分:0)
这是在不久前直接从Winforms源代码中获取的,我建议你在使用前查看代码。
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetActiveWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(HandleRef hWnd, string text, string caption, int type);
private static DialogResult GetResult(int val)
{
switch (val)
{
case 1:
return DialogResult.OK;
case 2:
return DialogResult.Cancel;
case 3:
return DialogResult.Abort;
case 4:
return DialogResult.Retry;
case 5:
return DialogResult.Ignore;
case 6:
return DialogResult.Yes;
case 7:
return DialogResult.No;
default:
return DialogResult.No;
}
}
IntPtr handle = GetActiveWindow();
DialogResult dialogResult = GetResult(MessageBox(new HandleRef((object) this.Handle, handle), "Test", "test", 1));
答案 1 :(得分:-1)
user32.dll中还有一个MessageBox()函数; MSDN页面为here。 C#中的声明代码是:
[DllImport( "user32.dll" )]
static extern int MessageBox( IntPtr hInstance, string lpText, string lpCaption, uint type );
private const uint MB_OK = 0x0;
private const uint MB_ICONASTERISK = 0x00000040;
我希望这有助于其他可能遇到类似问题的人。