为了给程序添加一个Balloon Notification又不使用opennetcf,就花了几个小时功夫把opennetcf里面有关Notification类的c#程序提取出来整理了,最终翻译成了VB.net,感触颇深。本人才疏学浅,就写写自己的一些感受吧~~
1、vb.net与c#之间还是有一定的差异,对于大段的程序用c# to vb.net Convertor程序反而起不到好大的效果,却会使程序变得混乱。建议小段程序使用Convertor,自己整理代码。
2、vb.net里面直接声明public event eventName(byval Param1 as object,...) 这样比用c#的委托那样搞过去搞过来写一大堆代码方便。我就郁闷c#里面没有看到过类似的用法呢?是不是c#不支持这样的语法哦~~声明:我对c#不是很熟悉哈~~~
3、Platform Invoke看起来比较复杂,但是做起来还是简单。只要理解了如何把各个变量的指针找到很多事情就好办了。Opennetcf里面又很多关于P/Invoke的例子,可以借鉴学习。
例如关于如何提取图标的例子(vb.net):
4、Opennetcf太大了,直接用不太好。如果需要其中很小一块功能而给客户安装一个新的类库有些浪费了,可以通过代码重写来达到要求。有些东西可以找替换的。比如在.net cf没有guid.newGuid方法,虽然opennetcf里面有GuidEx类,但如果层层深入翻译成vb.net代码会遇到很多问题。最为严重的就是c#里面的移位操作所带来的麻烦。这是,变通一下,找到了微软的MSDN里面的PocketGuid解决了这个问题(见下)
介绍文章请见:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/PPCGuidGen.asp
如果想直接应用opennetcf里面的guidex类的话vb.net会花费很大的功夫来改写,具体可以看看opennetcf的源代码。
6、最后一个:想得到,就做得到!
1、vb.net与c#之间还是有一定的差异,对于大段的程序用c# to vb.net Convertor程序反而起不到好大的效果,却会使程序变得混乱。建议小段程序使用Convertor,自己整理代码。
2、vb.net里面直接声明public event eventName(byval Param1 as object,...) 这样比用c#的委托那样搞过去搞过来写一大堆代码方便。我就郁闷c#里面没有看到过类似的用法呢?是不是c#不支持这样的语法哦~~声明:我对c#不是很熟悉哈~~~
3、Platform Invoke看起来比较复杂,但是做起来还是简单。只要理解了如何把各个变量的指针找到很多事情就好办了。Opennetcf里面又很多关于P/Invoke的例子,可以借鉴学习。
例如关于如何提取图标的例子(vb.net):
Private Declare Function ExtractIconEx()Function ExtractIconEx Lib "coredll.dll" (ByVal fileName As String, ByVal index As Integer, ByVal hIconLarge As Integer, ByRef hIconSmall As IntPtr, ByVal nIcons As Integer) As IntPtr
private mIcon as Intptr
ExtractIconEx(fullname, 0, 0, mIcon, 1)
private mIcon as Intptr
ExtractIconEx(fullname, 0, 0, mIcon, 1)
4、Opennetcf太大了,直接用不太好。如果需要其中很小一块功能而给客户安装一个新的类库有些浪费了,可以通过代码重写来达到要求。有些东西可以找替换的。比如在.net cf没有guid.newGuid方法,虽然opennetcf里面有GuidEx类,但如果层层深入翻译成vb.net代码会遇到很多问题。最为严重的就是c#里面的移位操作所带来的麻烦。这是,变通一下,找到了微软的MSDN里面的PocketGuid解决了这个问题(见下)
介绍文章请见:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/PPCGuidGen.asp
Imports System.Runtime.InteropServices
'
' Generate GUIDs on the .NET Compact Framework.
'
Public Class PocketGuidClass PocketGuid
' guid variant types
Private Enum GuidVariantEnum GuidVariant
ReservedNCS = &H0
Standard = &H2
ReservedMicrosoft = &H6
ReservedFuture = &H7
End Enum
' guid version types
Private Enum GuidVersionEnum GuidVersion
TimeBased = &H1
Reserved = &H2
NameBased = &H3
Random = &H4
End Enum
' constants that are used in the class
Private Class ConstValuesClass ConstValues
' number of bytes in guid
Public Const ByteArraySize As Integer = 16
' multiplex variant info
Public Const VariantByte As Integer = 8
Public Const VariantByteMask As Integer = &H3F
Public Const VariantByteShift As Integer = 6
' multiplex version info
Public Const VersionByte As Integer = 7
Public Const VersionByteMask As Integer = &HF
Public Const VersionByteShift As Integer = 4
End Class
' imports for the crypto api functions
Private Class WinApiClass WinApi
Public Const PROV_RSA_FULL As Integer = 1
Public Const CRYPT_VERIFYCONTEXT As Integer = &HF0000000
<DllImport("coredll.dll")> _
Public Shared Function CryptAcquireContext()Function CryptAcquireContext( _
ByRef phProv As IntPtr, ByVal pszContainer As String, _
ByVal pszProvider As String, ByVal dwProvType As Integer, _
ByVal dwFlags As Integer) As Boolean
End Function
<DllImport("coredll.dll")> _
Public Shared Function CryptReleaseContext()Function CryptReleaseContext( _
ByVal hProv As IntPtr, ByVal dwFlags As Integer) As Boolean
End Function
<DllImport("coredll.dll")> _
Public Shared Function CryptGenRandom()Function CryptGenRandom( _
ByVal hProv As IntPtr, ByVal dwLen As Integer, _
ByVal pbBuffer() As Byte) As Boolean
End Function
End Class
' all static methods
Private Sub New()Sub New()
End Sub
' Return a new System.Guid object.
Public Shared Function NewGuid()Function NewGuid() As Guid
Dim hCryptProv As IntPtr = IntPtr.Zero
Dim guid As Guid = guid.Empty
Try
' holds random bits for guid
Dim bits(ConstValues.ByteArraySize - 1) As Byte
' get crypto provider handle
If Not WinApi.CryptAcquireContext(hCryptProv, Nothing, Nothing, _
WinApi.PROV_RSA_FULL, WinApi.CRYPT_VERIFYCONTEXT) Then
Throw New SystemException( _
"Failed to acquire cryptography handle.")
End If
' generate a 128 bit (16 byte) cryptographically random number
If Not WinApi.CryptGenRandom(hCryptProv, bits.Length, bits) Then
Throw New SystemException( _
"Failed to generate cryptography random bytes.")
End If
' set the variant
bits(ConstValues.VariantByte) = bits(ConstValues.VariantByte) And _
CByte(ConstValues.VariantByteMask)
bits(ConstValues.VariantByte) = bits(ConstValues.VariantByte) Or _
CByte(GuidVariant.Standard << ConstValues.VariantByteShift)
' set the version
bits(ConstValues.VersionByte) = bits(ConstValues.VersionByte) And _
CByte(ConstValues.VersionByteMask)
bits(ConstValues.VersionByte) = bits(ConstValues.VersionByte) Or _
CByte(GuidVersion.Random << ConstValues.VersionByteShift)
' create the new System.Guid object
guid = New Guid(bits)
Finally
' release the crypto provider handle
If Not hCryptProv.Equals(IntPtr.Zero) Then
WinApi.CryptReleaseContext(hCryptProv, 0)
End If
End Try
Return guid
End Function
End Class
'
' Generate GUIDs on the .NET Compact Framework.
'
Public Class PocketGuidClass PocketGuid
' guid variant types
Private Enum GuidVariantEnum GuidVariant
ReservedNCS = &H0
Standard = &H2
ReservedMicrosoft = &H6
ReservedFuture = &H7
End Enum
' guid version types
Private Enum GuidVersionEnum GuidVersion
TimeBased = &H1
Reserved = &H2
NameBased = &H3
Random = &H4
End Enum
' constants that are used in the class
Private Class ConstValuesClass ConstValues
' number of bytes in guid
Public Const ByteArraySize As Integer = 16
' multiplex variant info
Public Const VariantByte As Integer = 8
Public Const VariantByteMask As Integer = &H3F
Public Const VariantByteShift As Integer = 6
' multiplex version info
Public Const VersionByte As Integer = 7
Public Const VersionByteMask As Integer = &HF
Public Const VersionByteShift As Integer = 4
End Class
' imports for the crypto api functions
Private Class WinApiClass WinApi
Public Const PROV_RSA_FULL As Integer = 1
Public Const CRYPT_VERIFYCONTEXT As Integer = &HF0000000
<DllImport("coredll.dll")> _
Public Shared Function CryptAcquireContext()Function CryptAcquireContext( _
ByRef phProv As IntPtr, ByVal pszContainer As String, _
ByVal pszProvider As String, ByVal dwProvType As Integer, _
ByVal dwFlags As Integer) As Boolean
End Function
<DllImport("coredll.dll")> _
Public Shared Function CryptReleaseContext()Function CryptReleaseContext( _
ByVal hProv As IntPtr, ByVal dwFlags As Integer) As Boolean
End Function
<DllImport("coredll.dll")> _
Public Shared Function CryptGenRandom()Function CryptGenRandom( _
ByVal hProv As IntPtr, ByVal dwLen As Integer, _
ByVal pbBuffer() As Byte) As Boolean
End Function
End Class
' all static methods
Private Sub New()Sub New()
End Sub
' Return a new System.Guid object.
Public Shared Function NewGuid()Function NewGuid() As Guid
Dim hCryptProv As IntPtr = IntPtr.Zero
Dim guid As Guid = guid.Empty
Try
' holds random bits for guid
Dim bits(ConstValues.ByteArraySize - 1) As Byte
' get crypto provider handle
If Not WinApi.CryptAcquireContext(hCryptProv, Nothing, Nothing, _
WinApi.PROV_RSA_FULL, WinApi.CRYPT_VERIFYCONTEXT) Then
Throw New SystemException( _
"Failed to acquire cryptography handle.")
End If
' generate a 128 bit (16 byte) cryptographically random number
If Not WinApi.CryptGenRandom(hCryptProv, bits.Length, bits) Then
Throw New SystemException( _
"Failed to generate cryptography random bytes.")
End If
' set the variant
bits(ConstValues.VariantByte) = bits(ConstValues.VariantByte) And _
CByte(ConstValues.VariantByteMask)
bits(ConstValues.VariantByte) = bits(ConstValues.VariantByte) Or _
CByte(GuidVariant.Standard << ConstValues.VariantByteShift)
' set the version
bits(ConstValues.VersionByte) = bits(ConstValues.VersionByte) And _
CByte(ConstValues.VersionByteMask)
bits(ConstValues.VersionByte) = bits(ConstValues.VersionByte) Or _
CByte(GuidVersion.Random << ConstValues.VersionByteShift)
' create the new System.Guid object
guid = New Guid(bits)
Finally
' release the crypto provider handle
If Not hCryptProv.Equals(IntPtr.Zero) Then
WinApi.CryptReleaseContext(hCryptProv, 0)
End If
End Try
Return guid
End Function
End Class
6、最后一个:想得到,就做得到!
好了,睡觉了~~~很晚了,最后把我翻译的代码贴在这里供大家参考评价,有些c#的我不太明确的都已用#######注释标明。
Imports System.Runtime.InteropServices
Namespace PocketBallonNamespace PocketBallon
Public Class NotificationClass Notification
Private WithEvents msgwnd As NotificationMessageWindow
Private Shared notifications As Hashtable
Private Shared id As Integer
Private Shared clsid As Guid = PocketGuid.NewGuid
Private m_data As SHNOTIFICATIONDATA
Private Structure SHNOTIFICATIONDATAStructure SHNOTIFICATIONDATA
Public cbStruct As Integer
Public dwId As Integer
Public npPriority As SHNP
Public Enum SHNPEnum SHNP As Integer
INFORM = &H1B1
ICONIC = 0
End Enum
Public csDuration As Integer
Public hicon As IntPtr
Public grfFlags As SHNF
Public Enum SHNFEnum SHNF As Integer
STRAIGHTTOTRAY = &H1
CRITICAL = &H2
FORCEMESSAGE = &H8
DISPLAYON = &H10
SILENT = &H20
End Enum
Public clsid As Guid
Public hwndSink As IntPtr
Public pszHTML As IntPtr
Public pszTitle As IntPtr
Public lParam As Integer
End Structure
Private mIcon As IntPtr
Private mDuration As Integer = 10
Private mText As String = String.Empty
Private mCaption As String = String.Empty
Private mCritical As Boolean = False
Private mVisible As Boolean = False
Properties#Region "Properties"
Public Property Caption()Property Caption() As String
Get
Return mCaption
End Get
Set(ByVal value As String)
mCaption = value
End Set
End Property
Public Property Critical()Property Critical() As Boolean
Get
Return mCritical
End Get
Set(ByVal value As Boolean)
mCritical = value
End Set
End Property
Public Property IconHandle()Property IconHandle() As IntPtr
Get
Return mIcon
End Get
Set(ByVal value As IntPtr)
Namespace PocketBallonNamespace PocketBallon
Public Class NotificationClass Notification
Private WithEvents msgwnd As NotificationMessageWindow
Private Shared notifications As Hashtable
Private Shared id As Integer
Private Shared clsid As Guid = PocketGuid.NewGuid
Private m_data As SHNOTIFICATIONDATA
Private Structure SHNOTIFICATIONDATAStructure SHNOTIFICATIONDATA
Public cbStruct As Integer
Public dwId As Integer
Public npPriority As SHNP
Public Enum SHNPEnum SHNP As Integer
INFORM = &H1B1
ICONIC = 0
End Enum
Public csDuration As Integer
Public hicon As IntPtr
Public grfFlags As SHNF
Public Enum SHNFEnum SHNF As Integer
STRAIGHTTOTRAY = &H1
CRITICAL = &H2
FORCEMESSAGE = &H8
DISPLAYON = &H10
SILENT = &H20
End Enum
Public clsid As Guid
Public hwndSink As IntPtr
Public pszHTML As IntPtr
Public pszTitle As IntPtr
Public lParam As Integer
End Structure
Private mIcon As IntPtr
Private mDuration As Integer = 10
Private mText As String = String.Empty
Private mCaption As String = String.Empty
Private mCritical As Boolean = False
Private mVisible As Boolean = False
Properties#Region "Properties"
Public Property Caption()Property Caption() As String
Get
Return mCaption
End Get
Set(ByVal value As String)
mCaption = value
End Set
End Property
Public Property Critical()Property Critical() As Boolean
Get
Return mCritical
End Get
Set(ByVal value As Boolean)
mCritical = value
End Set
End Property
Public Property IconHandle()Property IconHandle() As IntPtr
Get
Return mIcon
End Get
Set(ByVal value As IntPtr)
【上篇】用于主题检测的临时日志(9fcb0391-54d0-4f35-9170-e53887f47814 – 3bfe001a-32de-4114-a6b4-4005b770f6d7)
【下篇】AQA新加入手机自动化测试版块
【下篇】AQA新加入手机自动化测试版块