VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 在运行时显示或隐藏窗体的标题栏
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:9915, 日期:2000-12-30
这则代码告诉你如何在运行时显示或隐藏窗体的标题栏。要使一个窗口的标题栏消失,你必须去掉control box、最大化按钮和最小化按钮,并且将caption设为空。不幸的是,vb中窗体的controlbox、minbutton和maxbutton属性在运行期是只读的,因此,你只能在设计时做这些事。其实,只要能熟练操作关于窗口式样的api,你同样能在运行时办到这一点。
  新建一个项目,把以下代码写入窗体:

private declare function setwindowlong lib "user32" alias "setwindowlonga" _
(byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long

private declare function getwindowlong lib "user32" alias "getwindowlonga" _
(byval hwnd as long, byval nindex as long) as long

private const gwl_style = (-16)
private const ws_caption = &hc00000   注释: ws_border 或 ws_dlgframe
private const ws_maximizebox = &h10000
private const ws_minimizebox = &h20000
private const ws_sysmenu = &h80000

private declare function setwindowpos lib "user32" _
(byval hwnd as long, byval hwndinsertafter as long, byval x as long, _
byval y as long, byval cx as long, byval cy as long, byval wflags as long) as long

private enum esetwindowposstyles
  swp_showwindow = &h40
  swp_hidewindow = &h80
  swp_framechanged = &h20 注释: the frame changed: send wm_nccalcsize
  swp_noactivate = &h10
  swp_nocopybits = &h100
  swp_nomove = &h2
  swp_noownerzorder = &h200 注释: don注释:t do owner z ordering
  swp_noredraw = &h8
  swp_noreposition = swp_noownerzorder
  swp_nosize = &h1
  swp_nozorder = &h4
  swp_drawframe = swp_framechanged
  hwnd_notopmost = -2
end enum

private declare function getwindowrect lib "user32" (byval hwnd as long, lprect as rect) as long
private type rect
  left as long
  top as long
  right as long
  bottom as long
end type

private function showtitlebar(byval bstate as boolean)
dim lstyle as long
dim tr as rect

  注释: 获取窗口的位置:
  getwindowrect me.hwnd, tr

  注释: 调整标题栏是否可见:
  lstyle = getwindowlong(me.hwnd, gwl_style)
  if (bstate) then
  me.caption = me.tag
  if me.controlbox then
    lstyle = lstyle or ws_sysmenu
  end if
  if me.maxbutton then
    lstyle = lstyle or ws_maximizebox
  end if
  if me.minbutton then
    lstyle = lstyle or ws_minimizebox
  end if
  if me.caption <> "" then
    lstyle = lstyle or ws_caption
  end if
  else
  me.tag = me.caption
  me.caption = ""
  lstyle = lstyle and not ws_sysmenu
  lstyle = lstyle and not ws_maximizebox
  lstyle = lstyle and not ws_minimizebox
  lstyle = lstyle and not ws_caption
end if
setwindowlong me.hwnd, gwl_style, lstyle

注释: 重新设定窗口:
setwindowpos me.hwnd, 0, tr.left, tr.top, tr.right - tr.left, tr.bottom - tr.top, swp_noreposition or swp_nozorder or swp_framechanged
me.refresh

注释: 你可能需要在form_resize中加一点代码,因为客户区的大小已经改变:
注释:form_resize

end function


  为了试验一下代码,在窗体上放一个checkbox,将它的value属性设为1 (checked)。然后写入以下代码:

private sub check1_click()
  if (check1.value = checked) then
  showtitlebar true
  else
  showtitlebar false
end if
end sub

  运行,当你点击这个checkbox时,窗体的标题栏将会在隐藏或显示之间切换。