IP地址输入框介绍
在很多Windows应用程序上我们都会用到类似Windows自带的IP地址输入框,如下图所示:
在.NET中,有很多开发人员的做法是通过用普通的文本框加正则表达式的方式来实现这一功能或者干脆就使用文本框,但是其在方便性和实用性上很难跟MS系统自己的IP地址框相比。本文章实现的就是一个从Windows中“借”来个一个文本输入框,代码如下:
ImportsSystem.Runtime.InteropServices
NamespaceFormsNamespaceForms
PublicClassIPTextBoxClassIPTextBox
InheritsSystem.Windows.Forms.Control
组件设计器生成的代码#Region"组件设计器生成的代码"
PublicSubNew()SubNew()
MyBase.New()
'该调用是组件设计器所必需的。
InitializeComponent()
'在InitializeComponent()调用之后添加任何初始化
DimCommCtrlAsUser32.Structures.InitCommonControls
CommCtrl.dwSize=8
CommCtrl.dwICC=User32.Constants.ICC_INTERNET_CLASSES
IfUser32.InitCommonControlsEx(CommCtrl)Then
CtlHwnd=User32.CreateWindowEx(0,"SysIPAddress32","",_
User32.Constants.WS_CHILDOrUser32.Constants.WS_TABSTOPOrUser32.Constants.WS_VISIBLE,0,0,132,21,_
Me.Handle,IntPtr.Zero,GetInstance,IntPtr.Zero)
IfCtlHwnd.Equals(IntPtr.Zero)=FalseThen
'将IP控件的字体设置的根窗体一样用宋体
DimhFontAsIntPtr=Me.Font.ToHfont()
User32.SendMessage(CtlHwnd,User32.WindowsMessages.WM_SETFONT,hFont,IntPtr.Zero)
EndIf
Else
EndIf
EndSub
'Control重写dispose以清理组件列表。
ProtectedOverloadsOverridesSubDispose()SubDispose(ByValdisposingAsBoolean)
IfCtlHwnd.Equals(IntPtr.Zero)=FalseThenUser32.DestroyWindow(CtlHwnd)
IfdisposingThen
IfNot(componentsIsNothing)Then
components.Dispose()
EndIf
EndIf
MyBase.Dispose(disposing)
EndSub
'控件设计器所必需的
PrivatecomponentsAsSystem.ComponentModel.IContainer
'注意:以下过程是组件设计器所必需的
'可以使用组件设计器修改此过程。不要使用
'代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent()SubInitializeComponent()
components=NewSystem.ComponentModel.Container
EndSub
#EndRegion
ProtectedOverridesSubOnPaint()SubOnPaint(ByValpeAsSystem.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)
'在此添加自定义绘画代码
EndSub
PrivateSubIPTextBox_SizeChanged()SubIPTextBox_SizeChanged(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesMyBase.SizeChanged
IfCtlHwnd.Equals(IntPtr.Zero)=FalseThenUser32.SetWindowPos(CtlHwnd,0,0,0,Me.Width,Me.Height,&H22)
EndSub
PrivateCtlHwndAsIntPtr
Propertys#Region"Propertys"
PublicOverridesPropertyText()PropertyText()AsString
Get
DimTempLngAsInteger=0
DimtmpLngAsIntPtr=System.Runtime.InteropServices.Marshal.AllocHGlobal(4)
Try
User32.SendMessage(CtlHwnd,User32.Constants.IPM_GETADDRESS,IntPtr.Zero,tmpLng)
TempLng=System.Runtime.InteropServices.Marshal.ReadInt32(tmpLng)
CatchexAsException
MsgBox(ex.Message)
EndTry
System.Runtime.InteropServices.Marshal.FreeHGlobal(tmpLng)
ReturnFIRST_IPADDRESS(TempLng)&"."&SECOND_IPADDRESS(TempLng)&"."&THIRD_IPADDRESS(TempLng)&"."&FOURTH_IPADDRESS(TempLng)
EndGet
Set(ByValValueAsString)
IfValue=String.EmptyThenValue=""
IfValue.Split(".").Length<>4ThenValue=""
IfValue<>""ThenUser32.SendMessage(CtlHwnd,User32.Constants.IPM_SETADDRESS,IntPtr.Zero,NewIntPtr(MakeIPAddess(Value)))
EndSet
EndProperty
#EndRegion
Functions#Region"Functions"
'''<summary>
'''获取应用程序的进程句柄
'''</summary>
'''<returns></returns>
'''<remarks></remarks>
PrivateFunctionGetInstance()FunctionGetInstance()AsIntPtr
DimtmpTypeAsType=Me.GetType
DimtmpModuleAsSystem.Reflection.Module=tmpType.Module
ReturnSystem.Runtime.InteropServices.Marshal.GetHINSTANCE(tmpModule)
EndFunction
PrivateFunctionFIRST_IPADDRESS()FunctionFIRST_IPADDRESS(ByValxAsInt32)AsByte
FIRST_IPADDRESS=((xAnd&H7F000000)&H1000000)Or(((xAnd&H80000000)<>0)And&H80)
EndFunction
PrivateFunctionSECOND_IPADDRESS()FunctionSECOND_IPADDRESS(ByValxAsInt32)AsByte
SECOND_IPADDRESS=(xAnd&HFF0000)&H10000
EndFunction
PrivateFunctionTHIRD_IPADDRESS()FunctionTHIRD_IPADDRESS(ByValxAsInt32)AsByte
THIRD_IPADDRESS=(xAnd&HFF00&)&H100
EndFunction
PublicFunctionFOURTH_IPADDRESS()FunctionFOURTH_IPADDRESS(ByValxAsInt32)AsByte
FOURTH_IPADDRESS=xAnd&HFF
EndFunction
PrivateFunctionMAKEIPRANGE()FunctionMAKEIPRANGE(ByVallowAsByte,ByValhighAsByte)AsInt32
MAKEIPRANGE=high*&H100&Orlow
EndFunction
PrivateFunctionMakeIPAddess()FunctionMakeIPAddess(ByValb1AsByte,ByValb2AsByte,ByValb3AsByte,ByValb4AsByte)AsInt32
Return((b1And&H7F)*&H1000000Or(b1And&H80)<>0And&H80000000)Or(b2*&H10000)Or(b3*&H100&)Or(b4)
EndFunction
PrivateFunctionMakeIPAddess()FunctionMakeIPAddess(ByValIPAddressAsString)AsInt32
DimipsAsString()=IPAddress.Split(".")
Ifips.Length<>4Thenips=NewString(){"0","0","0","0"}
DimcoutAsInt32=
本文地址:
http://www.45fan.com/dnjc/69945.html