CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

如何在窗口的标题栏上加一个自己的按钮?

楼主Saytoyou(Cary)2004-08-03 12:28:02 在 VB / 基础类 提问

如何在窗口的标题栏上加一个自己的按钮?就象“最大”“恢复”和“最小化”按钮一样?  
  问题点数:20、回复次数:2Top

1 楼rainstormmaster(暴风雨 v2.0)回复于 2004-08-03 12:30:40 得分 5

需要自绘标题栏  
   
  或者cut掉标题栏,然后自己模拟标题栏Top

2 楼BlueBeer(1win)回复于 2004-08-03 12:31:39 得分 15

用  
  SetParent   Command1.hwnd,   0  
  SetWindowLong   Command1.hwnd,   -8,   Me.hwnd  
  把command1分离窗口出来,使之能自由移动  
  对me做子类处理,捕获WM_MOVE消息,处理command1的位置  
   
  需要注意的是如果希望command1响应事件,需要把它放在一个容器内  
  如frame1,然后对frame1使用上面的那两个函数。  
  可以参考  
  http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=7691  
   
  下面是个简单的例子:  
  在窗体中放上一个frame,在frame中放上一个commandbutton  
  '窗体代码  
  Private   Sub   Command1_Click()  
  Debug.Print   "Click   the   title   button"  
  End   Sub  
   
  Private   Sub   Form_Load()  
  Frame1.BorderStyle   =   0  
  Command1.Left   =   0  
  Command1.Top   =   0  
  Command1.Width   =   240  
  Command1.Height   =   210  
  moveHwnd   =   Frame1.hwnd  
  SetParent   moveHwnd,   0  
  SetWindowLong   moveHwnd,   -8,   Me.hwnd  
  gHW   =   Me.hwnd  
  hook  
  End   Sub  
   
  Private   Sub   Form_Unload(Cancel   As   Integer)  
  Unhook  
  End   Sub  
   
  '模块部分的代码  
   
  Option   Explicit  
  Public   moveHwnd   As   Long  
   
  Public   Const   GWL_WNDPROC   =   -4  
  Global   lpPrevWndProc   As   Long  
  Global   gHW   As   Long  
  Declare   Function   CallWindowProc   Lib   "user32"   Alias   "CallWindowProcA"   (ByVal   lpPrevWndFunc   As   Long,   ByVal   hwnd   As   Long,   ByVal   MSG   As   Long,   ByVal   wParam   As   Long,   ByVal   lParam   As   Long)   As   Long  
  Declare   Function   SetWindowLong   Lib   "user32"   Alias   "SetWindowLongA"   (ByVal   hwnd   As   Long,   ByVal   nIndex   As   Long,   ByVal   dwNewLong   As   Long)   As   Long  
   
  Public   Declare   Function   SetParent   Lib   "user32"   (ByVal   hWndChild   As   Long,   ByVal   hWndNewParent   As   Long)   As   Long  
  Public   Const   WM_MOVE   As   Long   =   &H3  
  Public   Declare   Function   MoveWindow   Lib   "user32"   (ByVal   hwnd   As   Long,   ByVal   x   As   Long,   ByVal   y   As   Long,   ByVal   nWidth   As   Long,   ByVal   nHeight   As   Long,   ByVal   bRepaint   As   Long)   As   Long  
  Public   Declare   Function   GetWindowRect   Lib   "user32"   (ByVal   hwnd   As   Long,   lpRect   As   RECT)   As   Long  
  Public   Type   RECT  
          Left   As   Long  
          Top   As   Long  
          Right   As   Long  
          Bottom   As   Long  
   
  End   Type          
           
  Function   WindowProc(ByVal   hw   As   Long,   ByVal   uMsg   As   Long,   ByVal   wParam   As   Long,   ByVal   lParam   As   Long)   As   Long  
   
  Select   Case   uMsg  
          Case   WM_MOVE  
                  Dim   t   As   RECT  
                  GetWindowRect   hw,   t  
                  MoveWindow   moveHwnd,   t.Right   -   72,   t.Top   +   6,   16,   14,   1  
          Case   Else    
  End   Select  
  WindowProc   =   CallWindowProc(lpPrevWndProc,   hw,   uMsg,   wParam,   lParam)  
  End   Function  
           
  Public   Sub   hook()      
    lpPrevWndProc   =   SetWindowLong(gHW,   GWL_WNDPROC,   AddressOf   WindowProc)  
  End   Sub  
           
  Public   Sub   Unhook()  
    Dim   temp   As   Long  
    temp   =   SetWindowLong(gHW,   GWL_WNDPROC,   lpPrevWndProc)  
  End   Sub  
   
   
  '方法二==========================================================  
   
   
  '模块  
  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   GetWindowRect   Lib   "user32"   (ByVal   hwnd   As   Long,   lpRect   As   Rect)   As   Long  
  Private   Declare   Function   GetParent   Lib   "user32"   (ByVal   hwnd   As   Long)   As   Long  
  Private   Declare   Function   SetParent   Lib   "user32"   (ByVal   hWndChild   As   Long,   ByVal   hWndNewParent   As   Long)   As   Long  
  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   Declare   Function   SetWindowsHookEx   Lib   "user32"   Alias   "SetWindowsHookExA"   (ByVal   idHook&,   ByVal   lpfn&,   ByVal   hmod&,   ByVal   dwThreadId&)   As   Long  
  Private   Declare   Function   UnhookWindowsHookEx   Lib   "user32"   (ByVal   hHook&)   As   Long  
  Private   Declare   Function   CreateWindowEx   Lib   "user32"   Alias   "CreateWindowExA"   (ByVal   dwExStyle   As   Long,   ByVal   lpClassName   As   String,   ByVal   lpWindowName   As   String,   ByVal   dwStyle   As   Long,   ByVal   x   As   Long,   ByVal   y   As   Long,   ByVal   nWidth   As   Long,   ByVal   nHeight   As   Long,   ByVal   hWndParent   As   Long,   ByVal   hMenu   As   Long,   ByVal   hInstance   As   Long,   lpParam   As   Any)   As   Long  
  Private   Declare   Function   ShowWindow   Lib   "user32"   (ByVal   hwnd   As   Long,   ByVal   nCmdShow   As   Long)   As   Long  
   
  '*********************  
  '*   Type   Declarations   *  
  '*********************  
  Private   Type   Rect  
          Left   As   Long  
          Top   As   Long  
          Right   As   Long  
          Bottom   As   Long  
  End   Type  
   
  Private   Type   CWPSTRUCT  
          lParam   As   Long  
          wParam   As   Long  
          Message   As   Long  
          hwnd   As   Long  
  End   Type  
   
  '*********************  
  '*   Consts                         *  
  '*********************  
  Const   WM_MOVE   =   &H3  
  Const   WM_SETCURSOR   =   &H20  
  Const   WM_NCPAINT   =   &H85  
  Const   WM_COMMAND   =   &H111  
   
  Const   SWP_FRAMECHANGED   =   &H20  
  Const   GWL_EXSTYLE   =   -20  
   
  '*********************  
  '*   Vars                             *  
  '*********************  
  Private   WHook&  
  Private   ButtonHwnd   As   Long  
   
  Public   Sub   Init()  
          'Create   the   button   that   is   going   to   be   placed   in   the   Titlebar  
          ButtonHwnd&   =   CreateWindowEx(0&,   "Button",   "?",   &H40000000,   50,   50,   14,   14,   Form1.hwnd,   0&,   App.hInstance,   0&)  
          'Show   the   button   cause   it磗   invisible  
          Call   ShowWindow(ButtonHwnd&,   1)  
          'Initialize   the   window   hooking   for   the   button  
          WHook   =   SetWindowsHookEx(4,   AddressOf   HookProc,   0,   App.ThreadID)  
          Call   SetWindowLong(ButtonHwnd&,   GWL_EXSTYLE,   &H80)  
          Call   SetParent(ButtonHwnd&,   GetParent(Form1.hwnd))  
  End   Sub  
   
  Public   Sub   Terminate()  
          'Terminate   the   window   hooking  
          Call   UnhookWindowsHookEx(WHook)  
          Call   SetParent(ButtonHwnd&,   Form1.hwnd)  
  End   Sub  
   
  Public   Function   HookProc&(ByVal   nCode&,   ByVal   wParam&,   Inf   As   CWPSTRUCT)  
          Dim   FormRect   As   Rect  
          Static   LastParam&  
          If   Inf.hwnd   =   GetParent(ButtonHwnd&)   Then  
                  If   Inf.Message   =   WM_COMMAND   Then  
                          Select   Case   LastParam  
                                  'If   the   LastParam   is   cmdInTitlebar   call   the   Click-Procedure  
                                  'of   the   button  
                                  Case   ButtonHwnd&:   Call   Form1.cmdInTitlebar_Click  
                          End   Select  
                  ElseIf   Inf.Message   =   WM_SETCURSOR   Then  
                          LastParam   =   Inf.wParam  
                  End   If  
                  ElseIf   Inf.hwnd   =   Form1.hwnd   Then  
                  If   Inf.Message   =   WM_NCPAINT   Or   Inf.Message   =   WM_MOVE   Then  
                          'Get   the   size   of   the   Form  
                          Call   GetWindowRect(Form1.hwnd,   FormRect)  
                          'Place   the   button   int   the   Titlebar  
                          Call   SetWindowPos(ButtonHwnd&,   0,   FormRect.Right   -   75,   FormRect.Top   +   6,   17,   14,   SWP_FRAMECHANGED)  
                  End   If  
          End   If  
  End   Function  
   
  '窗体  
  Public   Sub   cmdInTitlebar_Click()  
          MsgBox   "Example   created   by   Druid   Developing",   vbInformation,   "About   this   program"  
  End   Sub  
   
  Private   Sub   Form_Load()  
          Call   Init  
  End   Sub  
   
  Private   Sub   Form_Unload(Cancel   As   Integer)  
          Call   Terminate  
  End   SubTop

相关问题

  • 如何在窗口标题栏上添加按钮?
  • 在标题栏上加些按钮?
  • 给窗口加标题栏
  • 怎样使窗口标题栏中的“关闭”按钮变灰?
  • 如何在标题栏上增加按钮?
  • 怎样在标题栏上添加自己的按钮
  • 窗口标题栏
  • JFrame窗口,点击“还原按钮”时,为何缩小到只有标题栏
  • 在窗体标题栏增加一个按钮后,如何实现点击这个按钮后窗体只剩下标题栏?
  • 请问高手,如何能在标题栏添加删除按钮??

关键词

  • me
  • 标题栏
  • movehwnd
  • 按钮
  • command1
  • frame1
  • setwindowlong
  • byval
  • hwnd
  • sub

得分解答快速导航

  • 帖主:Saytoyou
  • rainstormmaster
  • BlueBeer

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo