当前位置: 首页 > 工具软件 > wxRuby > 使用案例 >

wxruby之布局模型

秦钟展
2023-12-01

概括、wxruby的布局模型采用boxsizer和gridsizer相结合的方式,两者互有特点且可以互相嵌套,灵活的利用两者可以制作出布局灵活的窗口。

也可以Point.new()指定绝对位置。

一、boxsizer模型

#coding=utf-8

require "wx"

def framebox(frame)
	vbox=Wx::BoxSizer.new(Wx::VERTICAL)
	vbox.add(Wx::Button.new(frame, 1010, "vertical1"), 0, Wx::ALIGN_LEFT) #对于vertical来说,left和right有意义
	vbox.add(Wx::Button.new(frame, 1010, "vertical2"), 1, Wx::EXPAND)
	vbox.add(10,20,0) #对于vertical box来说,第一个参数无意义
	vbox.add(Wx::Button.new(frame, 1010, "vertical3"), 0, Wx::ALIGN_RIGHT)
	hbox=Wx::BoxSizer.new(Wx::HORIZONTAL)
	hbox.add(vbox,1,Wx::EXPAND|Wx::ALL,15) #设置边框
	hbox.add(0,0,1) #插入占位空白符
	hbox.add(Wx::Button.new(frame, 1010, "horizontal1"), 1, Wx::ALIGN_TOP|Wx::NORTH, 15) #设置边框
	hbox.add(Wx::Button.new(frame, 1010, "horizontal2"), 1, Wx::EXPAND)
	hbox.add(Wx::Button.new(frame, 1010, "horizontal3"), 1, Wx::ALIGN_BOTTOM)
	
	return hbox	
end

#boxsizer模型可以实现组件的简单的横向或者纵向排列

class ShadowFrame < Wx::Frame
	def initialize(title,pos,size)
		super(nil,-1,title,pos,size)
		sizer=framebox(self)
		#sizer.fit(self)
		set_sizer(sizer)
	end
end

class ShadowApp < Wx::App
  def on_init
    topframe = ShadowFrame.new("shadow",Wx::DEFAULT_POSITION,Wx::Size.new(640,300))
		topframe.show(true)
  end
end

shadow = ShadowApp.new
shadow.main_loop

二、gridsizer模型

gridsizer模型的优势在于快速的将窗体分割成若干块,比boxsizer更灵活。

#coding=utf-8

require "wx"

def framegrid(frame)
#Wx::FlexGridSize 不会自动EXPAND
	grid=Wx::GridSizer.new(3,3,2,2) #3行,3列,行间距及列间距都是2 
	grid.add(Wx::Button.new(frame, 1010, "one"), 0, Wx::ALIGN_TOP)
	grid.add(Wx::Button.new(frame, 1010, "two"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "three"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "four"), 0, Wx::EXPAND)
	#grid.add(Wx::Button.new(frame, 1010, "five"), 0, Wx::EXPAND)
	grid.add(175, 50) #添加空白块
	grid.add(Wx::Button.new(frame, 1010, "six"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "seven"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "eight"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "nine"), 0, Wx::EXPAND)	
	
	return grid	
end

#boxsizer模型可以实现组件的简单的横向或者纵向排列

class ShadowFrame < Wx::Frame
	def initialize(title,pos,size)
		super(nil,-1,title,pos,size)
		#sizer=framegrid(self)
		sizer=make_grid3(self)
		#sizer.fit(self)
		set_sizer(sizer)
	end
end

class ShadowApp < Wx::App
  def on_init
    topframe = ShadowFrame.new("shadow",Wx::DEFAULT_POSITION,Wx::Size.new(640,300))
		topframe.show(true)
  end
end

shadow = ShadowApp.new
shadow.main_loop

三、混合使用

嵌套使用两者才能实现灵活的组件布局

#coding=utf-8

require "wx"

def boxgridgrid(frame)
#Wx::FlexGridSize 不会自动EXPAND
	grid=Wx::GridSizer.new(3,3,2,2) #3行,3列,行间距及列间距都是2 
	grid.add(Wx::Button.new(frame, 1010, "one"), 0, Wx::ALIGN_TOP|Wx::LEFT)
	grid.add(Wx::Button.new(frame, 1010, "two"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "three"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "four"), 0, Wx::EXPAND)
	#grid.add(Wx::Button.new(frame, 1010, "five"), 0, Wx::EXPAND)
	grid.add(175, 50) #添加空白块
	grid.add(Wx::Button.new(frame, 1010, "six"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "seven"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "eight"), 0, Wx::EXPAND)
	grid.add(Wx::Button.new(frame, 1010, "nine"), 0, Wx::EXPAND)	
	
	vbox=Wx::BoxSizer.new(Wx::VERTICAL)
	vbox.add(Wx::Button.new(frame, 1010, "vertical1"), 0, Wx::ALIGN_LEFT) #对于vertical来说,left和right有意义
	vbox.add(Wx::Button.new(frame, 1010, "vertical2"), 1, Wx::EXPAND)
	vbox.add(10,20,0) #对于vertical box来说,第一个参数无意义
	vbox.add(Wx::Button.new(frame, 1010, "vertical3"), 0, Wx::ALIGN_RIGHT)
	hbox=Wx::BoxSizer.new(Wx::HORIZONTAL)
	hbox.add(vbox,1,Wx::EXPAND|Wx::ALL,15) #设置边框
	hbox.add(0,0,0) #插入占位空白符
	hbox.add(Wx::Button.new(frame, 1010, "horizontal1"), 0, Wx::ALIGN_TOP|Wx::NORTH, 15) #设置边框
	hbox.add(Wx::Button.new(frame, 1010, "horizontal2"), 0, Wx::EXPAND)
	hbox.add(grid,1,Wx::EXPAND)
	
	return hbox	
end

#boxsizer模型可以实现组件的简单的横向或者纵向排列

class ShadowFrame < Wx::Frame
	def initialize(title,pos,size)
		super(nil,-1,title,pos,size)
		sizer=boxgridgrid(self)
		#sizer.fit(self)
		set_sizer(sizer)
	end
end

class ShadowApp < Wx::App
  def on_init
    topframe = ShadowFrame.new("shadow",Wx::POINT.new(100,50),Wx::Size.new(640,300)) 
		topframe.show(true)
  end
end

shadow = ShadowApp.new
shadow.main_loop

四、当窗体上的组件比较少时,可以使用组件的绝对定位

#coding=utf-8

require "wx"

BTN_ID=2000

class ShadowFrame < Wx::Frame
	def initialize(title, pos, size, style=Wx::DEFAULT_FRAME_STYLE)
		super(nil,-1,title,pos,size,style)
		btn1 = Wx::Button.new(self, BTN_ID, "start", Wx::Point.new(50,20))
		btn2 = Wx::Button.new(self, BTN_ID, "stop", Wx::Point.new(100,60))		
		center(Wx::BOTH)		
	end	
end

#如果父窗口里只有一个组件,则位置定位信息无效。

class ShadowApp < Wx::App
  def on_init
    topframe = ShadowFrame.new("shadow",Wx::DEFAULT_POSITION,Wx::Size.new(400,200),Wx::CAPTION|Wx::SYSTEM_MENU|Wx::CLOSE_BOX)
		topframe.show(true)
  end
end

shadow = ShadowApp.new
shadow.main_loop



 类似资料: