You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unitwindowobject.pas 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. { Copyright (C) 2005 Bas Steendijk and Peter Green
  2. For conditions of distribution and use, see copyright notice in zlib_license.txt
  3. which is included in the package
  4. ----------------------------------------------------------------------------- }
  5. unit unitwindowobject;
  6. interface
  7. uses
  8. classes,
  9. {$ifdef win32}
  10. windows,messages,wmessages,
  11. {$else}
  12. lmessages,
  13. {$macro on}
  14. {$define windows := lmessages}
  15. {$endif}
  16. sysutils,
  17. pgtypes;
  18. type
  19. twindowobject=class(tobject)
  20. hwndmain:hwnd;
  21. onmsg:function(msg,wparam,lparam:taddrint):boolean of object;
  22. exitloopflag:boolean;
  23. function settimer(id,timeout:taddrint):integer;
  24. function killtimer(id:taddrint):boolean;
  25. procedure postmessage(msg,wparam,lparam:taddrint);
  26. procedure messageloop;
  27. {$ifdef win32}
  28. procedure processmessages;
  29. function processmessage:boolean;
  30. {$endif}
  31. constructor create;
  32. destructor destroy; override;
  33. end;
  34. implementation
  35. //uses safewriteln;
  36. function WindowProc(ahWnd:HWND; auMsg:Integer; awParam:WPARAM; alParam:LPARAM):Integer; stdcall;
  37. var
  38. i:taddrint;
  39. begin
  40. ////swriteln('in unitwindowobject.windowproc');
  41. Result := 0; // This means we handled the message
  42. if ahwnd <> hwnd(0) then i := getwindowlongptr(ahwnd,0) else i := 0;
  43. if i <> 0 then begin
  44. if assigned(twindowobject(i).onmsg) then begin
  45. if not twindowobject(i).onmsg(aumsg,awparam,alparam) then i := 0;
  46. end else i := 0
  47. end;
  48. if i = 0 then Result := DefWindowProc(ahWnd, auMsg, awParam, alParam)
  49. end;
  50. var
  51. twindowobject_Class : TWndClass = (style:0; lpfnWndProc:@WindowProc;
  52. cbClsExtra:0; cbWndExtra:sizeof(pointer); hInstance:thinstance(0); hIcon:hicon(0); hCursor:hcursor(0);
  53. hbrBackground:hbrush(0);lpszMenuName:nil; lpszClassName:'twindowobject_class');
  54. function twindowobject.settimer;
  55. begin
  56. result := windows.settimer(hwndmain,id,timeout,nil);
  57. end;
  58. function twindowobject.killtimer;
  59. begin
  60. result := windows.killtimer(hwndmain,id);
  61. end;
  62. constructor twindowobject.create;
  63. begin
  64. inherited;
  65. //swriteln('in twindowobject.create, about to call registerclass');
  66. Windows.RegisterClass(twindowobject_Class);
  67. //swriteln('about to call createwindowex');
  68. hWndMain := CreateWindowEx(WS_EX_TOOLWINDOW, twindowobject_Class.lpszClassName,
  69. '', WS_POPUP, 0, 0,0, 0, hwnd(0), 0, HInstance, nil);
  70. //swriteln('about to check result of createwindowex');
  71. if hWndMain = hwnd(0) then raise exception.create('CreateWindowEx failed');
  72. //swriteln('about to store reference to self in extra windo memory');
  73. setwindowlongptr(hwndmain,0,taddrint(self));
  74. //swriteln('finished twindowobject.create , hwndmain='+inttohex(taddrint(hwndmain),16));
  75. end;
  76. destructor twindowobject.destroy;
  77. begin
  78. if hWndMain <> hwnd(0) then DestroyWindow(hwndmain);
  79. inherited;
  80. end;
  81. procedure twindowobject.postmessage;
  82. begin
  83. windows.postmessage(hwndmain,msg,wparam,lparam);
  84. end;
  85. {$ifdef win32}
  86. function twindowobject.ProcessMessage : Boolean;
  87. var
  88. Msg : TMsg;
  89. begin
  90. Result := FALSE;
  91. if PeekMessage(Msg, hwndmain, 0, 0, PM_REMOVE) then begin
  92. Result := TRUE;
  93. DispatchMessage(Msg);
  94. end;
  95. end;
  96. procedure twindowobject.processmessages;
  97. begin
  98. while processmessage do;
  99. end;
  100. {$endif}
  101. procedure twindowobject.messageloop;
  102. var
  103. MsgRec : TMsg;
  104. begin
  105. while GetMessage(MsgRec, hwnd(0), 0, 0) do begin
  106. DispatchMessage(MsgRec);
  107. if exitloopflag then exit;
  108. {if not peekmessage(msgrec,0,0,0,PM_NOREMOVE) then onidle}
  109. end;
  110. end;
  111. end.