📄 ipaddresscontrol.cs
字号:
if ( index < mod )
{
++offsets[index];
}
}
int x = 0;
int y = 0;
switch ( this.BorderStyle )
{
case BorderStyle.Fixed3D:
x = 3;
y = 3;
break;
case BorderStyle.FixedSingle:
x = 2;
y = 2;
break;
}
int offsetIndex = 0;
x += offsets[offsetIndex++];
for ( int i = 0; i < _fieldControls.Length; ++i )
{
_fieldControls[i].Location = new Point( x, y );
x += _fieldControls[i].Size.Width;
if ( i < _dotControls.Length )
{
x += offsets[offsetIndex++];
_dotControls[i].Location = new Point( x, y );
x += _dotControls[i].Size.Width;
x += offsets[offsetIndex++];
}
}
ResumeLayout( false );
}
private bool OnCedeFocus( int fieldId, Direction direction, Selection selection )
{
if ( ( direction == Direction.Backward && fieldId == 0 ) ||
( direction == Direction.Forward && fieldId == ( NumberOfFields - 1 ) ) )
{
return false;
}
if ( direction == Direction.Forward )
{
++fieldId;
}
else
{
--fieldId;
}
_fieldControls[fieldId].TakeFocus( direction, selection );
return true;
}
private void OnFieldTextChanged( int fieldId, string text )
{
if ( FieldChangedEvent != null )
{
FieldChangedEventArgs args = new FieldChangedEventArgs();
args.FieldId = fieldId;
args.Text = text;
FieldChangedEvent( this, args );
}
OnTextChanged( EventArgs.Empty );
}
private void Parse( string text )
{
Clear();
if ( null == text )
{
return;
}
int textIndex = 0;
int index = 0;
for ( index = 0; index < _dotControls.Length; ++index )
{
int findIndex = text.IndexOf( _dotControls[index].Text, textIndex );
if ( findIndex >= 0 )
{
_fieldControls[index].Text = text.Substring( textIndex, findIndex - textIndex );
textIndex = findIndex + _dotControls[index].Text.Length;
}
else
{
break;
}
}
_fieldControls[index].Text = text.Substring( textIndex );
}
private void OnSpecialKey( int fieldId, Keys keyCode )
{
switch ( keyCode )
{
case Keys.Back:
if ( fieldId > 0 )
{
_fieldControls[fieldId-1].HandleSpecialKey( Keys.Back );
}
break;
case Keys.Home:
_fieldControls[0].TakeFocus( Direction.Forward, Selection.None );
break;
case Keys.End:
_fieldControls[NumberOfFields - 1].TakeFocus( Direction.Backward, Selection.None );
break;
}
}
#endregion
#region Protected Overrides
protected override void OnBackColorChanged( EventArgs e )
{
foreach ( FieldControl fc in _fieldControls )
{
if ( fc != null )
{
fc.BackColor = this.BackColor;
}
}
foreach ( DotControl dc in _dotControls )
{
if ( dc != null )
{
dc.BackColor = this.BackColor;
dc.Invalidate();
}
}
base.OnBackColorChanged( e );
Invalidate();
}
protected override void OnFontChanged(EventArgs e)
{
foreach ( FieldControl fc in _fieldControls )
{
fc.SetFont( this.Font );
}
foreach ( DotControl dc in _dotControls )
{
dc.SetFont( this.Font );
}
AdjustSize();
LayoutControls();
base.OnFontChanged( e );
Invalidate();
}
protected override void OnForeColorChanged( EventArgs e )
{
foreach ( FieldControl fc in _fieldControls )
{
fc.ForeColor = this.ForeColor;
}
foreach ( DotControl dc in _dotControls )
{
dc.ForeColor = this.ForeColor;
}
base.OnForeColorChanged( e );
Invalidate( true );
}
protected override void OnGotFocus( EventArgs e )
{
_fieldControls[0].TakeFocus( Direction.Forward, Selection.All );
base.OnGotFocus( e );
}
private void OnPaintStandard( PaintEventArgs e )
{
SolidBrush ctrlBrush = null;
if ( Enabled )
{
if ( this.ReadOnly )
{
if ( this.BackColor.ToKnownColor() == KnownColor.Window )
{
ctrlBrush = new SolidBrush( Color.FromKnownColor( KnownColor.Control ) );
}
else
{
ctrlBrush = new SolidBrush( this.BackColor );
}
}
else
{
ctrlBrush = new SolidBrush( this.BackColor );
}
}
else
{
if ( this.BackColor.ToKnownColor() == KnownColor.Window )
{
ctrlBrush = new SolidBrush( Color.FromKnownColor( KnownColor.Control ) );
}
else
{
ctrlBrush = new SolidBrush( BackColor );
}
}
e.Graphics.FillRectangle( ctrlBrush, ClientRectangle );
switch ( BorderStyle )
{
case BorderStyle.Fixed3D:
ControlPaint.DrawBorder3D( e.Graphics, ClientRectangle, Border3DStyle.Sunken );
break;
case BorderStyle.FixedSingle:
ControlPaint.DrawBorder( e.Graphics, ClientRectangle,
Color.FromKnownColor( KnownColor.WindowFrame ), ButtonBorderStyle.Solid );
break;
}
}
private void OnPaintThemed( PaintEventArgs e )
{
NativeMethods.RECT rect = new NativeMethods.RECT();
rect.left = ClientRectangle.Left;
rect.top = ClientRectangle.Top;
rect.right = ClientRectangle.Right;
rect.bottom = ClientRectangle.Bottom;
IntPtr hdc = new IntPtr();
hdc = e.Graphics.GetHdc();
if ( this.BackColor.ToKnownColor() != KnownColor.Window )
{
e.Graphics.ReleaseHdc( hdc );
e.Graphics.FillRectangle( new SolidBrush( this.BackColor ), this.ClientRectangle );
hdc = e.Graphics.GetHdc();
IntPtr hTheme = NativeMethods.OpenThemeData( this.Handle, "Edit" );
NativeMethods.DTBGOPTS options = new NativeMethods.DTBGOPTS();
options.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(options);
options.dwFlags = NativeMethods.DTBG_OMITCONTENT;
int state = NativeMethods.ETS_NORMAL;
if ( !Enabled )
{
state = NativeMethods.ETS_DISABLED;
}
else
if ( ReadOnly )
{
state = NativeMethods.ETS_READONLY;
}
NativeMethods.DrawThemeBackgroundEx( hTheme, hdc,
NativeMethods.EP_EDITTEXT, state, ref rect, ref options );
if ( IntPtr.Zero != hTheme )
{
NativeMethods.CloseThemeData( hTheme );
}
}
else
if ( Enabled & !ReadOnly )
{
IntPtr hTheme = NativeMethods.OpenThemeData( this.Handle, "Edit" );
NativeMethods.DrawThemeBackground( hTheme, hdc, NativeMethods.EP_EDITTEXT,
NativeMethods.ETS_NORMAL, ref rect, IntPtr.Zero );
if ( IntPtr.Zero != hTheme )
{
NativeMethods.CloseThemeData( hTheme );
}
}
else
{
IntPtr hTheme = NativeMethods.OpenThemeData( this.Handle, "Globals" );
IntPtr hBrush = NativeMethods.GetThemeSysColorBrush( hTheme, 15 );
NativeMethods.FillRect( hdc, ref rect, hBrush );
if ( IntPtr.Zero != hBrush )
{
NativeMethods.DeleteObject( hBrush );
hBrush = IntPtr.Zero;
}
if ( IntPtr.Zero != hTheme )
{
NativeMethods.CloseThemeData( hTheme );
hTheme = IntPtr.Zero;
}
hTheme = NativeMethods.OpenThemeData( this.Handle, "Edit" );
NativeMethods.DTBGOPTS options = new NativeMethods.DTBGOPTS();
options.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(options);
options.dwFlags = NativeMethods.DTBG_OMITCONTENT;
NativeMethods.DrawThemeBackgroundEx( hTheme, hdc,
NativeMethods.EP_EDITTEXT, NativeMethods.ETS_DISABLED, ref rect, ref options );
if ( IntPtr.Zero != hTheme )
{
NativeMethods.CloseThemeData( hTheme );
}
}
e.Graphics.ReleaseHdc( hdc );
}
protected override void OnPaint( PaintEventArgs e )
{
bool themed = NativeMethods.IsThemed();
if ( DesignMode || !themed || ( themed && BorderStyle != BorderStyle.Fixed3D ) )
{
OnPaintStandard( e );
}
else
{
OnPaintThemed( e );
}
base.OnPaint( e );
}
protected override void OnSizeChanged( EventArgs e )
{
LayoutControls();
base.OnSizeChanged( e );
}
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc( ref Message m )
{
switch ( m.Msg )
{
case NativeMethods.WM_WINDOWPOSCHANGING:
NativeMethods.WINDOWPOS lParam = (NativeMethods.WINDOWPOS)m.GetLParam( typeof( NativeMethods.WINDOWPOS ) );
if ( lParam.cx < MinimumSize.Width )
{
lParam.flags |= NativeMethods.SWP_NOMOVE;
lParam.cx = MinimumSize.Width;
}
if ( lParam.cy < MinimumSize.Height )
{
lParam.flags |= NativeMethods.SWP_NOMOVE;
lParam.cy = MinimumSize.Height;
}
if ( _autoSize && lParam.cy != MinimumSize.Height )
{
lParam.flags |= NativeMethods.SWP_NOMOVE;
lParam.cy = MinimumSize.Height;
}
Marshal.StructureToPtr( lParam, m.LParam, true );
break;
}
base.WndProc( ref m );
}
#endregion
#region Generated Code
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// IPAddressControl
//
this.BackColor = System.Drawing.SystemColors.Window;
this.Name = "IPAddressControl";
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.IPAddressControl_DragEnter);
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.IPAddressControl_DragDrop);
}
#endregion
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -