home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 June / Vpr9806a.iso / OLS / Windows / ntaskbar / ntaskbar.lzh / NTASKAPI / NTASKAPI.PAS < prev   
Pascal/Delphi Source File  |  1996-09-02  |  17KB  |  487 lines

  1. unit Ntaskapi;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Menus;
  8.  
  9. {***************************************************
  10.     NICOLE TaskBar Addon Programming Interface (NTASK-API)
  11.  
  12.     TNicoleAddonコンポーネント ver1.0
  13.  
  14.  
  15.     このソースはライセンスフリーで無保証です。
  16.     一応、Delphi2.0でも1.0でもコンパイルできます
  17.     自由に改造したり、それを配布したりするのは自由です。
  18.     NICOLE TaskBarのアドオンについてもフリーです
  19.     NICOLE TaskBarのアドオンとして作成されたプログラムを
  20.     シェアウェアにするのも、配布するのも自由です。
  21.     私(NICOLE)への連絡や許可を取る必要はありません
  22.  
  23.     但し、私(NICOLE)は、このコードを利用した事による
  24.     いかなる責任も持ちませんので、ご了承下さい
  25.  
  26.     コードに無駄な部分があったりしますが
  27.     私はプロのプログラマではないので・・・
  28.  
  29.     TAB = 2 で見て下さい
  30.  
  31.  ***************************************************}
  32. const
  33.     NTASKBAR_MSG                        =    WM_USER + $200;
  34.  
  35.     NTASKBAR_VERSION                =    NTASKBAR_MSG + 1;
  36.     NTASKBAR_DIRECTORY            =    NTASKBAR_MSG + 2;
  37.     NTASKBAR_EXENAME                =    NTASKBAR_MSG + 3;
  38.     NTASKBAR_REGIST                    =    NTASKBAR_MSG + 4;
  39.     NTASKBAR_QUIT                        = NTASKBAR_MSG + 5;
  40.     NTASKBAR_SETFILER                = NTASKBAR_MSG + 6;
  41.     NTASKBAR_GETFILER                = NTASKBAR_MSG + 7;
  42.     NTASKBAR_BLINK                    = NTASKBAR_MSG + 8;
  43.  
  44.     NTASKBAR_ADDON_INSTALL    = NTASKBAR_MSG + 40;
  45.     NTASKBAR_ADDON_UNINSTALL= NTASKBAR_MSG + 41;
  46.     NTASKBAR_ADDON_TITLE        = NTASKBAR_MSG + 42;
  47.     NTASKBAR_ADDON_COPYRIGHT= NTASKBAR_MSG + 43;
  48.  
  49.     NTASKBAR_BTN_CREATE            =    NTASKBAR_MSG + 50;
  50.     NTASKBAR_BTN_REMOVE            =    NTASKBAR_MSG + 51;
  51.     NTASKBAR_BTN_CLICK            =    NTASKBAR_MSG + 52;
  52.     NTASKBAR_BTN_RCLICK            =    NTASKBAR_MSG + 53;
  53.     NTASKBAR_BTN_DROPFILE        =    NTASKBAR_MSG + 54;
  54.     NTASKBAR_BTN_SETICON        =    NTASKBAR_MSG + 55;
  55.     NTASKBAR_BTN_GETICON        =    NTASKBAR_MSG + 56;
  56.     NTASKBAR_BTN_SETHINT        =    NTASKBAR_MSG + 57;
  57.     NTASKBAR_BTN_GETHINT        =    NTASKBAR_MSG + 58;
  58.     NTASKBAR_BTN_DROPACCEPT    =    NTASKBAR_MSG + 59;
  59.     NTASKBAR_BTN_SETHEIGHT    =    NTASKBAR_MSG + 60;
  60.     NTASKBAR_BTN_GETHEIGHT    =    NTASKBAR_MSG + 61;
  61.     NTASKBAR_BTN_PAINT            =    NTASKBAR_MSG + 62;
  62.     NTASKBAR_BTN_DRAGACCEPT    = NTASKBAR_MSG + 63;
  63.     NTASKBAR_BTN_SETVISIBLE    = NTASKBAR_MSG + 64;
  64.   NTASKBAR_BTN_GETVISIBLE    = NTASKBAR_MSG + 65;
  65.     NTASKBAR_BTN_MOUSEENTER    = NTASKBAR_MSG + 66;
  66.     NTASKBAR_BTN_MOUSELEAVE    = NTASKBAR_MSG + 67;
  67.  
  68. {***************************************************
  69.  ***************************************************}
  70. type
  71. {$IFNDEF LPARAM}
  72.     LPARAM    = Longint;
  73.     WPARAM    = WORD;
  74.     UINT        = Longint;
  75.     LRESULT    = Longint;
  76. {$ENDIF}
  77.  
  78.     TNicoleDropFileEvent = procedure (Sender:TObject; FileName:string) of object;
  79.   TNicolePaintEvent = procedure (Sender:TObject; Canvas:TCanvas; Pushed:Boolean) of object;
  80.  
  81.   TNicoleAddon = class(TComponent)
  82.   private
  83.     { Private 宣言 }
  84.         FHint                : string;
  85.         FHandle            : HWND;
  86.         FIcon                : TIcon;
  87.         FHeight            : integer;
  88.         FDragAccept : Boolean;
  89.         FPopupMenu    : TPopupMenu;
  90.         FVisible        : Boolean;
  91.         FCopyright    : String;
  92.  
  93.         FOnClick        : TNotifyEvent;
  94.         FOnDropFile    : TNicoleDropFileEvent;
  95.         FOnPaint        : TNicolePaintEvent;
  96.         FOnMouseEnter : TNotifyEvent;
  97.         FOnMouseLeave : TNotifyEvent;
  98.  
  99.         function        FGetNicoleTaskbar : HWND;
  100.         procedure        FSetHint(theHint:string);
  101.         procedure        FSetIcon(theIcon:TIcon);
  102.         procedure        FSetHeight(theHeight:integer);
  103.     procedure        FSetDragAccept(theDragAccept:Boolean);
  104.         procedure        FSetVisible(theVisible:Boolean);
  105.         function        FGetVersion : String;
  106.         function        FGetDirectory    : String;
  107.         function        FGetExeName : String;
  108.         function        FGetRegist : Boolean;
  109.         procedure        FSetFiler(theFiler:String);
  110.         function        FGetFiler : String;
  111.         procedure        FSetCopyright(theCopyright:string);
  112.  
  113.   protected
  114.     { Protected 宣言 }
  115.         function        GetAtom(theAtom:TATOM) : String; virtual;
  116.         function        GetString(msg:Longint) : String; virtual;
  117.         function        SendTaskBar(msg:UINT; wp:WPARAM; lp:LPARAM): LRESULT; virtual;
  118.         procedure        PostTaskBar(msg:UINT; wp:WPARAM; lp:LPARAM); virtual;
  119.         procedure        Loaded; override;
  120.   public
  121.     { Public 宣言 }
  122.         constructor    Create(theOwner:TComponent); override;
  123.         destructor    Destroy; override;
  124.  
  125.         procedure        MessageHandler(var Msg: TMsg; var Handled: Boolean); virtual;
  126.  
  127.         procedure        Click; virtual;
  128.         procedure        DropFile(filename:string); virtual;
  129.         procedure        Paint(Pushed:Boolean); virtual;
  130.         procedure        Blink(theBlink:Boolean; theIcon : TIcon); virtual;
  131.         function        Install : integer;
  132.         function        Uninstall : integer;
  133.  
  134.         property        Handle : HWND read FHandle;
  135.         property        NicoleTaskBar : HWND read FGetNicoleTaskBar;
  136.         property        Version : String read FGetVersion;
  137.         property        Directory    : String read FGetDirectory;
  138.         property        ExeName    : String read FGetExeName;
  139.         property        Regist    : Boolean read FGetRegist;
  140.         property        Filer : String read FGetFiler write FSetFiler;
  141.   published
  142.     { Published 宣言 }
  143.         property        Hint : string read FHint write FSetHint;
  144.         property        Icon : TIcon read FIcon write FSetIcon;
  145.         property        Height : integer read FHeight write FSetHeight;
  146.     property        DragAccept : Boolean read FDragAccept write FSetDragAccept;
  147.     property        PopupMenu : TPopupMenu read FPopupMenu write FPopupMenu;
  148.       property        Visible : Boolean read FVisible write FSetVisible;
  149.         property        Copyright : String read FCopyright write FSetCopyright;
  150.  
  151.         property        OnClick : TNotifyEvent read FOnClick write FOnClick;
  152.         property        OnDropFile : TNicoleDropFileEvent read FOnDropFile write FOnDropFile;
  153.         property        OnPaint     : TNicolePaintEvent read FOnPaint write FOnPaint;
  154.         property        OnMouseEnter : TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
  155.         property        OnMouseLeave : TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  156.   end;
  157.  
  158. {***************************************************
  159.  ***************************************************}
  160. procedure Register;
  161.  
  162. implementation
  163.  
  164. procedure Register;
  165. begin
  166.   RegisterComponents('Nicole', [TNicoleAddon]);
  167. end;
  168. {***************************************************
  169.     いっけん意味のないコードに見えますが
  170.     実は重要な意味があるので、省略しないでください
  171.  ***************************************************}
  172. procedure    TNicoleAddon.Loaded;
  173. begin
  174.     inherited Loaded;
  175.  
  176.     Copyright := Copyright;
  177.     Icon := Icon;
  178. end;
  179. {***************************************************
  180.     インストールする
  181.  ***************************************************}
  182. function    TNicoleAddon.Install : integer;
  183. var
  184.     szTitle, szPath : array[0..256] of char;
  185. begin
  186.     StrPCopy(szTitle, Application.Title);
  187.     StrPCopy(szPath, Application.ExeName);
  188.     Result := SendTaskBar(NTASKBAR_ADDON_INSTALL, GlobalAddAtom(szTitle), GlobalAddAtom(szPath));
  189. end;
  190. {***************************************************
  191.     アンインストールする
  192.  ***************************************************}
  193. function    TNicoleAddon.Uninstall : integer;
  194. var
  195.     szTitle : array[0..256] of char;
  196. begin
  197.     StrPCopy(szTitle, Application.Title);
  198.     Result := SendTaskBar(NTASKBAR_ADDON_UNINSTALL, GlobalAddAtom(szTitle), 0);
  199. end;
  200. {***************************************************
  201.     アトムから文字列をStrig型として取り出し、アトムを削除する
  202.  ***************************************************}
  203. function    TNicoleAddon.GetAtom(theAtom:TAtom) : string;
  204. var
  205.     z : array[0..256] of char;
  206. begin
  207.     GlobalGetAtomName(theAtom, z, 255);
  208.     GlobalDeleteAtom(theAtom);
  209.     Result := StrPas(z);
  210. end;
  211. {***************************************************
  212.     NICOLE TaskBarからのメッセージを処理する
  213.     通常は Application.OnMessage にセットして使うのが簡単です
  214.  ***************************************************}
  215. procedure        TNicoleAddon.MessageHandler(var Msg: TMsg; var Handled: Boolean);
  216. var
  217.     pt : TPoint;
  218. begin
  219.     case msg.message of
  220.         NTASKBAR_BTN_CLICK:
  221.             begin
  222.                 if Handle = msg.wParam then Click;
  223.             end;
  224.         NTASKBAR_BTN_RCLICK:
  225.             if (Handle = msg.wParam) and (nil <> PopupMenu) then begin
  226.                 Application.MainForm.SetFocus;
  227.                 GetCursorPos(pt);
  228.                 PopupMenu.Popup(pt.x, pt.y);
  229.             end;
  230.         NTASKBAR_BTN_DROPFILE:
  231.             begin
  232.                 if Handle = msg.wParam then DropFile(GetAtom(msg.lParam));
  233.             end;
  234.         NTASKBAR_BTN_PAINT:
  235.             begin
  236.                 if Handle = msg.wParam then Paint(Boolean(msg.lParam));
  237.             end;
  238.         NTASKBAR_QUIT:
  239.             begin
  240.                 Application.Terminate;
  241.             end;
  242.         NTASKBAR_BTN_MOUSEENTER:
  243.             begin
  244.                 if (Handle = msg.wParam) and Assigned(FOnMouseEnter) then FOnMouseEnter(Self);
  245.             end;
  246.         NTASKBAR_BTN_MOUSELEAVE:
  247.             begin
  248.                 if (Handle = msg.wParam) and Assigned(FOnMouseLeave) then FOnMouseLeave(Self);
  249.             end;
  250.     end;
  251. end;
  252. {***************************************************
  253.     コンストラクタ
  254.  ***************************************************}
  255. constructor    TNicoleAddon.Create(theOwner:TComponent);
  256. var
  257.     szTitle, szPath : array[0..256] of char;
  258. begin
  259.     inherited Create(theOwner);
  260.     FIcon := TIcon.Create;
  261.      FHeight := 35;
  262.     FVisible := true;
  263.  
  264.     if (theOwner is TForm) and not (csDesigning in theOwner.ComponentState) then begin
  265.         if 0 <> NicoleTaskBar then begin
  266.             FHandle := SendTaskBar(NTASKBAR_BTN_CREATE, Application.Handle, 0);
  267.  
  268.             {Application.OnMessageに、自分のメッセージ処理メソッドを設定します
  269.             もし他の用途で Application.OnMessageを使っていたり
  270.             TNicoleAddonコンポーネントが複数ある場合には注意が必要です}
  271.             Application.OnMessage := MessageHandler;
  272.  
  273.              StrPCopy(szTitle, Application.Title);
  274.             StrPCopy(szPath, Application.ExeName);
  275.             PostTaskBar(NTASKBAR_ADDON_TITLE, GlobalAddAtom(szTitle), GlobalAddAtom(szPath));
  276.         end else begin
  277.             {NICOLE TaskBarが起動されていないので、終了する}
  278.             ShowMessage('NICOLE TaskBarが起動されていません');
  279.             Application.Terminate;
  280.         end;
  281.     end;
  282. end;
  283. {***************************************************
  284.     デストラクタ
  285.  ***************************************************}
  286. destructor    TNicoleAddon.Destroy;
  287. begin
  288.     {NICOLE TaskBarへボタンを削除するメッセージを送る}
  289.     PostTaskBar(NTASKBAR_BTN_REMOVE, Handle, 0);
  290.   Blink(false, nil);
  291.  
  292.     FIcon.Free;
  293.     Application.OnMessage := nil;
  294.     inherited Destroy;
  295. end;
  296. {***************************************************
  297.     OnClickにアタッチされたコードを呼び出す
  298.  ***************************************************}
  299. procedure    TNicoleAddon.Click;
  300. begin
  301.     if Assigned(FOnClick) then FOnClick(Self);
  302. end;
  303. {***************************************************
  304.     OnDropFileにアタッチされたコードを呼び出す
  305.  ***************************************************}
  306. procedure    TNicoleAddon.DropFile(filename:string);
  307. begin
  308.     if Assigned(FOnDropFile) then FOnDropFile(Self, filename);
  309. end;
  310. {***************************************************
  311.     OnPaintにアタッチされたコードを呼び出す
  312.  ***************************************************}
  313. procedure    TNicoleAddon.Paint(Pushed:Boolean);
  314. var
  315.     canvas : TCanvas;
  316. begin
  317.     {NICOLE TaskBarに何らかのエラーにより
  318.     アドオンのボタンが作成されていなければ何もしない}
  319.     if 0 = Handle then exit;
  320.  
  321.     if Assigned(FOnPaint) then begin
  322.         Canvas := TCanvas.Create;
  323.         Canvas.Handle := GetDC(Handle);
  324.         Canvas.Font := Application.MainForm.Font;
  325.         Canvas.Font.Color := clBtnText;
  326.         Canvas.Brush.Color := clBtnFace;
  327.         FOnPaint(Self, canvas, Pushed);
  328.         ReleaseDC(Handle, Canvas.Handle);
  329.         Canvas.Free;
  330.     end;
  331. end;
  332. {***************************************************
  333.     NicoleTaskBarプロパティの読み出し
  334.  ***************************************************}
  335. function    TNicoleAddon.FGetNicoleTaskBar : HWND;
  336. var
  337.     window : HWND;
  338. begin
  339.     window := FindWindow('TNICOLE_TaskBar', 'NICOLE TaskBar');
  340.     Result := window;
  341. end;
  342. {***************************************************
  343.     NICOLE TaskBar へアドオンのメッセージを送る
  344.  ***************************************************}
  345. function    TNicoleAddon.SendTaskBar(msg:UINT; wp:WPARAM; lp:LPARAM) : LRESULT;
  346. begin
  347.     if 0 = NicoleTaskBar then begin
  348.       Result := 0;
  349.         exit;
  350.     end;
  351.  
  352.     Result := SendMessage(NicoleTaskBar, msg, wp, lp);
  353. end;
  354.  
  355. procedure    TNicoleAddon.PostTaskBar(msg:UINT; wp:WPARAM; lp:LPARAM);
  356. begin
  357.     if 0 = NicoleTaskBar then exit;
  358.     PostMessage(NicoleTaskBar, msg, wp, lp);
  359. end;
  360. {***************************************************
  361.     Hint プロパティへの書き込み
  362.  ***************************************************}
  363. procedure    TNicoleAddon.FSetHint(theHint:string);
  364. var
  365.     z : array[0..256] of char;
  366. begin
  367.     {下のコードはPCharへのキャストを使えば簡単ですが、一応Delphi1.0でも
  368.     コンパイルできるようにわざと、このようにコーティングしています}
  369.  
  370.     FHint := theHint;
  371.     StrPCopy(z, theHint);
  372.     PostTaskBar(NTASKBAR_BTN_SETHINT, Handle, GlobalAddAtom(z));
  373. end;
  374. {***************************************************
  375.     Iconプロパティへの書き込み
  376.  ***************************************************}
  377. procedure    TNicoleAddon.FSetIcon(theIcon:TIcon);
  378. begin
  379.     FIcon.Assign(theIcon);
  380.     {実際にはアイコンハンドル(HICON)をメッセージで送る}
  381.     PostTaskBar(NTASKBAR_BTN_SETICON, Handle, Icon.Handle);
  382. end;
  383. {***************************************************
  384.     Heightプロパティへの書き込み
  385.  ***************************************************}
  386. procedure    TNicoleAddon.FSetHeight(theHeight:integer);
  387. begin
  388.     if theHeight < 1 then theHeight := 1;
  389.     FHeight := theHeight;
  390.     PostTaskBar(NTASKBAR_BTN_SETHEIGHT, Handle, Height);
  391. end;
  392. {***************************************************
  393.     DragAcceptプロパティへの書き込み
  394.  ***************************************************}
  395. procedure    TNicoleAddon.FSetDragAccept(theDragAccept:Boolean);
  396. begin
  397.     FDragAccept := theDragAccept;
  398.     PostTaskBar(NTASKBAR_BTN_DRAGACCEPT, Handle, LPARAM(DragAccept));
  399. end;
  400. {***************************************************
  401.     Visibleプロパティへの書き込み
  402.  ***************************************************}
  403. procedure    TNicoleAddon.FSetVisible(theVisible:Boolean);
  404. begin
  405.     FVisible := theVisible;
  406.     PostTaskBar(NTASKBAR_BTN_SETVISIBLE, Handle, LPARAM(theVisible));
  407. end;
  408. {***************************************************
  409.     Registプロパティの読込
  410.  ***************************************************}
  411. function    TNicoleAddon.FGetRegist : Boolean;
  412. begin
  413.     Result := Boolean(SendTaskBar(NTASKBAR_REGIST, 0, 0));
  414. end;
  415. {***************************************************************
  416.  *    文字列を返すメッセージを簡単に処理するルーチン
  417.  ***************************************************************}
  418. function    TNicoleAddon.GetString(msg:Longint) : String;
  419. var
  420.     theAtom : TATOM;
  421. begin
  422.     theAtom := SendTaskBar(msg, 0, 0);
  423.     Result := GetAtom(theAtom);
  424. end;
  425. {***************************************************************
  426.  *    Versionプロパティの読み出し
  427.  ***************************************************************}
  428. function    TNicoleAddon.FGetVersion : String;
  429. begin
  430.     Result := GetString(NTASKBAR_VERSION);
  431. end;
  432. {***************************************************************
  433.  *    ExeNameプロパティの読み出し
  434.  ***************************************************************}
  435. function    TNicoleAddon.FGetExeName : String;
  436. begin
  437.     Result := GetString(NTASKBAR_EXENAME);
  438. end;
  439. {***************************************************************
  440.  *    Directoryプロパティの読み出し
  441.  ***************************************************************}
  442. function    TNicoleAddon.FGetDirectory : String;
  443. begin
  444.     Result := GetString(NTASKBAR_DIRECTORY);
  445. end;
  446. {***************************************************************
  447.  *    Filerプロパティの読み出し
  448.  ***************************************************************}
  449. function    TNicoleAddon.FGetFiler : String;
  450. begin
  451.     Result := GetString(NTASKBAR_GETFILER);
  452. end;
  453. {***************************************************************
  454.  *    Filerプロパティへの書き込み
  455.  ***************************************************************}
  456. procedure    TNicoleAddon.FSetFiler(theFiler:String);
  457. var
  458.     z : array[0..256] of char;
  459. begin
  460.     StrPCopy(z, theFiler);
  461.     PostTaskBar(NTASKBAR_SETFILER, 0, GlobalAddAtom(z));
  462. end;
  463. {***************************************************************
  464.  *    Copyrightプロパティへの書き込み
  465.  ***************************************************************}
  466. procedure    TNicoleAddon.FSetCopyright(theCopyright:String);
  467. var
  468.     szTitle, szCopyright : array[0..256] of char;
  469. begin
  470.     FCopyright := theCopyright;
  471.     StrPCopy(szTitle, Application.Title);
  472.   StrPCopy(szCopyright, Copyright);
  473.     PostTaskBar(NTASKBAR_ADDON_COPYRIGHT, GlobalAddAtom(szTitle), GlobalAddAtom(szCopyright));
  474. end;
  475.  
  476. procedure    TNicoleAddon.Blink( theBlink:Boolean; theIcon:TIcon);
  477. begin
  478.     if nil = theIcon then begin
  479.         PostTaskBar(NTASKBAR_BLINK, WPARAM(theBlink), 0);
  480.     end else begin
  481.         PostTaskBar(NTASKBAR_BLINK, WPARAM(theBlink), theIcon.Handle);
  482.     end;
  483. end;
  484.  
  485. end.
  486.  
  487.