vb最为人垢病的是它的面向对象特性。实际上vb是一种基于对象的开发工具。在vb中
建立的类是支持继承的。下面是范例:
首先建立一个新工程,然后添加一个新的类模块(class module),类名称设定为baseclass。
然后在baseclass中加入以下代码:
public sub basesub() '虚拟特性,basesub在子类中实现
end sub
然后添加两个类模块,类名称分别设定为impclass以及impclass2,然后在类的代码窗口中写入:
implements baseclass '继承特性
上面这行代码说明类impclass以及impclass2实现类baseclass。
在impclass窗口中加入以下代码:
private sub baseclass_basesub() '实现基类中的basesub方法
msgbox "hello. this is imp. inherited from baseclass"
end sub
在impclass2中加入以下代码:
private sub baseclass_basesub()
msgbox "hello. this is imp2. inherited from baseclass"
end sub
完成了上面的类代码后,打开form1,在上面添加一个commandbutton,在按钮的click事件中
写入以下代码:
dim ximp as new impclass
dim ximp2 as new impclass2
dim xbase as baseclass
set xbase = ximp '多态特性
xbase.basesub
set xbase = ximp2
xbase.basesub
set xbase = nothing
set ximp = nothing
set ximp2 = nothing
运行程序,点击commandbutton,程序会先后弹出消息框,显示在impclass以及impclass2中
设定的消息。
从上面的代码中可以看到vb中是如何实现面向对象的特性:继承、虚拟以及多态的。只是同
诸如java、c++、object pascal不同,vb将很多实现的细节隐藏了起来。
问:如何屏蔽掉窗体中的关闭按钮x?
答:可以使用api函数将窗体菜单中的 关闭 项灰掉,因为菜单同关闭按钮是关联的,这样关闭
按钮也会不可用。具体代码如下:
option explicit
private declare function getsystemmenu lib "user32" _
(byval hwnd as long, byval brevert as long) as long
private declare function removemenu lib "user32" _
(byval hmenu as long, byval nposition as long, _
byval wflags as long) as long
private declare function enablemenuitem lib "user32" _
(byval hmenu as long, byval widenableitem as long, _
byval wenable as long) as long
const sc_close = &hf060
private sub form_load()
dim hmenu as long
hmenu = getsystemmenu(me.hwnd, 0)
removemenu hmenu, &hf060, &h200&
debug.print enablemenuitem(hmenu, sc_close, 1)
end sub
www.applevb.com