我正在用C#编写一个警报程序,它在指定的时间用用户指定的消息显示系统模式对话框。但是,我似乎找不到与C#等价的
MessageBoxA(HWND_DESKTOP, msg, "Alarm",
MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL | MB_SETFOREGROUND);
编辑:,我正在努力学习C#和.NET库。我认为编写相当于我用C或C++编写的一些小程序的程序是一个很好的起点。
发布于 2012-10-25 18:02:52
像这样的东西应该对你有用:
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);
老实说,我不喜欢这个解决方案,而且它不会对那些可以杀死进程的人起到警示作用:)
发布于 2012-10-25 18:25:49
只用你在标题中提到的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;
}
https://stackoverflow.com/questions/13074421
复制相似问题