我的C#窗体

来源:互联网 发布:法兰西战役知乎 编辑:程序博客网 时间:2024/06/05 07:47

我的C#里的Windows窗体,希望朋友们多多指点:

 

    public partial class NoBorder : Form
    
{
        
/// <summary>
        
/// 表示鼠标按下并调整大小位置时的数据状态
        
/// </summary>

        internal static class MState
        
{
            
private static int m_Left;
            
private static int m_Top;
            
private static int m_Right;
            
private static int m_Bottom;
            
private static int m_LastX;
            
private static int m_LastY;
            
private static MouseAction m_State;
            
private static MouseAction m_LastState;
            
/// <summary>
            
/// 窗体的左坐标
            
/// </summary>

            public static int Left
            
{
                
get return MState.m_Left; }
            }

            
/// <summary>
            
/// 窗体的上坐标
            
/// </summary>

            public static int Top
            
{
                
get return MState.m_Top; }
            }

            
/// <summary>
            
/// 窗体的右坐标
            
/// </summary>

            public static int Right
            
{
                
get return MState.m_Right; }
            }

            
/// <summary>
            
/// 窗体的下坐标
            
/// </summary>

            public static int Bottom
            
{
                
get return MState.m_Bottom; }
            }

            
/// <summary>
            
/// 当前的鼠标状态
            
/// </summary>

            public static MouseAction State
            
{
                
get return MState.m_State; }
            }

            
/// <summary>
            
/// 鼠标按下时的Y坐标
            
/// </summary>

            public static int LastX
            
{
                
get return MState.m_LastX; }
            }

            
/// <summary>
            
/// 鼠标按下时的X坐标
            
/// </summary>

            public static int LastY
            
{
                
get return MState.m_LastY; }
            }

            
/// <summary>
            
/// 鼠标按下时的状态
            
/// </summary>

            public static MouseAction LastState
            
{
                
get return MState.m_LastState; }
            }

            
/// <summary>
            
/// 设置数据值
            
/// </summary>
            
/// <param name="bounds">窗体的大小位置</param>
            
/// <param name="point">鼠标的位置</param>

            public static void SetData(Rectangle bounds, Point point)
            
{
                m_Left 
= bounds.Left;
                m_Top 
= bounds.Top;
                m_Right 
= bounds.Right;
                m_Bottom 
= bounds.Bottom;
                m_LastX 
= point.X;
                m_LastY 
= point.Y;
                m_LastState 
= m_State;
            }

            
/// <summary>
            
/// 设置状态
            
/// </summary>
            
/// <param name="state">要设置的状态值</param>

            public static void SetState(MouseAction state)
            
{
                m_State 
= state;
            }

            
/// <summary>
            
/// 重置状态
            
/// </summary>

            public static void ResetState()
            
{
                m_State 
= MouseAction.None;
                m_LastState 
= MouseAction.None;
            }

        }

        
internal enum MouseAction
        
{
            None,
            Left,
            LeftTop,
            Top,
            RightTop,
            Right,
            RightBottom,
            Bottom,
            LeftBottom
        }


        [DllImport(
"user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
        
public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
        [DllImport(
"user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        
public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

        
internal static int WM_NCHITTEST = 0x84//移动鼠标,按住或释放鼠标时发生的系统消息
        internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
        internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
        internal static IntPtr HTCAPTION = (IntPtr)0x2//标题栏

        
private int m_BorderWidth = 4;
        
private int m_CaptionHeight = 22;
        
public NoBorder()
        
{
            
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            InitializeComponent();
        }


        
protected override void CreateHandle()
        
{
            
base.CreateHandle();
            
int WS_SYSMENU = 0x00080000;

            
this.FormBorderStyle = FormBorderStyle.None;
            SetWindowLong(
new HandleRef(thisthis.Handle), -16, GetWindowLong(new HandleRef(thisthis.Handle), -16| WS_SYSMENU);
            
this.Padding = new Padding(this.m_BorderWidth, this.m_BorderWidth + this.m_CaptionHeight, this.m_BorderWidth, this.m_BorderWidth);            
        }

        
/// <summary>
        
/// 重写以处理模拟鼠标系统处理
        
/// </summary>
        
/// <param name="m"></param>

        protected override void WndProc(ref Message m)
        
{
            
if (m.Msg == WM_NCHITTEST)
            
{
                
base.WndProc(ref m);
                
if (DesignMode)
                
{
                    
return;
                }

                
if (m.Result == HTCLIENT)
                
{
                    m.HWnd 
= this.Handle;

                    Rectangle rect 
= this.Bounds;
                    Point cp 
= Cursor.Position;
                    
if (cp.Y > rect.Top + m_BorderWidth && cp.Y <= rect.Top + m_CaptionHeight + m_BorderWidth && cp.X>rect.Left+m_BorderWidth && cp.X<rect.Right-m_BorderWidth)
                    
{
                        
if ((cp.X <= rect.Left + m_BorderWidth + m_CaptionHeight))
                        
{
                            m.Result 
= HTSYSMENU;//模拟系统菜单,双击可以关闭窗体
                        }

                        
else
                        
{
                            m.Result 
= HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
                        }

                    }

                }

                
return;
            }

            
base.WndProc(ref m);
        }

        
/// <summary>
        
/// 绘制窗体
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnPaint(PaintEventArgs e)
        
{
            
base.OnPaint(e);
            Rectangle captionRect 
= Rectangle.FromLTRB(this.m_BorderWidth, this.m_BorderWidth, this.ClientRectangle.Right-this.m_BorderWidth, this.m_CaptionHeight);
            Rectangle clientRect 
= Rectangle.FromLTRB(this.m_BorderWidth, this.m_BorderWidth + this.m_CaptionHeight, this.ClientRectangle.Right - this.m_BorderWidth, this.ClientRectangle.Bottom-this.m_BorderWidth);
            
//绘制整个窗体
            e.Graphics.FillRectangle(SystemBrushes.InactiveBorder, this.ClientRectangle);
            
//绘制标题栏
            e.Graphics.FillRectangle(SystemBrushes.ActiveCaption, captionRect);
            
//绘制窗体的图标
            if (this.Icon != null)
            
{
                e.Graphics.DrawIcon(
this.Icon, Rectangle.FromLTRB(this.m_BorderWidth, this.m_BorderWidth, this.m_CaptionHeight, this.m_CaptionHeight));
            }

            
//绘制窗体按钮
            ControlPaint.DrawCaptionButton(e.Graphics, Rectangle.FromLTRB(this.ClientRectangle.Right-this.m_CaptionHeight-this.m_BorderWidth, this.m_BorderWidth, this.ClientRectangle.Right - this.m_BorderWidth, this.m_CaptionHeight), CaptionButton.Close, ButtonState.All);
            
//绘制窗体客户区
            e.Graphics.FillRectangle(SystemBrushes.Window, clientRect);
            
string strHello = "欢迎访问我的CSDN博客: http://blog.csdn.net/hbxtlhx";
            TextRenderer.DrawText(e.Graphics, strHello, 
this.Font, clientRect, Color.Red, Color.White, TextFormatFlags.VerticalCenter);
        }

        
/// <summary>
        
/// 重写以处理鼠标按钮准备调整窗体大小
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnMouseDown(MouseEventArgs e)
        
{
            
base.OnMouseDown(e);
            MState.SetData(
this.Bounds, Cursor.Position);
        }

        
/// <summary>
        
/// 重写以处理鼠标抬起事件重置状态
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnMouseUp(MouseEventArgs e)
        
{
            
base.OnMouseUp(e);
            MState.ResetState();
        }

        
/// <summary>
        
/// 重写以处理鼠标移动调整窗体大小位置
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnMouseMove(MouseEventArgs e)
        
{
            
base.OnMouseMove(e);
            
if (!this.Capture)
            
{
                MState.SetState(
this.setMouse(e.Location));
                
return;
            }


            
int dx = 0;
            
int dy = 0;
            
int dr = 0;
            
int db = 0;
            Point cp 
= Cursor.Position;
            
if (MState.LastState == MouseAction.Left)//
            {
                dx 
= cp.X - MState.LastX;
            }

            
else if (MState.LastState == MouseAction.Right)//
            {
                dr 
= cp.X - MState.LastX;
            }

            
else if (MState.LastState == MouseAction.Top)//
            {
                dy 
= cp.Y - MState.LastY;
            }

            
else if (MState.LastState == MouseAction.Bottom)//
            {
                db 
= cp.Y - MState.LastY;
            }

            
else if (MState.LastState == MouseAction.LeftTop)
            
{
                dx 
= cp.X - MState.LastX;
                dy 
= cp.Y - MState.LastY;
            }

            
else if (MState.LastState == MouseAction.RightTop)
            
{
                dr 
= cp.X - MState.LastX;
                dy 
= cp.Y - MState.LastY;
            }

            
else if (MState.LastState == MouseAction.LeftBottom)
            
{
                dx 
= cp.X - MState.LastX;
                db 
= cp.Y - MState.LastY;
            }

            
else if (MState.LastState == MouseAction.RightBottom)
            
{
                dr 
= cp.X - MState.LastX;
                db 
= cp.Y - MState.LastY;
            }

            
this.Bounds = Rectangle.FromLTRB(MState.Left + dx, MState.Top + dy, MState.Right + dr, MState.Bottom + db);
            
this.Invalidate();
        }

        
/// <summary>
        
/// 重写以处理重置鼠标光标信息
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnMouseLeave(EventArgs e)
        
{
            
base.OnMouseLeave(e);
            
if (this.Bounds.Contains(Cursor.Position))
            
{
                
this.Cursor = Cursors.Default;
            }

        }

        
/// <summary>
        
/// 由指定点设置鼠标动作
        
/// </summary>
        
/// <param name="point"></param>
        
/// <returns></returns>

        private MouseAction setMouse(Point point)
        
{
            Rectangle rect 
= this.ClientRectangle;

            
if ((point.X <= rect.Left + m_BorderWidth) && (point.Y <= rect.Top + m_BorderWidth))
            
{
                
//左上
                this.Cursor = Cursors.SizeNWSE;
                
return MouseAction.LeftTop;
            }

            
else if ((point.X >= rect.Left + rect.Width - m_BorderWidth) && (point.Y <= rect.Top + m_BorderWidth))
            
{
                
//右上
                this.Cursor = Cursors.SizeNESW;
                
return MouseAction.RightTop;
            }

            
else if ((point.X <= rect.Left + m_BorderWidth) && (point.Y >= rect.Top + rect.Height - m_BorderWidth))
            
{
                
//左下
                this.Cursor = Cursors.SizeNESW;
                
return MouseAction.LeftBottom;
            }

            
else if ((point.X >= rect.Left + rect.Width - m_BorderWidth) && (point.Y >= rect.Top + rect.Height - m_BorderWidth))
            
{
                
//右下
                this.Cursor = Cursors.SizeNWSE;
                
return MouseAction.RightBottom;
            }

            
else if ((point.X <= rect.Left + m_BorderWidth - 1))
            
{
                
//
                this.Cursor = Cursors.SizeWE;
                
return MouseAction.Left;
            }

            
else if ((point.X >= rect.Left + rect.Width - m_BorderWidth))
            
{
                
//
                this.Cursor = Cursors.SizeWE;
                
return MouseAction.Right;
            }

            
else if ((point.Y <= rect.Top + m_BorderWidth - 1))
            
{
                
//
                this.Cursor = Cursors.SizeNS;
                
return MouseAction.Top;
            }

            
else if ((point.Y >= rect.Top + rect.Height - m_BorderWidth))
            
{
                
//
                this.Cursor = Cursors.SizeNS;
                
return MouseAction.Bottom;
            }

            
this.Cursor = Cursors.Default;
            
return MouseAction.None;
        }

        
/// <summary>
        
/// 关闭窗体
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)
        
{
            
this.Close();
        }

    }