kooyee ‘s blog

          開源軟件, 眾人努力的結晶, 全人類的共同財富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          [SWT] Canvas scroll 滾動條

          Posted on 2007-07-25 19:47 kooyee 閱讀(3485) 評論(0)  編輯  收藏 所屬分類: GUI骨衣
          scroll(int destX, int destY, int x, int y, int width, int height, boolean all) 

                    
          //Scrolls a rectangular area of the receiver by first copying the source area to the destination and then causing the area of the source which is not covered by the destination to be repainted. 

          以origin(0,0)作為原點滾動,否則在滾動中會變形

          public static void main (String [] args) {
          >     Display display = new Display ();
          >     Shell shell = new Shell (display);
          >     shell.setLayout(new FillLayout());
          >     final Point origin = new Point (00);
          >     final Canvas canvas = new Canvas (shell,SWT.V_SCROLL | SWT.H_SCROLL);
          >     final int width = 500;
          >     final ScrollBar hBar = canvas.getHorizontalBar ();
          >     final Color red = new Color(null,255,0,0);
          >     hBar.setMaximum(width);
          >     hBar.addListener (SWT.Selection, new Listener () {
          >
          >         public void handleEvent (Event e) {
          >         System.out.println("HBar listener");
          >             int hSelection = hBar.getSelection ();
          >             int destX = -hSelection - origin.x;
          >             canvas.scroll (destX, 000, width, 100true);
          >             origin.x = -hSelection;
          >             System.out.println("HBar listener exit");
          >         }

          >     }
          );
          >     final ScrollBar vBar = canvas.getVerticalBar ();
          >     vBar.setMaximum(100);
          >     vBar.addListener (SWT.Selection, new Listener () {
          >         public void handleEvent (Event e) {
          >             int vSelection = vBar.getSelection ();
          >             int destY = -vSelection - origin.y;
          >             canvas.scroll (0, destY, 00, width, 100true);
          >             origin.y = -vSelection;
          >         }

          >     }
          );
          >     canvas.addListener (SWT.Paint, new Listener () {
          >
          >     boolean init = true;
          >     public void handleEvent (Event e) {
          >     System.out.println("Paint listener");
          >
          >             GC gc = e.gc;
          >             gc.setBackground(red);
          >                 gc.fillRectangle(origin.x,origin.y,width,100);
          >                 gc.drawText ("test", origin.x, origin.y);
          >
          >
          >         }

          >     }
          );
          >     shell.setSize (500150);
          >     shell.open ();
          >     while (!shell.isDisposed ()) {
          >         if (!display.readAndDispatch ()) display.sleep ();
          >     }

          >     display.dispose ();
          > }


          要是用GC畫圖的話,一定要加上origin.x和origin.y. 這樣才會在新的redraw()的圖形中把矩形顯示在相應位置
          //x,y 矩形的起點坐標
          gc.fillRectangle(origin.x+x,origin.y+y,595,842);

          例如 設矩形起點(50,50)和origin(0,0), 滾動后origin點的坐標變為(0,-10)。這樣origin.x+x,origin.y+y 后矩形的新的起點為(50,40),然后GC在新的起點(50,40)畫出圖形, 這樣顯示出來的矩形就向上移動了10個像素。(如果對起點的x,y的值進行運算,沒有必要改動origin.x和origin.y,分開考慮。 )
          比如畫3個上下間隔為100的矩形
          gc.fillRectangle(origin.x+x,origin.y+y,595,842);
             y += 100;
          gc.fillRectangle(origin.x
          +x,origin.y+y,595,842);
             y += 100;
          gc.fillRectangle(origin.x
          +x,origin.y+y,595,842);

          主站蜘蛛池模板: 红河县| 伊通| 余江县| 乌兰县| 兴海县| 海口市| 栾城县| 红安县| 额尔古纳市| 阳泉市| 吉安县| 宁远县| 会宁县| 华宁县| 富宁县| 清新县| 武定县| 杂多县| 三亚市| 永春县| 西青区| 剑阁县| 泰兴市| 富裕县| 河曲县| 康保县| 昭通市| 五寨县| 河池市| 汉川市| 通海县| 长子县| 长岛县| 班玛县| 赞皇县| 望都县| 凌海市| 延寿县| 北碚区| 峨眉山市| 彭水|