home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kompon / d23456 / PHANTOM.ZIP / English / Phantom.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-20  |  16KB  |  575 lines

  1. {
  2.  
  3.   TPhantom 7.0
  4.  
  5.   programming by Roland Gruber (delphi@rolandgruber.de)
  6.                                (http://www.rolandgruber.de)
  7.  
  8.   You can use this component in commercial and noncommercial
  9.   programms as long as you mention the author(i.g. in an infobox).
  10.  
  11.   The use of this component is at your own risk.
  12.   I do not take any responsibility for any damages.
  13.  
  14.   If you improve this component I would be happy if you
  15.   sent me a copy of the source code and if you have
  16.   suggestions how to improve the component just send
  17.   me an e-mail.
  18.   (delphi@rolandgruber.de)
  19.  
  20.   PAY ATTENTION!!! This component does not work with Win NT!
  21.  
  22.   How to use this component:
  23.  
  24.   Visible: defines if your form is visible or not
  25.            Set this property at run-time (for example at Form.OnActivate)
  26.   Serviceprocess: hides the program from the taskmanager(CTRL+ALT+DEL)
  27.   Iconfile: filename of the .ico-file for the taskbar icon
  28.   Iconvisibility: defines if the taskbar icon is visible
  29.   Popupright: name of the popup window that appears when
  30.                the user right clicks on the taskbar icon
  31.   Popupleft:  the same as above on left click
  32.   Leftclick:  what happens if the user left clicks on
  33.                the taskbar icon
  34.   Rightclick: same as above on a right click
  35.   Doubleclick: same as above on a double click
  36.   Tip: Text that is shown when cursor is over taskbar icon
  37.   Flash: flashes the button in the taskbar
  38.          (!!! returns to false immediately after being set to true (after one flashing)!!!)
  39.   FlashTime: time(ms) of flashing
  40.   Priority: Priority of the application
  41.   MousePosX: Position of cursor on x-axis
  42.   MousePosY: Position of cursor on y-axis
  43.   MouseRightClick: simulates click of the right mouse button
  44.   MouseLeftClick: simulates click of the left mouse button
  45.   MouseMiddleClick: simulates click of the middle mouse button
  46.   SendString: simulates the input of a string by keyboard
  47.               ( supported characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  48.                                       abcdefghijklmnopqrstuvwxyz
  49.                                       1234567890!"º$%&/()=
  50.                                       ,.-;:_ +* )
  51.   LockInput: this locks the mouse and keyboard: if set to liNormal the user is able to unlock
  52.              both by pressing Ctrl+Alt+Del, if set to liHard he has no chance
  53.   }
  54.  unit Phantom;
  55.  
  56. interface
  57.  
  58. uses
  59.   Windows, wintypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  60.   menus, shellapi, extctrls;
  61.  
  62.   const
  63.   TASKBAREVENT: PChar = 'NewTNIMessage_20';
  64.  
  65. type
  66.  
  67.   TPriority = (Idle, Normal, Hoch, Realtime);
  68.   TLock = (liHard, liNormal, liNone);
  69.   TPhantom = class(TComponent)
  70.   private
  71.     fIconDatei: Ticon;
  72.     ftip:string  ;
  73.     fSichtbar: Boolean ;
  74.     fServiceprocess: Boolean ;
  75.     fLinksKlick: TNotifyEvent;
  76.     fRechtsKlick: TNotifyEvent;
  77.     fDoppelKlick: TNotifyEvent;
  78.     fIconSichtbarkeit: boolean;
  79.     fPopupRechts: TPopupMenu;
  80.     fPopupLinks: TPopupMenu;
  81.     fblinken:boolean;
  82.     fblinkdauer:integer;
  83.     fprioritaet: TPriority;
  84.     fcursor: TPoint;
  85.     kerneldll:THandle;
  86.     userdll: THandle;
  87.     fkeycode: string;
  88.     feingabesperre:TLock;
  89.     { Private-Deklarationen }
  90.     procedure PWsichtbarkeit(value:boolean);
  91.     procedure PWserviceprocess(value:boolean);
  92.     procedure sichtbarmachen;
  93.     procedure unsichtbarmachen;
  94.     procedure WPopRechts;
  95.     procedure WPopLinks;
  96.     procedure CreateIcon;
  97.     procedure ChangeIcon;
  98.     procedure DeleteIcon;
  99.     procedure settip(value:string);
  100.     procedure Wicondatei(value:Ticon);
  101.     procedure WIconSichtbarkeit(value:boolean);
  102.     procedure blink(value:boolean);
  103.     procedure prioritaetsetzen(value:TPriority);
  104.     function  getposx:integer;
  105.     function  getposy:integer;
  106.     procedure setposx(value:integer);
  107.     procedure setposy(value:integer);
  108.     procedure setsendstring(value:string);
  109.     procedure SetSperre(value:TLock);
  110.   protected
  111.     { Protected-Deklarationen }
  112.     procedure serviceprocessEin;
  113.     procedure serviceprocessAus;
  114.     procedure WRechtsklick; virtual;
  115.     procedure WLinksklick; virtual;
  116.     procedure WDoppelklick; virtual;
  117.     procedure WndProc(var Msg: TMessage);
  118.     procedure Notification(Component: TComponent; Operation: TOperation); override;
  119.   public
  120.     { Public-Deklarationen }
  121.       procedure loaded;override;
  122.       destructor Destroy; override;
  123.       constructor Create(AOwner: TComponent); override;
  124.    published
  125.     { Published-Deklarationen }
  126.     procedure MouseRightClick;
  127.     procedure MouseLeftClick;
  128.     procedure MouseMiddleClick;
  129.     property Visible: Boolean read fsichtbar write PWsichtbarkeit;
  130.     property Serviceprocess: Boolean read fserviceprocess write PWserviceprocess;
  131.     property Iconfile: Ticon read fIconDatei write Wicondatei;
  132.     property Iconvisibility: boolean read fIconSichtbarkeit write WIconSichtbarkeit;
  133.     property Leftclick: TNotifyEvent read fLinksKlick write flinksklick;
  134.     property Rightclick: TNotifyEvent read fRechtsKlick write fRechtsklick;
  135.     property Doublelick: TNotifyEvent read fDoppelKlick write fDoppelklick;
  136.     property PopupRight: TPopupMenu read fPopupRechts write fPopuprechts;
  137.     property PopupLeft: TPopupMenu read fPopupLinks write fPopuplinks;
  138.     property Tip: string read FTip write SetTip;
  139.     property Flash: boolean read fblinken write blink;
  140.     property FlashTime: integer read fblinkdauer write fblinkdauer;
  141.     property Priority: TPriority  read fprioritaet write prioritaetsetzen;
  142.     property MousePosX: integer read getposx write setposx;
  143.     property MousePosY: integer read getposy write setposy;
  144.     property SendString: string read fkeycode write setsendstring;
  145.     property LockInput: TLock read feingabesperre write SetSperre;
  146.   end;
  147.  
  148. procedure Register;
  149.  
  150. implementation
  151.  
  152. var ficonmessage: UINT;
  153.     FWnd: HWnd;
  154.  
  155. constructor tphantom.Create(AOwner: TComponent) ;
  156. begin
  157. inherited Create(AOwner);
  158. ficonmessage := RegisterWindowMessage(TASKBAREVENT);
  159. FWnd := AllocateHWnd(WndProc);
  160. FIcondatei := TIcon.Create;
  161. iconvisibility:=false ;
  162. visible:=true;
  163. serviceprocess:=false;
  164. fblinken:=false;
  165. fblinkdauer:=300;
  166. priority:=normal;
  167. kerneldll:=LoadLibrary('kernel32.dll');
  168. userdll:=Loadlibrary('user32.dll');
  169. LockInput:=liNone;
  170. end;
  171.  
  172. destructor TPhantom.Destroy;
  173. begin
  174.   if Ficonsichtbarkeit then deleteIcon;
  175.   FIcondatei.Free;
  176.   DeallocateHWnd(FWnd);
  177.   FreeLibrary(kerneldll);
  178.   Freelibrary(userdll);
  179.   inherited destroy;
  180. end;
  181.  
  182. procedure TPhantom.WndProc(var Msg: TMessage);
  183. begin
  184.   with Msg do begin
  185.     if Msg = ficonmessage then
  186.       case LParamLo of
  187.         WM_LBUTTONDOWN:   WLinksKlick;
  188.         WM_RBUTTONDOWN:   WRechtsKlick;
  189.         WM_LBUTTONDBLCLK: WDoppelKlick;
  190.       end
  191.     else
  192.       Result := DefWindowProc(FWnd, Msg, wParam, lParam);
  193.   end;
  194. end;
  195.  
  196. procedure TPhantom.Notification(Component: TComponent; Operation: TOperation);
  197. begin
  198.   inherited Notification(Component, Operation);
  199.   if Operation = opRemove then begin
  200.     if Component = FPopuprechts then FPopuprechts := nil;
  201.     if Component = FPopuplinks then FPopuplinks := nil;
  202.   end;
  203. end;
  204.  
  205. procedure TPhantom.PWsichtbarkeit(value:boolean);
  206. begin
  207. fsichtbar:=value;
  208. if (csDesigning in ComponentState) then exit;
  209. if visible=true then sichtbarmachen;
  210. if visible=false then unsichtbarmachen;
  211. end;
  212.  
  213. procedure TPhantom.PWserviceprocess(value:boolean);
  214. begin
  215. fserviceprocess:=value;
  216. if serviceprocess then serviceprocessEin;
  217. if not serviceprocess then serviceprocessAus;
  218. end;
  219.  
  220. procedure TPhantom.serviceprocessEin;
  221. type Tregisterservice = function(dwProcessId,dwType:dword): Integer;stdcall;
  222. var registerserviceprocess:Tregisterservice;
  223. begin
  224.   if (csDesigning in ComponentState) then exit;
  225.   @registerserviceprocess:=GetProcAddress(kerneldll, 'RegisterServiceProcess');
  226.   if @registerserviceprocess=nil then exit;
  227.   RegisterServiceProcess(GetCurrentProcessID,1);
  228. end;
  229.  
  230. procedure TPhantom.serviceprocessAus;
  231. type Tregisterservice = function(dwProcessId,dwType:dword): Integer;stdcall;
  232. var registerserviceprocess:Tregisterservice;
  233. begin
  234.   if (csDesigning in ComponentState) then exit;
  235.   @registerserviceprocess:=GetProcAddress(kerneldll, 'RegisterServiceProcess');
  236.   if @registerserviceprocess=nil then exit;
  237.   RegisterServiceProcess(GetCurrentProcessID,0);
  238. end;
  239.  
  240. procedure TPhantom.sichtbarmachen;
  241. begin
  242. if (csDesigning in ComponentState) then exit;
  243. showwindow(FindWindow(nil, @Application.Title[1]),SW_RESTORE)
  244. end;
  245.  
  246. procedure TPhantom.unsichtbarmachen;
  247. var handle:HWND;
  248. begin
  249. if (csDesigning in ComponentState) then exit;
  250. handle:=FindWindow(nil, @Application.Title[1]);
  251. showwindow(handle,SW_MINIMIZE);
  252. showwindow(handle,SW_HIDE) ;
  253. end;
  254.  
  255. procedure TPhantom.WRechtsklick;
  256. begin
  257. if Assigned(FPopuprechts) then WPoprechts
  258.   else if Assigned(FRechtsKlick) then
  259.     FRechtsKlick(Self);
  260. end;
  261.  
  262. procedure TPhantom.WLinksklick;
  263. begin
  264. if Assigned(FPopuplinks) then WPoplinks
  265.   else if Assigned(FLinksKlick) then
  266.     FLinksKlick(Self);
  267. end;
  268.  
  269. procedure TPhantom.WDoppelklick;
  270. begin
  271. if Assigned(Fdoppelklick)then FDoppelKlick(Self);
  272. end;
  273.  
  274. procedure TPhantom.WPopRechts;
  275. var punkt:Tpoint;
  276. begin
  277. GetCursorPos(Punkt);
  278. SetForeGroundWindow(FWnd);
  279. FPopuprechts.Popup(Punkt.X, Punkt.Y);
  280. end;
  281.  
  282. procedure TPhantom.WPopLinks;
  283. var punkt:Tpoint;
  284. begin
  285. GetCursorPos(Punkt);
  286. SetForeGroundWindow(FWnd);
  287. FPopuplinks.Popup(Punkt.X, Punkt.Y);
  288. end;
  289.  
  290. procedure TPhantom.CreateIcon;
  291. var icon: TNOTIFYICONDATA;
  292. begin
  293. with icon do begin
  294.     cbSize := SizeOf(TNOTIFYICONDATA);
  295.     Wnd := FWnd;
  296.     uID := 1;
  297.     uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
  298.     uCallbackMessage :=ficonMessage;
  299.     hIcon := FIcondatei.Handle;
  300.     StrCopy(szTip, PChar(FTip));
  301.     Shell_NotifyIcon(NIM_ADD, @icon);
  302.   end;
  303. ficonsichtbarkeit:=true;
  304. end;
  305.  
  306. procedure TPhantom.ChangeIcon;
  307. var icon: TNOTIFYICONDATA;
  308. begin
  309. with icon do begin
  310.     cbSize := SizeOf(TNOTIFYICONDATA);
  311.     Wnd := FWnd;
  312.     uID := 1;
  313.     uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
  314.     uCallbackMessage := ficonMessage;
  315.     hIcon := FIcondatei.Handle;
  316.     StrCopy(szTip, PChar(FTip));
  317.     Shell_NotifyIcon(NIM_MODIFY, @icon);
  318.   end;
  319. ficonsichtbarkeit:=true;
  320. end;
  321.  
  322. procedure TPhantom.DeleteIcon;
  323. var icon: TNOTIFYICONDATA;
  324. begin
  325. with icon do begin
  326.     cbSize := SizeOf(TNOTIFYICONDATA);
  327.     Wnd := FWnd;
  328.     uID := 1;
  329.     uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
  330.     uCallbackMessage := ficonmessage;
  331.     hIcon := FIcondatei.Handle;
  332.     StrCopy(szTip, PChar(FTip));
  333.     Shell_NotifyIcon(NIM_DELETE, @icon);
  334.   end;
  335. ficonsichtbarkeit:=false;
  336. end;
  337.  
  338. procedure TPhantom.settip(value:string);
  339. begin
  340. if FTip <> value then begin
  341.     FTip := value;
  342.     changeicon;
  343.   end;
  344. end;
  345.  
  346. procedure TPhantom.Wicondatei(value:Ticon);
  347. begin
  348. if ficondatei<>value then begin
  349.       ficondatei.assign(value);
  350.       if (csDesigning in ComponentState) then exit;
  351.       if ficonsichtbarkeit then changeicon else createicon;
  352.       if ficondatei.empty then deleteicon;
  353.   end;
  354. end;
  355.  
  356. procedure TPhantom.WIconSichtbarkeit(value:boolean);
  357. begin
  358. ficonsichtbarkeit:=value;
  359. if not (csDesigning in ComponentState) then begin
  360.    if ficonsichtbarkeit then createicon;
  361.    if not ficonsichtbarkeit then deleteicon;
  362.    end;
  363. end;
  364.  
  365. procedure TPhantom.Loaded;
  366. begin
  367.   inherited Loaded;
  368.   if Ficonsichtbarkeit and not FIcondatei.Empty then begin
  369.     createIcon;
  370.   end;
  371. end;
  372.  
  373. procedure TPhantom.blink(value:boolean);
  374. begin
  375. fblinken:=value;
  376. if fblinken=false then exit;
  377. flashwindow(application.handle,true);
  378. sleep(fblinkdauer);
  379. flashwindow(application.handle,true);
  380. fblinken:=false;
  381. end;
  382.  
  383. procedure TPhantom.prioritaetsetzen(value:TPriority);
  384. begin
  385. fprioritaet:=value;
  386. if (csdesigning in componentstate) then exit;
  387. case value of Idle : setpriorityclass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  388.               Normal : setpriorityclass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  389.               Hoch : setpriorityclass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  390.               Realtime : setpriorityclass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
  391.    end;
  392. end;
  393.  
  394. procedure TPhantom.MouseRightClick;
  395. begin
  396. mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);
  397. mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);
  398. end;
  399.  
  400. procedure TPhantom.MouseLeftClick;
  401. begin
  402. mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
  403. mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
  404. end;
  405.  
  406. procedure TPhantom.MouseMiddleClick;
  407. begin
  408. mouse_event(MOUSEEVENTF_MIDDLEDOWN,0,0,0,0);
  409. mouse_event(MOUSEEVENTF_MIDDLEUP,0,0,0,0);
  410. end;
  411.  
  412. procedure TPhantom.setposx(value:integer);
  413. begin
  414. getcursorpos(fcursor);
  415. fcursor.x:=value;
  416. setcursorpos(fcursor.x, fcursor.y);
  417. end;
  418.  
  419. procedure TPhantom.setposy(value:integer);
  420. begin
  421. getcursorpos(fcursor);
  422. fcursor.y:=value;
  423. setcursorpos(fcursor.x, fcursor.y);
  424. end;
  425.  
  426. function TPhantom.getposx:integer;
  427. begin
  428. getcursorpos(fcursor);
  429. result:=fcursor.x;
  430. end;
  431.  
  432. function TPhantom.getposy:integer;
  433. begin
  434. getcursorpos(fcursor);
  435. result:=fcursor.y;
  436. end;
  437.  
  438. procedure TPhantom.setsendstring(value:string);
  439. var c:byte;
  440. label fertig;
  441. begin
  442. fkeycode:=value;
  443. if length(value)=0 then exit;
  444. while length(value)<>0 do begin
  445. c:=byte(value[1]);
  446. if c>=33 then begin    
  447.   if c<=41 then begin
  448.   c:=(c+16);
  449.   keybd_event(VK_SHIFT, 0, 0,0);
  450.   keybd_event(c, 0, 0,0);
  451.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  452.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP,0);
  453.   goto fertig;
  454.   end;
  455.  end;
  456. if c>=48 then begin
  457.   if c<=57 then begin
  458.   keybd_event(c, 0, 0,0);
  459.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  460.   goto fertig;
  461.   end;
  462.  end;
  463. if c>=65 then begin
  464.   if c<=90 then begin
  465.   keybd_event(VK_SHIFT, 0, 0,0);
  466.   keybd_event(c, 0, 0,0);
  467.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  468.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP,0);
  469.   goto fertig;
  470.   end;
  471.  end;
  472. if c>=97 then begin
  473.   if c<=122 then begin
  474.   c:=(c-32);
  475.   keybd_event(c, 0, 0,0);
  476.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  477.   goto fertig;
  478.  end;
  479. end;
  480. if c=58 then begin
  481.   c:=190;
  482.   keybd_event(VK_SHIFT, 0, 0,0);
  483.   keybd_event(c, 0, 0,0);
  484.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  485.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP,0);
  486.   goto fertig;
  487. end;
  488. if c=59 then begin
  489.   c:=188;
  490.   keybd_event(VK_SHIFT, 0, 0,0);
  491.   keybd_event(c, 0, 0,0);
  492.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  493.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP,0);
  494.   goto fertig;
  495. end;
  496. if c=95 then begin
  497.   c:=189;
  498.   keybd_event(VK_SHIFT, 0, 0,0);
  499.   keybd_event(c, 0, 0,0);
  500.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  501.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP,0);
  502.   goto fertig;
  503. end;
  504. if c=167 then begin
  505.   c:=51;
  506.   keybd_event(VK_SHIFT, 0, 0,0);
  507.   keybd_event(c, 0, 0,0);
  508.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  509.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP,0);
  510.   goto fertig;
  511. end;
  512. if c=61 then begin
  513.   c:=48;
  514.   keybd_event(VK_SHIFT, 0, 0,0);
  515.   keybd_event(c, 0, 0,0);
  516.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  517.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP,0);
  518.   goto fertig;
  519. end;
  520. if c=42 then begin
  521.   c:=187;
  522.   keybd_event(VK_SHIFT, 0, 0,0);
  523.   keybd_event(c, 0, 0,0);
  524.   keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  525.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP,0);
  526.   goto fertig;
  527. end;
  528. if c=43 then c:=187;
  529. if c=44 then c:=188;
  530. if c=46 then c:=190;
  531. if c=45 then c:=189;
  532.  keybd_event(c, 0, 0,0);
  533.  keybd_event(c, 0, KEYEVENTF_KEYUP,0);
  534. fertig:
  535. delete(value,1,1);
  536. application.ProcessMessages;
  537. end;
  538. end;
  539.  
  540. procedure TPhantom.SetSperre(value:TLock);
  541. type Tblockinput = function(value:boolean): Dword;stdcall;
  542. var blockinput:Tblockinput;
  543.     j:integer;
  544. begin
  545.   if (csDesigning in ComponentState) then begin
  546.   feingabesperre:=value;
  547.   exit;
  548.   end;
  549.   @blockinput:=GetProcAddress(userdll, 'BlockInput');
  550.   if @blockinput=nil then begin
  551.   feingabesperre:=value;
  552.   exit;
  553.   end;
  554.   if value=liNone then begin
  555.    blockinput(false);
  556.    if feingabesperre=liHard then SystemParametersInfo(SPI_SCREENSAVERRUNNING,0,@j,0);
  557.   end
  558.   else if value=liNormal then begin
  559.    if feingabesperre=liHard then SystemParametersInfo(SPI_SCREENSAVERRUNNING,0,@j,0)
  560.    else blockinput(true);
  561.   end
  562.   else if value=liHard then begin
  563.    blockinput(true);
  564.    SystemParametersInfo(SPI_SCREENSAVERRUNNING,1,@j,0);
  565.   end;
  566. feingabesperre:=value;
  567. end;
  568.  
  569. procedure Register;
  570. begin
  571.   RegisterComponents('Roland', [TPhantom]);
  572. end;
  573.  
  574. end.
  575.