VB6窗口操作

来源:互联网 发布:淘宝什么是清关信息 编辑:程序博客网 时间:2024/05/16 04:16

 '  
  '窗口操作(SmWinCtrl)  
  '  
   
  Option   Explicit  
   
  Private   Const   HWND_TOP   =   0  
  Private   Const   HWND_BOTTOM   =   1  
  Private   Const   HWND_TOPMOST   =   -1  
  Private   Const   HWND_NOTOPMOST   =   -2  
   
  Private   Const   SWP_NOSIZE   =   &H1  
  Private   Const   SWP_NOMOVE   =   &H2  
  Private   Const   SWP_NOZORDER   =   &H4  
  Private   Const   SWP_NOREDRAW   =   &H8  
  '/*以下常数在   ShowWindow中应用*  
  Private   Const   SW_SHOWMAXIMIZED   =   3  
  Private   Const   SW_HIDE   =   0  
  Private   Const   SW_MINIMIZE   =   6  
  Private   Const   SW_RESTORE   =   9  
  Private   Const   SW_SHOW   =   5  
  Private   Const   SW_SHOWMINIMIZED   =   2  
  Private   Const   SW_SHOWMINNOACTIVE   =   7  
  Private   Const   SW_SHOWNA   =   8  
  Private   Const   SW_SHOWNOACTIVATE   =   4  
  Private   Const   SW_SHOWNORMAL   =   1  
  '/====================================  
  '/隐藏窗口,活动状态给令一个窗口  
  '/     SW_HIDE  
  '/     最小化窗口,活动状态给令一个窗口  
  '/     SW_MINIMIZE  
  '/   用原来的大小和位置显示一个窗口,同时令其进入活动状态  
  '/     SW_RESTORE  
  '/用当前的大小和位置显示一个窗口,同时令其进入活动状态  
  '/     SW_SHOW  
  '/     最大化窗口,并将其激活  
  '/   SW_SHOWMAXIMIZED  
  '/     最小化窗口,并将其激活  
  '/     SW_SHOWMINIMIZED  
  '/     最小化一个窗口,同时不改变活动窗口  
  '/     SW_SHOWMINNOACTIVE  
  '/     用当前的大小和位置显示一个窗口,不改变活动窗口  
  '/   SW_SHOWNA  
  '/   用最近的大小和位置显示一个窗口,同时不改变活动窗口  
  '/   SW_SHOWNOACTIVATE  
  '/     与SW_RESTORE相同  
  '/SW_SHOWNORMAL  
  Private   Const   xMenuID   =   10&  
  Private   Const   WM_NCACTIVATE   =   &H86  
  '/********************************************  
  '/MENU   ID  
  Private   Const   SC_RESTORE   =   &HF120&     '还原  
  Private   Const   SC_MOVE   =   &HF010&   '移动  
  Private   Const   SC_SIZE   =   &HF000&   '大小  
  Private   Const   SC_MINIMIZE   =   &HF020&   '缩到最小  
  Private   Const   SC_MAXIMIZE   =   &HF030&   '放到最大  
  Private   Const   SC_CLOSE   =   &HF060&   '关闭  
  '/********************************************  
   
  Private   Const   MIIM_STATE   =   &H1&  
  Private   Const   MIIM_ID   =   &H2&  
   
  Private   Const   MFS_GRAYED   =   &H3&  
  Private   Const   MFS_CHECKED   =   &H8&  
   
  Private   Type   MENUITEMINFO  
          cbSize   As   Long  
          fMask   As   Long  
          fType   As   Long  
          fState   As   Long  
          Wid   As   Long  
          hSubMenu   As   Long  
          hbmpChecked   As   Long  
          hbmpUnchecked   As   Long  
          dwItemData   As   Long  
          dwTypeData   As   String  
          cch   As   Long  
  End   Type  
   
  Private   Declare   Function   GetSystemMenu   Lib   "user32"   (ByVal   hWnd   As   Long,   ByVal   bRevert   As   Long)   As   Long  
  Private   Declare   Function   GetMenuItemInfo   Lib   "user32"   Alias   "GetMenuItemInfoA"   (ByVal   hMenu   As   Long,   ByVal   un   As   Long,   ByVal   B   As   Boolean,   lpMenuItemInfo   As   MENUITEMINFO)   As   Long  
  Private   Declare   Function   SetMenuItemInfo   Lib   "user32"   Alias   "SetMenuItemInfoA"   (ByVal   hMenu   As   Long,   ByVal   un   As   Long,   ByVal   bool   As   Boolean,   lpcMenuItemInfo   As   MENUITEMINFO)   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   ShowWindow   Lib   "user32"   _  
                                                              (ByVal   hWnd   As   Long,   _  
                                                              ByVal   nCmdShow   As   Long)   As   Long  
  '/恢复一个最小化的程序,并将其激活  
  Private   Declare   Function   OpenIcon   Lib   "user32"   (ByVal   hWnd   As   Long)   As   Long  
  '/将输入焦点移到指定的窗体(必须与调用者同一线程)  
  Private   Declare   Function   SetFocus   Lib   "user32"   (ByVal   hWnd   As   Long)   As   Long  
  '/激活指定窗口(必须与调用者同一线程)  
  Private   Declare   Function   SetActiveWindow   Lib   "user32"   (ByVal   hWnd   As   Long)   As   Long  
  '/获取前台窗口或者聚焦窗口的句柄  
  Private   Declare   Function   GetForegroundWindow   Lib   "user32"   ()   As   Long  
   
  '/===============================================================================  
  '/取窗口标题  
  Private   Declare   Function   GetWindowText   Lib   "user32"   Alias   "GetWindowTextA"   _  
                                                                                                            (ByVal   hWnd   As   Long,   _  
                                                                                                            ByVal   lpString   As   String,   _  
                                                                                                            ByVal   cch   As   Long)   As   Long  
  '/设置窗口标题  
  Private   Declare   Function   SetWindowText   Lib   "user32"   Alias   "SetWindowTextA"   _  
                                                                                                              (ByVal   hWnd   As   Long,   _  
                                                                                                                ByVal   lpString   As   String)   As   Long  
  '/将窗口放在前台  
  Private   Declare   Function   SetForegroundWindow   Lib   "user32"   (ByVal   hWnd   As   Long)   As   Long  
  '/向窗口发送消息  
  Private   Declare   Function   SendMessage   Lib   "user32"   Alias   "SendMessageA"   _  
                                                          (ByVal   hWnd   As   Long,   ByVal   wMsg   As   Long,   _  
                                                          ByVal   wParam   As   Long,   lParam   As   Any)   As   Long  
  '/----------------------------------------------------------------------  
  '/关闭一个窗口  
  '/HWND   要关闭的窗口的句柄  
  '/返回值     如果非零,则成功,否则失败  
  Private   Declare   Function   DestroyWindow   Lib   "user32"   (ByVal   hWnd   As   Long)   As   Long  
   
  '/为窗体指定一个新父  
  '/自身的HWND,新父的HWND.  
  Private   Declare   Function   SetParent   Lib   "user32"   (ByVal   HwndChild   As   Long,   ByVal   HWndNewParent   As   Long)   As   Long  
   
  '  
  '设置窗口为最顶层  
  '函数:SetTopWindow  
  '参数:Winwnd   要设置为最顶层窗口的HWND  
  '返回值:  
  '例子:  
  Public   Function   SetTopWindow(WinWnd   As   Long)  
                SetWindowPos   WinWnd,   HWND_TOPMOST,   0,   0,   0,   0,   SWP_NOSIZE   Or   SWP_NOMOVE  
  End   Function  
   
  Public   Function   MoveWindow(WinHwnd   As   Long,   X   As   Long,   Y   As   Long)  
                SetWindowPos   WinHwnd,   HWND_NOTOPMOST,   X,   Y,   0,   0,   SWP_NOSIZE   Or   SWP_NOMOVE  
  End   Function   
     '  
  '将窗口位于Z轴方向的最前.  
  '函数:SetTopWindowZ  
  '参数:MWinwnd   要设置为Z轴最前窗口的HWND  
  '返回值:  
  '例子:  
  Public   Function   SetTopWindowZ(MWinWnd   As   Long)  
      Dim   WinHwnd   As   Long  
      WinHwnd   =   GetForegroundWindow()  
      If   WinHwnd   <>   MWinWnd   And   WinHwnd   <>   0   Then  
            SetWindowPos   WinHwnd,   HWND_TOP,   0,   0,   0,   0,   SWP_NOSIZE   Or   SWP_NOMOVE  
      End   If  
  End   Function  
   
  '  
  '将最顶层窗口恢复为正常  
  '函数:NoTopWindow  
  '参数:Winwnd   要恢复窗口的HWND  
  '返回值:  
  '例子:  
  Public   Function   NoTopWindow(WinWnd   As   Long)  
                SetWindowPos   WinWnd,   HWND_NOTOPMOST,   0,   0,   0,   0,   SWP_NOSIZE   Or   SWP_NOMOVE  
  End   Function  
   
  '  
  '将焦点移到指定的窗体上  
  '函数:SetWinFocus  
  '参数:hWnd   操作窗口HWND  
  '返回值:  
  '例子:  
  Public   Sub   SetWinFocus(hWnd   As   Long)  
                Call   SetFocus(hWnd)  
  End   Sub  
   
  '  
  '无关闭按钮.  
  '函数:NoClose  
  '参数:FHwnd   操作窗口HWND  
  '返回值:  
  '例子:  
  Public   Function   NoClose(FHwnd   As   Long)  
          Dim   hMenu   As   Long,   MII   As   MENUITEMINFO  
           
          hMenu   =   GetSystemMenu(FHwnd,   0)  
          MII.cbSize   =   Len(MII)  
          MII.dwTypeData   =   String(80,   0)  
          MII.cch   =   Len(MII.dwTypeData)  
          MII.fMask   =   MIIM_STATE  
          MII.Wid   =   SC_CLOSE  
          GetMenuItemInfo   hMenu,   SC_CLOSE,   False,   MII  
          MII.Wid   =   xMenuID  
          MII.fMask   =   MIIM_ID  
          SetMenuItemInfo   hMenu,   SC_CLOSE,   False,   MII  
          MII.fState   =   MII.fState   Or   MFS_GRAYED  
          MII.fMask   =   MIIM_STATE  
          SetMenuItemInfo   hMenu,   MII.Wid,   False,   MII  
          SendMessage   FHwnd,   WM_NCACTIVATE,   True,   ByVal   0&  
  End   Function  
   
  '  
  '无最小化按钮  
  '函数:NoMin  
  '参数:FHwnd   操作窗口HWND  
  '返回值:  
  '例子:  
  Public   Function   NoMin(FHwnd   As   Long)  
          Dim   hMenu   As   Long,   MII   As   MENUITEMINFO  
          hMenu   =   GetSystemMenu(FHwnd,   0)  
          MII.cbSize   =   Len(MII)  
          MII.dwTypeData   =   String(80,   0)  
          MII.cch   =   Len(MII.dwTypeData)  
          MII.fMask   =   MIIM_STATE  
          MII.Wid   =   SC_MINIMIZE  
          GetMenuItemInfo   hMenu,   SC_MINIMIZE,   False,   MII  
          MII.Wid   =   xMenuID  
          MII.fMask   =   MIIM_ID  
          SetMenuItemInfo   hMenu,   SC_MINIMIZE,   False,   MII  
          MII.fState   =   MII.fState   Or   MFS_GRAYED  
          MII.fMask   =   MIIM_STATE  
          SetMenuItemInfo   hMenu,   MII.Wid,   False,   MII  
          SendMessage   FHwnd,   WM_NCACTIVATE,   True,   ByVal   0&  
  End   Function  
   
  '  
  '无最大化按钮.  
  '函数:NoMax  
  '参数:FHwnd   操作窗口HWND  
  '返回值:   
'例子:  
  Public   Function   NoMax(FHwnd   As   Long)  
          Dim   hMenu   As   Long,   MII   As   MENUITEMINFO  
          hMenu   =   GetSystemMenu(FHwnd,   0)  
          MII.cbSize   =   Len(MII)  
          MII.dwTypeData   =   String(80,   0)  
          MII.cch   =   Len(MII.dwTypeData)  
          MII.fMask   =   MIIM_STATE  
          MII.Wid   =   SC_RESTORE  
          GetMenuItemInfo   hMenu,   SC_RESTORE,   False,   MII  
          MII.Wid   =   xMenuID  
          MII.fMask   =   MIIM_ID  
          SetMenuItemInfo   hMenu,   SC_RESTORE,   False,   MII  
          MII.fState   =   MII.fState   Or   MFS_GRAYED  
          MII.fMask   =   MIIM_STATE  
          SetMenuItemInfo   hMenu,   MII.Wid,   False,   MII  
          SendMessage   FHwnd,   WM_NCACTIVATE,   True,   ByVal   0&  
  End   Function