IP地址输入框.docx
《IP地址输入框.docx》由会员分享,可在线阅读,更多相关《IP地址输入框.docx(6页珍藏版)》请在冰豆网上搜索。
IP地址输入框
IP地址输入框
IP地址输入框
在很多Windows应用程序上我们都会用到类似Windows自带的IP地址输入框,如下图所示:
在.NET中,有很多开发人员的做法是通过用普通的文本框加正则表达式的方式来实现这一功能或者干脆就使用文本框,但是其在方便性和实用性上很难跟MS系统自己的IP地址框相比。
本文章实现的就是一个从Windows中“借”来个一个文本输入框,代码如下:
ImportsSystem.Runtime.InteropServicesNamespaceFormsNamespaceForms
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#EndRegionProtectedOverridesSubOnPaint()SubOnPaint(ByValpeAsSystem.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)‘在此添加自定义绘画代码
EndSubPrivateSubIPTextBox_SizeChanged()SubIPTextBox_SizeChanged(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesMyBase.SizeChanged
IfCtlHwnd.Equals(IntPtr.Zero)=FalseThenUser32.SetWindowPos(CtlHwnd,0,0,0,Me.Width,Me.Height,&H22)
EndSubPrivateCtlHwndAsIntPtrPropertys#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
#EndRegionFunctions#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)
EndFunctionPrivateFunctionFIRST_IPADDRESS()FunctionFIRST_IPADDRESS(ByValxAsInt32)AsByte
FIRST_IPADDRESS=((xAnd&H7F000000)&H1000000)Or(((xAnd&H80000000)<>0)And&H80)
EndFunctionPrivateFunctionSECOND_IPADDRESS()FunctionSECOND_IPADDRESS(ByValxAsInt32)AsByte
SECOND_IPADDRESS=(xAnd&HFF0000)&H10000
EndFunctionPrivateFunctionTHIRD_IPADDRESS()FunctionTHIRD_IPADDRESS(ByValxAsInt32)AsByte
THIRD_IPADDRESS=(xAnd&HFF00&)&H100
EndFunctionPublicFunctionFOURTH_IPADDRESS()FunctionFOURTH_IPADDRESS(ByValxAsInt32)AsByte
FOURTH_IPADDRESS=xAnd&HFF
EndFunctionPrivateFunctionMAKEIPRANGE()FunctionMAKEIPRANGE(ByVallowAsByte,ByValhighAsByte)AsInt32
MAKEIPRANGE=high*&H100&Orlow
EndFunctionPrivateFunctionMakeIPAddess()FunctionMakeIPAddess(ByValb1AsByte,ByValb2AsByte,ByValb3AsByte,ByValb4AsByte)AsInt32
Return((b1And&H7F)*&H1000000Or(b1And&H80)<>0And&H80000000)Or(b2*&H10000)Or(b3*&H100&)Or(b4)
EndFunctionPrivateFunctionMakeIPAddess()FunctionMakeIPAddess(ByValIPAddressAsString)AsInt32
DimipsAsString()=IPAddress.Split(".")
Ifips.Length<>4Thenips=NewString(){"0","0","0","0"}
DimcoutAsInt32=0
ForiAsInteger=0To3
IfNotIsNumeric(ips(i))Then
ThrowNewException("IP地址错误!
")
EndIf
cout=(cout<<8)Or(Val(ips(i))And&HFF)
Next
Returncout
EndFunction#EndRegionEndClassPublicClassUser32ClassUser32<DllImport("user32",EntryPoint:
="CreateWindowExA",CharSet:
=CharSet.Ansi,SetLastError:
=True,ExactSpelling:
=True)>_
PublicSharedFunctionCreateWindowEx()FunctionCreateWindowEx(ByValdwExStyleAsInteger,<MarshalAs(UnmanagedType.VBByRefStr)>ByReflpClassNameAsString,<MarshalAs(UnmanagedType.VBByRefStr)>ByReflpWindowNameAsString,ByValdwStyleAsInteger,ByValxAsInteger,ByValyAsInteger,ByValnWidthAsInteger,ByValnHeightAsInteger,ByValhWndParentAsIntPtr,ByValhMenuAsIntPtr,ByValhInstanceAsIntPtr,ByVallpParamAsIntPtr)AsIntPtr
EndFunction<DllImport("user32",CharSet:
=CharSet.Ansi,SetLastError:
=True,ExactSpelling:
=True)>_
PublicSharedFunctionDestroyWindow()FunctionDestroyWindow(ByValhwndAsIntPtr)AsInteger
EndFunction<DllImport("user32",EntryPoint:
="SendMessageA",CharSet:
=CharSet.Ansi,SetLastError:
=True,ExactSpelling:
=True)>_
PublicSharedFunctionSendMessage()FunctionSendMessage(ByValhwndAsInteger,ByValwMsgAsInteger,ByValwParamAsInteger,ByVallParamAsInteger)AsInteger
EndFunction
<DllImport("user32",EntryPoint:
="SendMessageA",CharSet:
=CharSet.Ansi,SetLastError:
=True,ExactSpelling:
=True)>_
PublicSharedFunctionSendMessage()FunctionSendMessage(ByValhwndAsIntPtr,ByValwMsgAsInteger,ByValwParamAsIntPtr,ByVallParamAsIntPtr)AsIntPtr
EndFunction<DllImport("user32",CharSet:
=CharSet.Ansi,SetLastError:
=True,ExactSpelling:
=True)>_
PublicSharedFunctionSetWindowPos()FunctionSetWindowPos(ByValhwndAsIntPtr,ByValhWndInsertAfterAsInteger,ByValxAsInteger,ByValyAsInteger,ByValcxAsInteger,ByValcyAsInteger,ByValwFlagsAsInteger)AsInteger
EndFunction<DllImport("comctl32.dll",CharSet:
=CharSet.Ansi,SetLastError:
=True,ExactSpelling:
=True)>_
PublicSharedFunctionInitCommonControlsEx()FunctionInitCommonControlsEx(ByRefTLPINITCOMMONCONTROLSEXAsStructures.InitCommonControls)AsInteger
EndFunction‘Fields
PublicConstMENU_CLASSAsString="#32768"‘NestedTypes
PublicEnumConstantsEnumConstants
ICC_INTERNET_CLASSES=2048
IPM_CLEARADDRESS=1124
IPM_GETADDRESS=1126
IPM_SETADDRESS=1125
WS_CHILD=1073741824
WS_TABSTOP=65536
WS_VISIBLE=268435456
EndEnumPublicClassStructuresClassStructures<StructLayout(LayoutKind.Sequential)>_
PublicStructureInitCommonControlsStructureInitCommonControls
‘Fields
PublicdwICCAsInteger
PublicdwSizeAsInteger
EndStructure<StructLayout(LayoutKind.Sequential)>_
PublicStructureWINDOWPOSStructureWINDOWPOS
‘Fields
PubliccxAsInteger
PubliccyAsInteger
PublicflagsAsInteger
PublichWndAsIntPtr
PublichWndInsertAfterAsIntPtr
PublicxAsInteger
PublicyAsInteger
EndStructure
EndClassPublicEnumWindowsMessagesEnumWindowsMessages
WM_SETFONT=48
EndEnumEndClassEndNamespace