[原创]在C#中申明和使用MessageBox 这个API函数 ( 积分: 1 )

  • 主题发起人 主题发起人 dcms
  • 开始时间 开始时间
D

dcms

Unregistered / Unconfirmed
GUEST, unregistred user!
[原创]在C#中申明和使用MessageBox 这个API函数
http://www.dcmscn.cn/dispbbs.asp?boardID=13&ID=416&page=1
大家如果需要什么API不知道在C#中怎么定义的可以告诉我,我来帮你做哦!
 
[原创]在C#中申明和使用MessageBox 这个API函数
http://www.dcmscn.cn/dispbbs.asp?boardID=13&ID=416&page=1
大家如果需要什么API不知道在C#中怎么定义的可以告诉我,我来帮你做哦!
 
轮子已经有了,何必再重新造呢:
System.Windows.Forms.MessageBox.Show(...);
结合上
public enum System.Windows.Forms.MessageBoxButtons
{
AbortRetryIgnore = 0x00000002,
OK = 0x00000000,
OKCancel = 0x00000001,
RetryCancel = 0x00000005,
YesNo = 0x00000004,
YesNoCancel = 0x00000003,
}
public enum System.Windows.Forms.MessageBoxIcon
{
Asterisk = 0x00000040,
Error = 0x00000010,
Exclamation = 0x00000030,
Hand = 0x00000010,
Information = 0x00000040,
None = 0x00000000,
Question = 0x00000020,
Stop = 0x00000010,
Warning = 0x00000030,
}
还实现不了MessageBox这个API函数的功能吗?
 
楼主探讨的是造轮子的方法
 
呵呵,轮子的制造也的讲究一点点技巧啊,看陈宽达的<Delphi深度历险>的时候,很佩服作者游刃有余的穿插使用两个类库,那么我们也可以效仿一下^-^
如下为楼主的原文,我把编译指令去掉先:
/* MessageBox() Flags */
//{$EXTERNALSYM MB_OK}
const uint MB_OK = 0x00000;
//{$EXTERNALSYM MB_OKCANCEL}
const uint MB_OKCANCEL = 0x00000001;
//{$EXTERNALSYM MB_ABORTRETRYIGNORE}
const uint MB_ABORTRETRYIGNORE = 0x00000002;
//{$EXTERNALSYM MB_YESNOCANCEL}
const uint MB_YESNOCANCEL = 0x00000003;
//{$EXTERNALSYM MB_YESNO}
const uint MB_YESNO = 0x00000004;
//{$EXTERNALSYM MB_RETRYCANCEL}
const uint MB_RETRYCANCEL = 0x00000005;
如上的代码就变成了:
//******************************************
const uint MB_OK = 0x00000;
const uint MB_OKCANCEL = 0x00000001;
const uint MB_ABORTRETRYIGNORE = 0x00000002;
const uint MB_YESNOCANCEL = 0x00000003;
const uint MB_YESNO = 0x00000004;
const uint MB_RETRYCANCEL = 0x00000005;
//******************************************
那么在看看Enum System.Windows.Forms.MessageBoxButtons 的定义:
public enum System.Windows.Forms.MessageBoxButtons
{

OK = 0x00000000,
OKCancel = 0x00000001,
AbortRetryIgnore = 0x00000002,
YesNoCancel = 0x00000003,
YesNo = 0x00000004,
RetryCancel = 0x00000005,
}
看看二者居然惊人的一致!
同样的在看看楼主下面的代码:
//{$EXTERNALSYM MB_ICONHAND}
const uint MB_ICONHAND = 0x00000010;
//{$EXTERNALSYM MB_ICONQUESTION}
const uint MB_ICONQUESTION = 0x00000020;
//{$EXTERNALSYM MB_ICONEXCLAMATION}
const uint MB_ICONEXCLAMATION = 0x00000030;
//{$EXTERNALSYM MB_ICONASTERISK}
const uint MB_ICONASTERISK = 0x00000040;
//{$EXTERNALSYM MB_USERICON}
const uint MB_USERICON = 0x00000080;
//{$EXTERNALSYM MB_ICONWARNING}
const uint MB_ICONWARNING = MB_ICONEXCLAMATION;
//{$EXTERNALSYM MB_ICONERROR}
const uint MB_ICONERROR = MB_ICONHAND;
//{$EXTERNALSYM MB_ICONINFORMATION}
const uint MB_ICONINFORMATION = MB_ICONASTERISK;
//{$EXTERNALSYM MB_ICONSTOP}
const uint MB_ICONSTOP = MB_ICONHAND;
去掉编译指令,变成了:
//************************************************
const uint MB_ICONHAND = 0x00000010;
const uint MB_ICONQUESTION = 0x00000020;
const uint MB_ICONEXCLAMATION = 0x00000030;
const uint MB_ICONASTERISK = 0x00000040;
const uint MB_USERICON = 0x00000080;
const uint MB_ICONWARNING = MB_ICONEXCLAMATION;
const uint MB_ICONERROR = MB_ICONHAND;
const uint MB_ICONINFORMATION = MB_ICONASTERISK;
const uint MB_ICONSTOP = MB_ICONHAND;
//************************************************
上面是用常量又定义了常量,去掉常量,就成了如下:
//************************************************
const uint MB_ICONHAND = 0x00000010;
const uint MB_ICONQUESTION = 0x00000020;
const uint MB_ICONEXCLAMATION = 0x00000030;
const uint MB_ICONASTERISK = 0x00000040;
const uint MB_USERICON = 0x00000080;
const uint MB_ICONWARNING = 0x00000030;
const uint MB_ICONERROR = 0x00000010;
const uint MB_ICONINFORMATION = 0x00000030;
const uint MB_ICONSTOP = 0x00000010;
//************************************************

再看看enum System.Windows.Forms.MessageBoxIcon 的实现:
public enum System.Windows.Forms.MessageBoxIcon
{
Hand = 0x00000010,
Question = 0x00000020,
Exclamation = 0x00000030,
Asterisk = 0x00000040,
//这个对不上,另外声明:const uint MB_USERICON = 0x00000080;
None = 0x00000000,
Warning = 0x00000030,
Error = 0x00000010,
Information = 0x00000040,
Stop = 0x00000010,
}
除了没有声明
const uint MB_USERICON = 0x00000080;
外,也是惊人的一致!!.
而Net FrameWork中多了
None = 0x00000000,
那么另外在代码中声明
const uint MB_USERICON = 0x00000080;
就可以使用了!
导入API:
[DllImport("user32.dll")]
public static extern int MessageBoxA(IntPtr HWND,string lpText, string lpCaption, uint uType);

使用例子;
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (MessageBoxA(this.Handle,"是否真的关闭程序","确认对话框",MB_OKCANCEL+MB_ICONWARNING) == 1)
e.Cancel = false;
else
e.Cancel = true;
}
//***************************************************
那么也就是说,那些个常量的声明都可以不要的,如上的例子程序就变成了:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
int MsgBoxResult = MessageBoxA(this.Handle,"是否真的关闭程序","确认对话框",MessageBoxButtons.OKCancel + MessageBoxIcon.Warning);
e.Cancel = !( MsgBoxResult == 1 );
}

//***************************************************
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
后退
顶部