我正在用 C# 编写一个警报程序,它在指定时间显示一个带有用户指定消息的系统模式对话框。但是,我似乎找不到 C# 等价物
MessageBoxA(HWND_DESKTOP, msg, "Alarm",
MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL | MB_SETFOREGROUND);
编辑:我正在尝试学习 C# 和 .NET 库。我认为编写与我用 C 或 C++ 编写的一些小程序等效的程序是一个不错的起点。
只需使用您在标题中提到的API ...
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct HWND__ {
/// int
public int unused;
}
public partial class NativeMethods {
/// Return Type: int
///hWnd: HWND->HWND__*
///lpText: LPCSTR->CHAR*
///lpCaption: LPCSTR->CHAR*
///uType: UINT->unsigned int
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint="MessageBoxA")]
public static extern int MessageBoxA([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, [System.Runtime.InteropServices.InAttribute()][System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpText, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpCaption, uint uType) ;
}
public partial class NativeConstants {
/// MB_SETFOREGROUND -> 0x00010000L
public const int MB_SETFOREGROUND = 65536;
/// MB_SYSTEMMODAL -> 0x00001000L
public const int MB_SYSTEMMODAL = 4096;
/// MB_ICONWARNING -> MB_ICONEXCLAMATION
public const int MB_ICONWARNING = NativeConstants.MB_ICONEXCLAMATION;
/// MB_OK -> 0x00000000L
public const int MB_OK = 0;
/// MB_ICONEXCLAMATION -> 0x00000030L
public const int MB_ICONEXCLAMATION = 48;
}
像这样的东西应该适合你:
MessageBox.Show("text", "caption", MessageBoxButtons.OK, MessageBoxIcon.Warning);
在 MSDN 上了解更多信息:http: //msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show.aspx
编辑: 就像另一种想法一样,这将创建屏幕大小的形式,并通过阻止屏幕上的所有其他内容来显示消息框,直到您关闭该消息框。
internal class TransparentWholeScreen: Form
{
public TransparentWholeScreen()
{
Size = Screen.PrimaryScreen.Bounds.Size;
TopMost = true;
FormBorderStyle = FormBorderStyle.None;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
Shown += OnShown;
}
private void OnShown(object sender, EventArgs e)
{
var dialogResult = MessageBox.Show("text", "caption", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if(dialogResult == DialogResult.OK)
{
Close();
}
}
}
只需在闹钟时间结束时添加以下代码:
var backGroundForm = new TransparentWholeScreen();
backGroundForm.ShowDialog(this);
老实说,我不喜欢这个解决方案,除了它不会作为可以杀死进程的人的警报:)