IP地址输入框

在很多Windows应用程序上我们都会用到类似Windows自带的IP地址输入框,如下图所示:

在.NET中,有很多开发人员的做法是通过用普通的文本框加正则表达式的方式来实现这一功能或者干脆就使用文本框,但是其在方便性和实用性上很难跟MS系统自己的IP地址框相比。本文章实现的就是一个从Windows中“借”来个一个文本输入框,代码如下:

 

Imports  System.Runtime.InteropServices

Namespace Forms
    
Public Class IPTextBox
        
Inherits System.Windows.Forms.Control

组件设计器生成的代码

        
Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
            
MyBase.OnPaint(pe)

            
'在此添加自定义绘画代码
        End Sub


        
Private Sub IPTextBox_SizeChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.SizeChanged
            
If CtlHwnd.Equals(IntPtr.Zero) = False Then User32.SetWindowPos(CtlHwnd, 000Me.Width, Me.Height, &H22)
        
End Sub


        
Private CtlHwnd As IntPtr

Propertys

Functions

    
End Class


    
Public Class User32

        
<DllImport("user32", EntryPoint:="CreateWindowExA", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
        
Public Shared Function CreateWindowEx(ByVal dwExStyle As Integer<MarshalAs(UnmanagedType.VBByRefStr)> ByRef lpClassName As String<MarshalAs(UnmanagedType.VBByRefStr)> ByRef lpWindowName As StringByVal dwStyle As IntegerByVal x As IntegerByVal y As IntegerByVal nWidth As IntegerByVal nHeight As IntegerByVal hWndParent As IntPtr, ByVal hMenu As IntPtr, ByVal hInstance As IntPtr, ByVal lpParam As IntPtr) As IntPtr
        
End Function


        
<DllImport("user32", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
        
Public Shared Function DestroyWindow(ByVal hwnd As IntPtr) As Integer
        
End Function


        
<DllImport("user32", EntryPoint:="SendMessageA", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
        
Public Shared Function SendMessage(ByVal hwnd As IntegerByVal wMsg As IntegerByVal wParam As IntegerByVal lParam As IntegerAs Integer
        
End Function

        
<DllImport("user32", EntryPoint:="SendMessageA", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
        
Public Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As IntegerByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        
End Function


        
<DllImport("user32", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
        
Public Shared Function SetWindowPos(ByVal hwnd As IntPtr, ByVal hWndInsertAfter As IntegerByVal x As IntegerByVal y As IntegerByVal cx As IntegerByVal cy As IntegerByVal wFlags As IntegerAs Integer
        
End Function


        
<DllImport("comctl32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
        
Public Shared Function InitCommonControlsEx(ByRef TLPINITCOMMONCONTROLSEX As Structures.InitCommonControls) As Integer
        
End Function


        
' Fields
        Public Const MENU_CLASS As String = "#32768"

        
' Nested Types
        Public Enum Constants
            ICC_INTERNET_CLASSES 
= 2048
            IPM_CLEARADDRESS 
= 1124
            IPM_GETADDRESS 
= 1126
            IPM_SETADDRESS 
= 1125
            WS_CHILD 
= 1073741824
            WS_TABSTOP 
= 65536
            WS_VISIBLE 
= 268435456
        
End Enum


        
Public Class Structures

            
<StructLayout(LayoutKind.Sequential)> _
            
Public Structure InitCommonControls
                
' Fields
                Public dwICC As Integer
                
Public dwSize As Integer
            
End Structure


            
<StructLayout(LayoutKind.Sequential)> _
            
Public Structure WINDOWPOS
                
' Fields
                Public cx As Integer
                
Public cy As Integer
                
Public flags As Integer
                
Public hWnd As IntPtr
                
Public hWndInsertAfter As IntPtr
                
Public x As Integer
                
Public y As Integer
            
End Structure

        
End Class


        
Public Enum WindowsMessages
            WM_SETFONT 
= 48
        
End Enum


    
End Class


End Namespace


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值