require 'tk' require 'tkmove2.rb' def bind( c ) c.bind( '1', proc{|x, y| $id = create_box(c, x, y) }, "%x %y") c.bind( 'B1-Motion', proc{|x, y| adjust_box( c, x, y ) }, "%x %y" ) end def unbind( c ) c.bind( '1', proc{} ) c.bind( 'B1-Motion', proc{} ) end $x0, $y0 = 0, 0 def create_box(c, x, y) box = TkcRectangle.new(c, x, y, x, y, 'width'=>2) box.addtag('box') c.dtag 'selected' c.addtag_withtag 'selected', box.id $x0, $y0 = x, y end def adjust_box( c, x, y ) c.coords('selected', $x0, $y0, x, y ) end c = TkCanvas.new.pack f = TkFrame.new.pack m = TkMove.new(c, 'box') m.unbind(c, 'box') bind(c) TkButton.new(f,'text'=>'Draw', 'command'=>proc{ bind( c ); m.unbind(c, 'box')}).pack('side'=>'left') TkButton.new(f,'text'=>'Move', 'command'=>proc{ unbind( c ); m.bind(c, 'box')}).pack('side'=>'left') Tk.mainloop