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

📄 appbar.pas

📁 与系统的桌面工具栏同样的功能 。 developed in delphi
💻 PAS
📖 第 1 页 / 共 4 页
字号:
{*****************************************************************************}
{                                                                             }
{ TAppBar Class v1.4                                                          }
{ implements Application Desktop Toolbar                                      }
{ (based on J.Richter's CAppBar MFC Class)                                    }
{                                                                             }
{ Copyright (c) 1997/98 Paolo Giacomuzzi                                      }
{ e-mail: paolo.giacomuzzi@usa.net                                            }
{ http://www.geocities.com/SiliconValley/9486                                 }
{                                                                             }
{*****************************************************************************}


unit AppBar;


interface


uses
  Windows, Messages, SysUtils, Classes, Forms, Dialogs, Controls, ExtCtrls,
  ShellApi, Registry;


const
  // AppBar's user notification message
  WM_APPBARNOTIFY = WM_USER + 100;

  // Timer interval
  SLIDE_DEF_TIMER_INTERVAL = 400;  // milliseconds

  // Defaults
  AB_DEF_SIZE_INC  = 1;
  AB_DEF_DOCK_SIZE = 32;
  AB_DEF_ROOT_KEY  = HKEY_CURRENT_USER;
  AB_DEF_KEY_NAME  = 'Software\AppBar\1.4\Delphi';


type
  // You can send to the Windows shell one of the following messages:
  // Message             Description
  // --------------      --------------------------------------------------
  // ABM_NEW             Register a new AppBar to the system
  // ABM_REMOVE          Remove a previously created AppBar from the system
  // ABM_QUERYPOS        Query the AppBar position
  // ABM_SETPOS          Set the AppBar position
  // ABM_GETSTATE        Get the edge the Appbar is docked to
  // ABM_GETTASKBARPOS   Get the Explorer Taskbar position
  // ABM_ACTIVATE        Activate the AppBar
  // ABM_GETAUTOHIDEBAR  Query if AppBar has Auto-hide behavior
  // ABM_SETAUTOHIDEBAR  Set the AppBar's Auto-hide behavior

  // The ABM_message constants are defined in SHELLAPI.PAS as follows:
  // ABM_NEW              = $00000000;
  // ABM_REMOVE           = $00000001;
  // ABM_QUERYPOS         = $00000002;
  // ABM_SETPOS           = $00000003;
  // ABM_GETSTATE         = $00000004;
  // ABM_GETTASKBARPOS    = $00000005;
  // ABM_ACTIVATE         = $00000006;
  // ABM_GETAUTOHIDEBAR   = $00000007;
  // ABM_SETAUTOHIDEBAR   = $00000008;
  // ABM_WINDOWPOSCHANGED = $00000009;

  // The following enumerated type defines the constants in the table
  TAppBarMessage = (abmNew, abmRemove, abmQueryPos, abmSetPos, abmGetState,
                    abmGetTaskBarPos, abmActivate, abmGetAutoHideBar,
                    abmSetAutoHideBar, abmWindowPosChanged);

  // An AppBar can be in one of 6 states shown in the table below:
  // State          Description
  // -----------    -----------------------------------------------------
  // ABE_UNKNOWN    The Appbar is in an unknown state
  //                (usually during construction/destruction)
  // ABE_FLOAT      The AppBar is floating on the screen
  // ABE_LEFT       The Appbar is docked on the left   edge of the screen
  // ABE_TOP        The Appbar is docked on the top    edge of the screen
  // ABE_RIGHT      The Appbar is docked on the right  edge of the screen
  // ABE_BOTTOM     The Appbar is docked on the bottom edge of the screen

  // The ABE_edge state constants are defined in SHELLAPI.PAS as follows:
  // ABE_LEFT    = 0;
  // ABE_TOP     = 1;
  // ABE_RIGHT   = 2;
  // ABE_BOTTOM  = 3;

  // The ABE_UNKNOWN and ABE_FLOAT constants are defined here as follows:
  // ABE_UNKNOWN = 4;
  // ABE_FLOAT   = 5;

  // The following enumerated type defines the constants in the table
  // (Values are mutually exclusive)
  TAppBarEdge = (abeLeft, abeTop, abeRight, abeBottom, abeUnknown, abeFloat);

  // An AppBar can have several behavior flags as shown below:
  // Flag                        Description
  // --------------------------- -----------------------------------
  // ABF_ALLOWLEFT               Allow dock on left   of screen
  // ABF_ALLOWRIGHT              Allow dock on right  of screen
  // ABF_ALLOWTOP                Allow dock on top    of screen
  // ABF_ALLOWBOTTOM             Allow dock on bottom of screen
  // ABF_ALLOWFLOAT              Allow float in the middle of screen

  // The following enumerated type defines the constants in the table
  TAppBarFlag = (abfAllowLeft, abfAllowTop, abfAllowRight, abfAllowBottom,
                 abfAllowFloat);
  TAppBarFlags = set of TAppBarFlag;

  // The following enumerated type defines the AppBar behavior in the Taskbar
  TAppBarTaskEntry = (abtShow, abtHide, abtFloatDependent);

  // The record below contains all of the AppBar settings that
  // can be saved/loaded in/from the Registry
  TAppBarSettings = record
    cbSize         : DWORD;        // Size of this structure
    abEdge         : TAppBarEdge;  // ABE_UNKNOWN, ABE_FLOAT, or ABE_edge
    abFlags        : TAppBarFlags; // ABF_* flags
    bAutohide      : Boolean;      // Should AppBar be auto-hidden when docked?
    bAlwaysOnTop   : Boolean;      // Should AppBar always be on top?
    bSlideEffect   : Boolean;      // Should AppBar slide?
    nTimerInterval : Integer;      // Slide Timer Interval (determines speed)
    szSizeInc      : TSize;        // Discrete width/height size increments
    szDockSize     : TSize;        // Width/Height for docked bar
    rcFloat        : TRect;        // Floating rectangle in screen coordinates
    nMinWidth      : Integer;      // Min allowed width
    nMinHeight     : Integer;      // Min allowed height
    nMaxWidth      : Integer;      // Max allowed width
    nMaxHeight     : Integer;      // Max allowed height
    szMinDockSize  : TSize;        // Min Width/Height when docked
    szMaxDockSize  : TSize;        // Max Width/Height when docked
    abTaskEntry    : TAppBarTaskEntry; // AppBar behavior in the Taskbar
  end;

  // The record below contains the settings location in the registry
  TAppBarSettingsLocation = record
    nRootKey : Integer;  // HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE
    KeyName  : String;   // Key Name starting from root
  end;

  // TAppBar class ////////////////////////////////////////////////////////////
  TAppBar = class(TForm)

  private

  { Internal implementation state variables }

    // This AppBar's settings info
    FABS : TAppBarSettings;

    // We need a member variable which tracks the proposed edge of the
    // AppBar while the user is moving it, deciding where to position it.
    // While not moving, this member must contain ABE_UNKNOWN so that
    // GetEdge returns the current edge contained in FABS.abEdge.
    // While moving the AppBar, FabEdgeProposedPrev contains the
    // proposed edge based on the position of the AppBar.  The proposed
    // edge becomes the new edge when the user stops moving the AppBar.
    FabEdgeProposedPrev : TAppBarEdge;

    // We need a member variable which tracks whether a full screen
    // application window is open
    FbFullScreenAppOpen : Boolean;

    // We need a member variable which tracks whether our autohide window
    // is visible or not
    FbAutoHideIsVisible : Boolean;

    // We need a timer to to determine when the AppBar should be re-hidden
    FTimer : TTimer;

    // We need a member variable to store the settings location in the registry
    FabSettingsLocation : TAppBarSettingsLocation;

  { Internal implementation functions }

    // Modifies window creation flags
    procedure CreateParams (var Params: TCreateParams); override;

    // These functions encapsulate the shell's SHAppBarMessage function
    function AppBarMessage (abMessage : TAppBarMessage;
                            abEdge    : TAppBarEdge;
                            lParam    : LPARAM;
                            bRect     : Boolean;
                            var rc    : TRect) : UINT;

    function AppBarMessage1 (abMessage : TAppBarMessage) : UINT;

    function AppBarMessage2 (abMessage : TAppBarMessage;
                             abEdge    : TAppBarEdge) : UINT;

    function AppBarMessage3 (abMessage : TAppBarMessage;
                             abEdge    : TAppBarEdge;
                             lParam    : LPARAM) : UINT;

    function AppBarMessage4 (abMessage : TAppBarMessage;
                             abEdge    : TAppBarEdge;
                             lParam    : LPARAM;
                             var rc    : TRect) : UINT;

    // Gets a edge (ABE_FLOAT or ABE_edge) from a point (screen coordinates)
    function CalcProposedState (var pt : TSmallPoint) : TAppBarEdge;

    // Gets a retangle position (screen coordinates) from a proposed state
    procedure GetRect (abEdgeProposed : TAppBarEdge; var rcProposed : TRect);

    // Adjusts the AppBar's location to account for autohide
    // Returns TRUE if rectangle was adjusted
    function AdjustLocationForAutohide (bShow  : Boolean;
                                        var rc : TRect) : Boolean;

    // If AppBar is Autohide and docked, shows/hides the AppBar
    procedure ShowHiddenAppBar (bShow : Boolean);

    // When Autohide AppBar is shown/hidden, slides in/out of view
    procedure SlideWindow (var rcEnd : TRect);

    // Returns which edge we're autohidden on or ABE_UNKNOWN
    function GetAutohideEdge : TAppBarEdge;

    // Returns a TSmallPoint that gives the cursor position in screen coords
    function GetMessagePosition : TSmallPoint;

    // Changes the style of a window (translated from AfxModifyStyle)
    function ModifyStyle (hWnd         : THandle;
                          nStyleOffset : Integer;
                          dwRemove     : DWORD;
                          dwAdd        : DWORD;
                          nFlags       : UINT) : Boolean;

  protected

  { Property selector functions }

    // Retrieves the AppBar's edge.  If the AppBar is being positioned, its
    // proposed state is returned instead
    function GetEdge : TAppBarEdge;

    // Changes the AppBar's edge to ABE_UNKNOWN, ABE_FLOAT or an ABE_edge
    procedure SetEdge (abEdge : TAppBarEdge);

    // Changes the slide time interval
    procedure SetSlideTime (nInterval : Integer);

  { Overridable functions }

    // Called when the AppBar's proposed state changes
    procedure OnAppBarStateChange (bProposed      : Boolean;
                                   abEdgeProposed : TAppBarEdge); virtual;

    // Called if user attempts to dock an Autohide AppBar on
    // an edge that already contains an Autohide AppBar
    procedure OnAppBarForcedToDocked; virtual;

    // Called when AppBar gets an ABN_FULLSCREENAPP notification
    procedure OnABNFullScreenApp (bOpen : Boolean); virtual;

    // Called when AppBar gets an ABN_POSCHANGED notification
    procedure OnABNPosChanged; virtual;

    // Called when AppBar gets an ABN_WINDOWARRANGE notification
    procedure OnABNWindowArrange (bBeginning : Boolean); virtual;

  { Message handlers }

    // Called when the AppBar receives a WM_APPBARNOTIFY window message
    procedure OnAppBarCallbackMsg(var Msg : TMessage); message WM_APPBARNOTIFY;

    // Called when the AppBar form is first created
    procedure OnCreate (var Msg: TWMCreate); message WM_CREATE;

    // Called when the AppBar form is about to be destroyed
    procedure OnDestroy (var Msg : TWMDestroy); message WM_DESTROY;

    // Called when the AppBar receives a WM_WINDOWPOSCHANGED message
    procedure OnWindowPosChanged (var Msg : TWMWindowPosChanged);
                                                   message WM_WINDOWPOSCHANGED;

    // Called when the AppBar receives a WM_ACTIVATE message
    procedure OnActivate (var Msg : TWMActivate); message WM_ACTIVATE;

    // Called every timer tick
    procedure OnAppBarTimer (Sender : TObject);

    // Called when the AppBar receives a WM_NCMOUSEMOVE message
    procedure OnNcMouseMove (var Msg : TWMNCMouseMove); message WM_NCMOUSEMOVE;

    // Called when the AppBar receives a WM_NCHITTEST message
    procedure OnNcHitTest (var Msg: TWMNCHitTest); message WM_NCHITTEST;

    // Called when the AppBar receives a WM_ENTERSIZEMOVE message
    procedure OnEnterSizeMove (var Msg : TMessage); message WM_ENTERSIZEMOVE;

    // Called when the AppBar receives a WM_EXITSIZEMOVE message
    procedure OnExitSizeMove (var Msg : TMessage); message WM_EXITSIZEMOVE;

    // Called when the AppBar receives a WM_MOVING message
    procedure OnMoving (var Msg : TMessage); message WM_MOVING;

    // Called when the AppBar receives a WM_SIZING message
    procedure OnSizing (var Msg : TMessage); message WM_SIZING;

    // Called when the AppBar receives a WM_GETMINMAXINFO message
    procedure OnGetMinMaxInfo (var Msg : TWMGetMinMaxInfo);
                                                      message WM_GETMINMAXINFO;

  { AppBar-specific helper functions }

    // Returns TRUE if abEdge is ABE_LEFT or ABE_RIGHT, else FALSE is returned
    function IsEdgeLeftOrRight (abEdge : TAppBarEdge) : Boolean;

    // Returns TRUE if abEdge is ABE_TOP or ABE_BOTTOM, else FALSE is returned
    function IsEdgeTopOrBottom (abEdge : TAppBarEdge) : Boolean;

    // Returns TRUE if abEdge is ABE_FLOAT, else FALSE is returned
    function IsFloating (abEdge : TAppBarEdge) : Boolean;

    // Returns TRUE if abFlags contain an at least allowed edge to dock on
    function IsDockable (abFlags : TAppBarFlags) : Boolean;

    // Returns TRUE if abFlags contain abfAllowLeft and abfAllowRight
    function IsDockableVertically (abFlags : TAppBarFlags) : Boolean;

    // Returns TRUE if abFlags contain abfAllowTop and abfAllowBottom
    function IsDockableHorizontally (abFlags : TAppBarFlags) : Boolean;

    // Forces the shell to update its AppBar list and the workspace area
    procedure ResetSystemKnowledge;

    // Returns a proposed edge or ABE_FLOAT based on ABF_* flags and a
    // point specified in screen coordinates
    function GetEdgeFromPoint (abFlags : TAppBarFlags;
                               pt      : TSmallPoint) : TAppBarEdge;

  public

  { Public member functions }

    // Constructs an AppBar
    constructor Create (Owner : TComponent); override;

    // Destroys a previously created AppBar
    destructor Destroy; override;

    // Forces the AppBar's visual appearance to match its internal state
    procedure UpdateBar; virtual;

    // Loads settings from the registry at RootKey and KeyName location.
    // Returns TRUE if the settings are available, else FALSE
    function LoadSettings : Boolean; virtual;

    // Saves settings into the registry at RootKey and KeyName location.
    // Returns TRUE if succeeded, else FALSE
    function SaveSettings : Boolean; virtual;

  published

  { Properties }

    // Allowed dockable edges
    property Flags : TAppBarFlags read FABS.abFlags write FABS.abFlags;

    // Horizontal size increment
    property HorzSizeInc : Integer read  FABS.szSizeInc.cx
                                   write FABS.szSizeInc.cx;
    // Vertical size increment
    property VertSizeInc : Integer read  FABS.szSizeInc.cy
                                   write FABS.szSizeInc.cy;
    // Edge to dock on
    property Edge : TAppBarEdge read GetEdge write SetEdge;

    // Auto-hide On/Off
    property AutoHide : Boolean read FABS.bAutohide write FABS.bAutohide;

    // Always On Top On/Off
    property AlwaysOnTop : Boolean read  FABS.bAlwaysOnTop
                                   write FABS.bAlwaysOnTop;
    // Slide Effect On/Off
    property SlideEffect : Boolean read  FABS.bSlideEffect
                                   write FABS.bSlideEffect;
    // Slide Time
    property SlideTime : Integer read FABS.nTimerInterval write SetSlideTime;

    // Horizontal size when docked on left or right
    property HorzDockSize : Integer read  FABS.szDockSize.cy
                                    write FABS.szDockSize.cy;

    // Vertical size when docked on top or bottom
    property VertDockSize : Integer read  FABS.szDockSize.cx
                                    write FABS.szDockSize.cx;

    // AppBar coordinates when floating
    property FloatLeft   : Integer read  FABS.rcFloat.Left
                                   write FABS.rcFloat.Left;
    property FloatTop    : Integer read  FABS.rcFloat.Top
                                   write FABS.rcFloat.Top;
    property FloatRight  : Integer read  FABS.rcFloat.Right
                                   write FABS.rcFloat.Right;
    property FloatBottom : Integer read  FABS.rcFloat.Bottom
                                   write FABS.rcFloat.Bottom;

    // AppBar MinMax dimensions when floating
    property MinWidth  : Integer read FABS.nMinWidth  write FABS.nMinWidth;
    property MinHeight : Integer read FABS.nMinHeight write FABS.nMinHeight;
    property MaxWidth  : Integer read FABS.nMaxWidth  write FABS.nMaxWidth;
    property MaxHeight : Integer read FABS.nMaxHeight write FABS.nMaxHeight;

    // Min Height when docked horizontally
    property MinHorzDockSize : Integer read  FABS.szMinDockSize.cy
                                       write FABS.szMinDockSize.cy;

    // Min Width when docked vertically
    property MinVertDockSize : Integer read  FABS.szMinDockSize.cx
                                       write FABS.szMinDockSize.cx;

    // Max Height when docked horizontally
    property MaxHorzDockSize : Integer read  FABS.szMaxDockSize.cy
                                       write FABS.szMaxDockSize.cy;

    // Max Width when docked vertically
    property MaxVertDockSize : Integer read  FABS.szMaxDockSize.cx
                                       write FABS.szMaxDockSize.cx;

    // AppBar behavior in the Window Taskbar
    property TaskEntry : TAppBarTaskEntry read  FABS.abTaskEntry
                                          write FABS.abTaskEntry;

    // RootKey in the registry where settings should be loaded/saved
    property RootKey : Integer read  FabSettingsLocation.nRootKey
                               write FabSettingsLocation.nRootKey;

⌨️ 快捷键说明

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