一个简单的模拟窗口函数,可是我写的SetWindowPos(CRect icrect)函数不起作用,而且,说我的Scrollbar的ReDraw()有问题,为什么?初学,请高手指教~~
源码在附件里,谢谢!

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.Text; 
    namespace Windows 

       public class CRect 
       { 
          public int left; 
          public int top; 
          public int width; 
          public int height;
          public CRect() 
          { 
             left = 0; 
             top = 0; 
             width = 0; 
             height = 0; 
          } 
          public CRect(int iLeft, int iTop, int iWidth, int iHeight) 
          { 
             left = iLeft;
             top = iTop; 
             width = iWidth; 
             height = iHeight; 
          } 
          public int right 
          { 
            get { return left + width; }
            set { 
                 if (value - left > 0) 
                 { 
                   width = value - left; 
                 } 
                 else { width = 0; } 
                } 
          }// End of property of right       public int bottom 
          { 
             get { return top + height; } 
             set { if (value - top > 0) { height = value - top; } else { height = 0; } } 
          }// End of property of bottom 
          
          public override string ToString() 
          { 
             return "RECT(left, top, width, height, 
                           right, bottom) is \r\n" + "(" + left.ToString() + ", " + top.ToString() + ", " + width.ToString() + ", " + height.ToString() + ", " + right.ToString() + ", " + bottom.ToString() + ")"; 
          } 
          public override bool Equals(object oRect) 
          { 
             if (left == ((CRect)oRect).left && top == ((CRect)oRect).top && width == ((CRect) 
                                oRect).width && height == ((CRect)oRect).height)
             { 
               return true; 
             } 
             else { return false; } 
          }
          public void ReWrite(CRect crect) 
          { 
              this.left = crect.left; 
              this.top = crect.top; 
              this.width = crect.width; 
              this.height = crect.height; 
          }
          public override int GetHashCode() 
          { 
            return base.GetHashCode(); 
          } 
       }
       public abstract class Window 
       {
          protected CRect myCRect; 
          protected String name; 
          public Window() 
          { 
          } 
          public Window(CRect icrect, String strname) 
          { 
              myCRect = icrect; name = strname;
          } 
          public abstract void SetWindowPos(CRect icrect);
          public abstract void InitDialog(); 
          public abstract void Redraw(); 
        } 
        public class Button : Window 
        { 
            public Button() { } 
            public Button(CRect icrect,String strName):base(icrect,strName) 
            {
               myCRect = icrect; name = strName; 
            } 
            public override void InitDialog() { }         public override void Redraw() 
            { 
              Console.WriteLine(name+myCRect.ToString()); 
            } 
            public override void SetWindowPos(CRect icrect) 
            { 
               if (myCRect.Equals(icrect)) ;
               else myCRect.ReWrite(icrect); 
            }
        } 
        public class ScrollBar : Window 
        { 
           protected Button m_PrevButton; 
           protected Button m_ForwButton; 
           private int nWidth;        public int width//20 
           { get { return this.nWidth; } 
             set { if (value != this.nWidth) this.nWidth = value; }
           } 
           public ScrollBar() { } 
           
           public ScrollBar( CRect icrect,String strName):base(icrect,strName) 
           { myCRect = icrect; name = strName; }        public override void InitDialog() 
           { m_PrevButton = new Button(new CRect 
            (myCRect.left,myCRect.top,myCRect.width,this.nWidth),"PrevButton:"); m_ForwButton =  
       new Button(new CRect(myCRect.left,myCRect.top,this.nWidth,myCRect.height),"ForwButton:"); 
           }        public override void Redraw()
           { 
              //base.Redraw(); 
              Console.WriteLine(name + myCRect.ToString());
              m_PrevButton.Redraw(); 
              m_ForwButton.Redraw(); 
           } 
           
           public override void SetWindowPos(CRect icrect) 
           { 
              if (myCRect.Equals(icrect)) ; else myCRect.ReWrite(icrect); 
           } 
        } 
        public class Form : Window 
        { 
           public Form() { } 
           public Form(CRect icrect, String strName):base(icrect,strName) 
           { myCRect = icrect; name = strName; }        public override void InitDialog() { }        public override void Redraw() { }        public override void SetWindowPos(CRect icrect) 
           { 
              if (myCRect.Equals(icrect)) ; 
              else { myCRect.ReWrite(icrect); } 
           }
        } 
        public class MyForm : Form 
        { 
           public ScrollBar myScrollBarRight, myScrollBarBottom;
           public Button myOKButton; 
           public MyForm(CRect icrect, String strName):base(icrect,strName) 
           { 
              myCRect = icrect; name = strName; // = 20; //myScrollBar(); //myOKButton(); 
           }
     
           public override void InitDialog() 
           { 
              base.InitDialog(); 
              myScrollBarBottom = new ScrollBar(new CRect(myCRect.left + 3, myCRect.bottom - 48, myCRect.right - 26, 20), "myScrollBarBottom"); 
              myScrollBarRight = new ScrollBar(new CRect(myCRect.right - 23, myCRect.top + 23, 20, myCRect.bottom - 68), "myScrollBarRight"); 
              myOKButton = new Button(new CRect(myCRect.right-113,myCRect.bottom-38,100,25),"myOKButton"); 
              myScrollBarBottom.width = 20; myScrollBarRight.width = 20;
           } 
           public override void Redraw() 
           { 
              base.Redraw(); 
              myScrollBarRight.Redraw();
              myScrollBarBottom.Redraw(); 
              myOKButton.Redraw(); 
           } 
           public override void SetWindowPos(CRect icrect) 
           { 
              if (myCRect.Equals(icrect)) ; 
              else { myCRect.ReWrite(icrect);
                    myCRect.ToString(); } 
           } 
         } 
         class Tester 
         { 
             static void Main()
             { 
                MyForm form1 = new MyForm(new CRect(0, 0, 0, 0), "Demo of my window foundation class library"); 
                form1.InitDialog(); 
                Console.WriteLine("--------{0}--------", form1); 
                form1.SetWindowPos(new CRect(0, 0, 400, 400)); 
                form1.Redraw();
                Console.WriteLine("--------{0}--------", form1);
                form1.SetWindowPos(new CRect(0, 0, 350, 370)); 
                form1.Redraw();
                Console.ReadLine();
              } 
         } 

      

  2.   

    你的SetWindowPos(CRect icrect)函数下IF语句为空语句,这样有可能出错,不过没见出错!你有的重载为空,导致未将对象引用到设置对象实例的错误。 public override void Redraw()
     {
                //base.Redraw(); 
                Console.WriteLine(name + myCRect.ToString());
               // m_PrevButton.Redraw();           // m_ForwButton.Redraw();    
     }这两个对应的redraw事件重载为空
      

  3.   

    你继承的是window这个类,这个类里面的方法为抽象的,在 public class ScrollBar : Window 内,你把
    m_PrevButton.Redraw(); 
    m_ForwButton.Redraw();  
    写在重载里面。   
    你把这两个注释掉,就能运行。
      

  4.   

    我想要的就是这两个的输出,注释了就没用了。
    我自己想了想,原来是我的逻辑本身有问题,我没有再ScrollBar类里的InitDialog()里对它们进行初始化,呵呵,不过谢谢了~~~