⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autohidexp.h

📁 这是一本学习 window编程的很好的参考教材
💻 H
字号:
#if !defined(AFX_AUTOHIDE_H__20010516_C3E9_46AD_95FE_0080AD509054__INCLUDED_)
#define AFX_AUTOHIDE_H__20010516_C3E9_46AD_95FE_0080AD509054__INCLUDED_

#pragma once

/////////////////////////////////////////////////////////////////////////////
// AutoHideXp.h - An AutoHide control
//
// Written by Bjarke Viksoe (bjarke@viksoe.dk)
// Copyright (c) 2001-2003 Bjarke Viksoe.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. 
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//

#ifndef __cplusplus
   #error ATL requires C++ compilation (use a .cpp suffix)
#endif

#ifndef __ATLAPP_H__
   #error AutoHideXP.h requires atlapp.h to be included first
#endif


#define ATL_SIMPLE_AUTOHIDEVIEW_STYLE \
   (WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS)

// Sliding window animation delay
#ifndef AUTOHIDE_DELAY_IN
   #define AUTOHIDE_DELAY_IN  200
   #define AUTOHIDE_DELAY_OUT 100
#endif

#define AUTOHIDE_LEFT    0
#define AUTOHIDE_BOTTOM  1

typedef struct
{
   HWND hWnd;
   TCHAR szTitle[80];
   int iImage;
   int iDirection;
   RECT rc;
} AUTOPANE;

#define WM_AUTOHIDE_SETPANE    WM_USER + 360
#define WM_AUTOHIDE_VIEWCLOSE  WM_USER + 361


///////////////////////////////////////////////////////
// CAutoFloatWindow

typedef CWinTraits<WS_POPUP|WS_CAPTION|WS_THICKFRAME|WS_SYSMENU, WS_EX_TOOLWINDOW> CAutoFloatWinTraits;

template< class T, class TBase = CWindow, class TWinTraits = CAutoFloatWinTraits >
class ATL_NO_VTABLE CAutoFloatWindowImpl : 
   public CWindowImpl< T, TBase, TWinTraits >
{
public:
   DECLARE_WND_CLASS_EX(NULL, CS_DBLCLKS, NULL)

   typedef CAutoFloatWindowImpl< T , TBase, TWinTraits > thisClass;

#ifndef AW_SLIDE
   // AnimateWindow() constants defined in Platform SDK
   enum
   {
      AW_HOR_POSITIVE = 0x00000001,
      AW_HOR_NEGATIVE = 0x00000002,
      AW_VER_POSITIVE = 0x00000004,
      AW_VER_NEGATIVE = 0x00000008,
      AW_HIDE         = 0x00010000,
      AW_ACTIVATE     = 0x00020000,
      AW_SLIDE        = 0x00040000,
   };
#endif // AW_SLIDE

   BEGIN_MSG_MAP(CAutoFloatWindowImpl)
      MESSAGE_HANDLER(WM_CREATE, OnCreate)
      MESSAGE_HANDLER(WM_NCPAINT, OnNcPaint)
      MESSAGE_HANDLER(WM_PRINT, OnPrint)
      MESSAGE_HANDLER(WM_NCHITTEST, OnHitTest)
      MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
      MESSAGE_HANDLER(WM_NCACTIVATE, OnNcActivate)
      MESSAGE_HANDLER(WM_SIZE, OnSize)
      MESSAGE_HANDLER(WM_SYSCOMMAND, OnSysCommand)
      MESSAGE_HANDLER(WM_SETTINGCHANGE, OnSettingChange)
      MESSAGE_HANDLER(WM_AUTOHIDE_SETPANE, OnSetPane);
   END_MSG_MAP()

   CAutoFloatWindowImpl() : 
      m_hwndOwner(NULL)
   {
      ::ZeroMemory(&m_pane, sizeof(m_pane));
   }
   
   HWND m_hwndOwner;
   AUTOPANE m_pane;
   SIZE m_sizeBorder;

   // Operations

   BOOL _AnimateWindow(DWORD dwTime, DWORD dwFlags)
   {
      if( !AtlIsOldWindows() ) {
         typedef BOOL (CALLBACK* LPFNANIMATEWINDOW)(HWND,DWORD,DWORD);
         LPFNANIMATEWINDOW lpfnAnimateWindow = (LPFNANIMATEWINDOW)
            ::GetProcAddress(::GetModuleHandle(_T("user32.dll")), "AnimateWindow");
         if( lpfnAnimateWindow != NULL ) return lpfnAnimateWindow( m_hWnd, dwTime, dwFlags );
      }
      // For incompatible AnimateWindow() Windows versions
      if( dwFlags & AW_ACTIVATE ) {
         SetWindowPos(HWND_TOP, 0,0,0,0, SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
      }
      if( dwFlags & AW_HIDE ) {
         SetWindowPos(HWND_TOP, 0,0,0,0, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
      }
      return FALSE;
   }

   void _DrawFrame(HDC hDC) const
   {
      // Repaint frame borders (except the border on the right)
      CDCHandle dc = hDC;
      HBRUSH hBrushBorder = ::GetSysColorBrush(COLOR_ACTIVEBORDER);
      HBRUSH hBrushGrey = ::GetSysColorBrush(COLOR_BTNFACE);
      RECT rcWin;
      GetWindowRect(&rcWin);
      RECT rcLeft = { 0, m_sizeBorder.cy, m_sizeBorder.cx, (rcWin.bottom - rcWin.top) - m_sizeBorder.cy };
      dc.FillRect(&rcLeft, m_pane.iDirection == AUTOHIDE_LEFT ? hBrushGrey : hBrushBorder);
      RECT rcBottom = { 0, (rcWin.bottom - rcWin.top) - m_sizeBorder.cy, (rcWin.right - rcWin.left) - m_sizeBorder.cx, (rcWin.bottom - rcWin.top) };
      dc.FillRect(&rcBottom, m_pane.iDirection == AUTOHIDE_LEFT ? hBrushBorder : hBrushGrey);
      if( m_pane.iDirection == AUTOHIDE_LEFT ) {
         RECT rcTop = { 0, 0, (rcWin.right - rcWin.left) - m_sizeBorder.cx, m_sizeBorder.cy };
         dc.FillRect(&rcTop, hBrushBorder);
      }
      else {
         RECT rcRight = { (rcWin.right - rcWin.left) - m_sizeBorder.cx, 0, (rcWin.right - rcWin.left), (rcWin.bottom - rcWin.top) };
         dc.FillRect(&rcRight, hBrushBorder);
      }
   }

   // Message handlers

   LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      SendMessage(WM_SETTINGCHANGE);
      return 0;
   }
   LRESULT OnSettingChange(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      m_sizeBorder.cx = ::GetSystemMetrics(SM_CYSIZEFRAME);
      m_sizeBorder.cy = ::GetSystemMetrics(SM_CYSIZEFRAME);
      return 0;
   }
   LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      return 1; // handled, no background painting needed
   }
   LRESULT OnNcPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      LRESULT lRes = DefWindowProc();
      CWindowDC dc(m_hWnd);
      _DrawFrame(dc);
      return lRes;
   }
   LRESULT OnNcActivate(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
   {
      // When it becomes inactive, close the view
      if( (BOOL) wParam == FALSE ) {
         DWORD dwFlags = m_pane.iDirection == AUTOHIDE_LEFT ? AW_HOR_NEGATIVE : AW_VER_POSITIVE;
         _AnimateWindow(AUTOHIDE_DELAY_OUT, AW_SLIDE|dwFlags|AW_HIDE);
         RECT rc;
         GetWindowRect(&rc);
         WPARAM iSize = m_pane.iDirection == AUTOHIDE_LEFT ? rc.right - rc.left : rc.bottom - rc.top;
         ::SendMessage(m_hwndOwner, WM_AUTOHIDE_VIEWCLOSE, iSize, 0L);
      }
      bHandled = FALSE;
      return 0;
   }
   LRESULT OnHitTest(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      LRESULT lRes = DefWindowProc();
      switch( lRes ) {
      case HTBOTTOM:
      case HTBOTTOMLEFT:
      case HTLEFT:
      case HTCAPTION:
      case HTTOPRIGHT:
         lRes = HTCLIENT;
         break;
      case HTTOP:
      case HTTOPLEFT:
         lRes = m_pane.iDirection == AUTOHIDE_BOTTOM ? HTTOP : HTCLIENT;
         break;
      case HTRIGHT:
      case HTBOTTOMRIGHT:
         lRes = m_pane.iDirection == AUTOHIDE_LEFT ? HTRIGHT : HTCLIENT;
         break;
      }
      return lRes;
   }
   LRESULT OnSysCommand(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
   {
      switch( wParam & 0xFFF0 ) {
      case SC_CLOSE:
         ::SetFocus(m_hwndOwner); // Kill focus
         return 0;
      }
      bHandled = FALSE;
      return 0;
   }
   LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      if( !::IsWindow(m_pane.hWnd) ) return 0;
      RECT rc;
      GetClientRect(&rc);
      ::SetWindowPos(m_pane.hWnd, HWND_TOP, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER|SWP_SHOWWINDOW);
      return 0;
   }
   LRESULT OnPrint(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
   {
      LRESULT lRes = DefWindowProc();
      if( lParam & PRF_NONCLIENT ) _DrawFrame( (HDC) wParam );
      return lRes;
   }
   LRESULT OnSetPane(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
   {
      ATLASSERT(::IsWindow((HWND)wParam));
      ATLASSERT(lParam!=0);
      if( m_pane.hWnd != NULL ) ::ShowWindow(m_pane.hWnd, SW_HIDE);
      m_hwndOwner = (HWND) wParam;
      m_pane = *(reinterpret_cast<AUTOPANE *>(lParam));
      // Re-parent
      ::SetParent(m_pane.hWnd, m_hWnd);
      // Set title
      SetWindowText(m_pane.szTitle);
      // Place view inside pane
      // NOTE: OnSize() also calls ShowWindow() to restore an invisible child
      BOOL bDummy;
      OnSize(WM_SIZE, 0,0, bDummy);
      UpdateWindow();
      // Entr

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -