本帖最后由 xiaolong263 于 2011-08-13 18:07:16 编辑

解决方案 »

  1.   

    继续上一页:
     public abstract class MouseAction
            {
                public abstract void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form);
            }
            public class MouseSizeRight : MouseAction
            {
                private int lx;
                public MouseSizeRight(int LocationX)
                {
                    lx = LocationX;
                }            public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
                {
                    form.Width = ScreenX - lx;
                    form.Invalidate();
                }
            }
            public class MouseSizeTopLeft : MouseAction
            {
                private int lx, ly;
                private int p;
                private int p_2;
                private int Width;
                private int Height;
                public MouseSizeTopLeft(int LocationX, int LocationY)
                {
                    lx = LocationX;
                    ly = LocationY;
                }            public MouseSizeTopLeft(int p, int p_2, int Width, int Height)
                {
                    // TODO: Complete member initialization
                    this.p = p;
                    this.p_2 = p_2;
                    this.Width = Width;
                    this.Height = Height;
                }
                public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
                {
                    form.Width = lx - ScreenX;
                    form.Width = ScreenY - ly;
                    form.Invalidate();
                }
            }        //MouseSizeLeft:拉伸左边框
            //MouseSizeBottom:拉伸下边框
            //MouseSizeTop:拉伸上边框
            //MouseSizeTopLeft:拉伸左上角
            //MouseSizeTopRight:拉伸右上角
            //MouseSizeBottomLeft:拉伸左下角
            //MouseSizeBottomRight:拉伸右下角
            //MouseDrag:鼠标拖动        public class MouseDrag : MouseAction
            {
                private int x, y;
                public MouseDrag(int hitX, int hitY)
                {
                    x = hitX;
                    y = hitY;
                }            public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
                {
                    form.Location = new Point(ScreenX - x, ScreenY - y);
                }
            }        //定义鼠标动作
            private void Mainfrm_MouseDown(object sender, MouseEventArgs e)
            {
                x = e.X;
                y = e.Y;
                //鼠标点击左上边框
                if ((e.X <= LEFT + 10 && e.Y <= TOP) || (e.Y <= TOP + 10 && e.X <= LEFT))
                {
                    mouse = new MouseSizeTopLeft(Location.X, Location.Y, Width, Height);
                    return;
                }
                //鼠标点击系统关闭按纽
                if (e.X > Width - 20 && e.Y > 6 && e.X < Width - 20 + SysButton_Min.Width && e.Y < 6 + SysButton_Min.Height)
                {
                    Close();
                    return;
                }
            }        private void Mainfrm_MouseMove(object sender, MouseEventArgs e)
            {
                this.Parent.Cursor = CheckCursorType(e.X, e.Y);//改变鼠标的指针形状
                if (mouse != null)
                {
                    mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this);//执行时间响应
                    //注意坐标是Control.MousePosition这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
                }
            }
            private Cursor CheckCursorType(int X, int Y)
            {
                if (((X <= LEFT + 10 && Y <= TOP) || (Y <= TOP + 10 && X <= LEFT))|| ((X >= Width - RIGHT - 10 && Y >= Height - BOTTOM)|| (Y >= Height - BOTTOM - 10 && X >= Width - RIGHT)))
                {
                    return Cursors.SizeNWSE;
                }
                else if (((Y <= TOP + 10 && X >= Width - RIGHT)|| (Y <= TOP && X >= Width - RIGHT - 10))|| ((X <= LEFT && Y >= Height - BOTTOM - 10)|| (Y >= Height - BOTTOM && X <= LEFT + 10)))
                {
                    return Cursors.SizeNESW;
                }
                else if (X >= Width - RIGHT || X <= LEFT)
                {
                    return Cursors.SizeWE;
                }
                else if (Y >= Height - BOTTOM || Y <= TOP)
                {
                    return Cursors.SizeNS;
                }
                else
                {
                    return Cursors.Arrow;
                }
            }        private void Mainfrm_MouseUp(object sender, MouseEventArgs e)
            {
                mouse = null;
            }
            private void Form_DoubleClick(object sender, System.EventArgs e)
            {
                if (y > TOP && y < TITLE_WIDTH)
                {
                    if (WindowState == FormWindowState.Normal)
                    {
                        WindowState = FormWindowState.Maximized;
                        //SysButton = SysButton_Re;
                        Invalidate();
                    }
                    else if (WindowState == FormWindowState.Maximized)
                    {
                        WindowState = FormWindowState.Normal;
                        //SysButton = SysButton_Max;
                        Invalidate();
                    }
                }
            }
        }
    }
    问题为:编译程序没有报错,错误代码为:
      

  2.   

    未处理 System.NullReferenceException
      Message=未将对象引用设置到对象的实例。
      Source=testwinfrm
      StackTrace:
           在 testwinfrm.Mainfrm.Mainfrm_MouseMove(Object sender, MouseEventArgs e) 位置 C:\Documents and Settings\Administrator\桌面\testwinfrm\testwinfrm\Mainfrm.cs:行号 262
           在 System.Windows.Forms.Control.OnMouseMove(MouseEventArgs e)
           在 System.Windows.Forms.Control.WmMouseMove(Message& m)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
           在 System.Windows.Forms.Form.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 testwinfrm.Program.Main() 位置 C:\Documents and Settings\Administrator\桌面\testwinfrm\testwinfrm\Program.cs:行号 18
           在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
      

  3.   

    具体为:这一句:
     private void Mainfrm_MouseMove(object sender, MouseEventArgs e)
            {
                this.Parent.Cursor = CheckCursorType(e.X, e.Y);//改变鼠标的指针形状
                if (mouse != null)
                {
                    mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this);//执行时间响应
                    //注意坐标是Control.MousePosition这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
                }
            }
    颜色所示为未将对象引用到实例。本人现在求助各位,本人将窗体添加Panel并让其在窗体全部填充就不会报错,但是下面的鼠标事件全部无效。请各位帮我看看是怎么回事。窗体为自己个性化的。还有麻烦各位完善下我的代码。下地初次涉足这一块实在不理解。谢谢各位。还有就是介绍几本winfom方面的资料。谢谢大家。小弟真是求助无门了。特请大家不吝赐教,完善下我的代码,可以直接发我邮箱:[email protected]。谢谢大虾。
      

  4.   

    this.Parent是否为空。
    有时间贴这么多代码,不如调试下。
      

  5.   

    this.Parent.Cursor让人费解,如果是主窗体就没有parent,
    为什么不直接用 this.Cursor
      

  6.   

    this.Parent.Cursor让人费解,如果是主窗体就没有parent,
    为什么不直接用 this.Cursor这位大哥提醒的好啊。已经解决,只是船体还无法移动操作,还有窗体边框及其最大化。最小化。画法及其参数。
      

  7.   

    谢谢大家的关注。问题暂时已经解决。谢谢大家。只是感觉没有Windows自带的运行流畅。