[PB2CS]Beep(n)的处理
PowerBuilder中的Beep()是通过声卡发声,它调用Windows系统的默认响声,当不能播放默认响声时,就会在蜂鸣器上发出嘟嘟声。C#中的Console.Beep()仅仅通过蜂鸣器发声,不能替代PowerBuilder的Beep(),必须寻找其它能调用系统默认声响的发声函数,MessageBeep函数可以做到这点。该函数主要用来播放系统报警声音,如果不能播放SystemDefault定义的系统缺省声音,它就会在计算机的蜂鸣器上发出蜂鸣声。函数的声明为:
BOOL MessageBeep(UINT uType);
参数uType说明了告警级,如下表所示(在缺省时下表的MB_系列声音均未定义)
| Value | Meaning |
| -1 |
Simple beep. If the sound card is not available, the sound is generated using the speaker. Note that this value is resolved to 0xFFFFFFFF within the function. |
| MB_ICONASTERISK 0x00000040L |
SystemAsterisk |
| MB_ICONEXCLAMATION 0x00000030L |
SystemExclamation |
| MB_ICONHAND 0x00000010L |
SystemHand |
| MB_ICONQUESTION 0x00000020L |
SystemQuestion |
| MB_OK 0x00000000L |
SystemDefault |
可以使用下面的代码来完成Beep(n)到C#的转换[DllImport("user32.dll", SetLastError=true)]public static extern bool MessageBeep(UInt32 beepType);
MessageBeep(0xFFFFFFFF);
System.Media.SystemSounds.Beep.Play() // 更好的转换方式,使用了dotnet平台提供的方法
BTW:通过实践发现,PowerBuilder中Beep(n)函数的响铃次数参数好像不起作用,不管设为多少,就是响一下。
Related Posts
People who read this, also read...
[PB2CS]MessageBox的转换
PowerBuilder中MessageBox的格式:MessageBox(title, text {, icon {, button {, default } } } )
C#中MessageBox的格式:MessageBox(text, title {, button {, icon {, default } } } )
在PowerBuilder中MessageBox默认是有icon的,为Information。在C#中默认没有icon,如图:

所以在将PowerBuilder中的MessageBox转换到C#时,遇到没有icon的都必须添加一个Information的icon。
| PowerBuilder | C# |
| MessageBox(title, text) | MessageBox.Show(text, title, OK, Information) |
| MessageBox(title, text, icon) | MessageBox.Show(text, title, OK, icon) |
| MessageBox(title, text, icon, button) | MessageBox.Show(text, title, button, icon) |
| MessageBox(title, text, icon, button, default) | MessageBox.Show(text, title, button, icon, default) |
另外,C#中MessageBox返回值是DialogResult类型,这是一个enum类型。PowerBuilder中的MessageBox则是返回一个整型,因此转换到C#时需要对返回值做一些处理。
第一种处理方式:
DialogResult dResult = MessageBox.Show(...)
if (dResult == DialogReslt.Yes) { ... }
第二种处理方式:
DialogResult dResult = MessageBox.Show(...);
int ret = (int)dResult;
if (ret == 1) { ... }
或直接就是
int ret = (int)MessageBox.Show(...)
if (ret == 1) { ... }
| PowerBuilder | C# |
| OK! (Default) | OK (Default) |
| OKCancel! | OKCancel |
| YesNo! | YesNo |
| YesNoCancel! | YesNoCancel |
| RetryCancel! | RetryCancel |
| AbortRetryIgnore! | AbortRetryIgnore |
| PowerBuilder | C# |
| Information! (Default) | Information |
| StopSign! | Stop |
| Exclamation! | Exclamation |
| Question! | Question |
| None! | None (Default) |
| PowerBuilder | C# |
| 1 (Default) | Button1 (Default) |
| 2 | Button2 |
| 3 | Button3 |
Related Posts
People who read this, also read...
[PB2CS]decimal{int}和Round(decimal, int)的转换
在PowerBuilder中decimal{int}和Round(decimal, int)是等价的,例如:
decimal{2} unit_price
unit_price = 3.455 => unit_price = 3.46
等价于
decimal unit_price
unit_price = Round(3.455, 2) => unit_price = 3.46
另外,在PowerBuilder中Round采用的是四舍五入算法,因为四舍五入法总是逢五进位,除非样本中保留精度位不出现5,否则结果必然偏大。所以大多数程序语言都使用计算精度比四舍五入法高的Banker's rounding(银行家舍入)算法,即四舍六入算法。事实上这也是IEEE规定的舍入标准。C#.net中的Math.Round默认使用的也是Banker舍入法。
Math.Round有几个重载方法:
Math.Round(Decimal, MidpointRounding)
Math.Round(Double, MidpointRounding)
Math.Round(Decimal, Int32, MidpointRounding)
Math.Round(Double, Int32, MidpointRounding)
MidpointRounding参数是个枚举类型,有两个成员,该参数指定当一个值正好处于另两个数中间时如何舍入这个值。MSDN中的说明是:
ToEven 当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。
AwayFromZero 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
注意!这里关于MidpointRounding.AwayFromZero的说明是错误的!应该是为舍入为两个值中绝对值较大的值。
由于Math.Round默认使用的是MidpointRounding.ToEven,所以在将PowerBuilder中的Round转换到C#.net时一定要加一个MidpointRounding.AwayFromZero参数:
Round(decimal, int) => Math.Round(decimal, int, MidpointRounding.AwayFromZero)
同样,转换decimal{int}可以采用如下方式:
decimal{int} total_amount => decimal total_amount
total_amount = value => total_amount = Decimal.Round(value, int, MidpointRounding.AwayFromZero)
四舍六入算法
口诀:四舍六入五考虑,五后非零必进一。五后皆零视奇偶,五前为偶应舍去,五前为奇则进一。
1. 若被舍弃的第一位数字小于5,则其前一位保持不变。
2. 若被舍弃的第一位数字大于5,则其前一位数字加1。
3. 若被舍弃的第一位数字等于5,而其后数字全部为零,则视被保留的末位数字为奇数还是偶数而定进或舍。奇数时进一,偶数时舍。
4. 若被舍弃的第一位数字等于5,而其后面的数字并非全部为零,则进一。
5. 若被舍弃的数字包括几位数字时,不得对该数字进行连续的进位或舍弃,而应根据以上各条作一次处理。
6. 整数的修约也应遵照上述法则。
// PowerBuilder测试数据
decimal{2} unit_price
decimal{0} total_price
unit_price = 3.455 => 3.46
unit_price = 3.465 => 3.47
unit_price = -3.455 => -3.46
unit_price = -3.465 => -3.47
total_price = 3.5 => 4
total_price = 4.5 => 5
total_price = -3.5 => -4
total_price = -4.5 => -5
// C#测试数据
Math.Round(3.455, 2, MidpointRounding.ToEven) => 3.46
Math.Round(3.455, 2, MidpointRounding.AwayFromZero) => 3.46
Math.Round(3.465, 2, MidpointRounding.ToEven) => 3.46
Math.Round(3.465, 2, MidpointRounding.AwayFromZero) => 3.47
Math.Round(-3.455, 2, MidpointRounding.ToEven) => -3.46
Math.Round(-3.455, 2, MidpointRounding.AwayFromZero) => -3.46
Math.Round(-3.465, 2, MidpointRounding.ToEven) => -3.46
Math.Round(-3.465, 2, MidpointRounding.AwayFromZero) => -3.47
Math.Round(3.5, 0, MidpointRounding.ToEven) => 4
Math.Round(3.5, 0, MidpointRounding.AwayFromZero) => 4
Math.Round(4.5, 0, MidpointRounding.ToEven) => 4
Math.Round(4.5, 0, MidpointRounding.AwayFromZero) => 5
Math.Round(-3.5, 0, MidpointRounding.ToEven) => -4
Math.Round(-3.5, 0, MidpointRounding.AwayFromZero) => -4
Math.Round(-4.5, 0, MidpointRounding.ToEven) => -4
Math.Round(-4.5, 0, MidpointRounding.AwayFromZero) => -5

