Ooops
November 25, 2020, 08:53:10 AM

Author Topic: [bb] CloseHandler by Xaymar [ 1+ years ago ]  (Read 673 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] CloseHandler by Xaymar [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : CloseHandler
Author : Xaymar
Posted : 1+ years ago

Description : Utility_InstallCloseHandler( hwnd )
Where: Whenever you want to catch the WM_CLOSE message of a window.
With: Window handle of the window you want it to hook into.
Utility_UninstallCloseHandler( hwnd )
What: Whenever you want to stop catching the WM_CLOSE message of a window.
With: Window handle of the window you want it to stop hooking into.
Utility_GetCloseCount%( hwnd )
What: Returns the amount of WM_CLOSE message catched for hwnd.
With: Window handle of the window you want to get the amount of WM_CLOSE from.


Code :
Code: BlitzBasic
  1. ;----------------------------------------------------------------
  2. ;-- Userlib
  3. ;----------------------------------------------------------------
  4. ;.lib "User32.dll"
  5. ;User32_SetWindowLong%(hwnd%, nIndex%, dwNewLong%):"SetWindowLongA"
  6. ;User32_GetWindowLong%(hwnd%, index%):"GetWindowLongA"
  7. ;
  8. ;.lib "Utility_CloseHandler.dll"
  9. ;Utility_InstallCloseHandler(hwnd%):"Utility_InstallCloseHandler"
  10. ;Utility_UninstallCloseHandler(hwnd%):"Utility_UninstallCloseHandler"
  11. ;Utility_GetCloseCount%(hwnd%):"Utility_GetCloseCount"
  12. ;----------------------------------------------------------------
  13.  
  14. ;----------------------------------------------------------------
  15. ;-- C++ DLL
  16. ;----------------------------------------------------------------
  17. ; #Include <windows.h>
  18. ;
  19. ; LRESULT CALLBACK _CloseWindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  20. ;
  21. ; struct WindowUserData {
  22. ;      Int oldWindowProcedure;
  23. ;      Int oldUserData;
  24. ;      Int closeCount;
  25. ; };
  26. ;
  27. ; STDAPIV_(void) Utility_InstallCloseHandler(HWND hwnd) {
  28. ;     If (hwnd) {
  29. ;         WindowUserData* hwndData = New WindowUserData;
  30. ;         ZeroMemory(hwndData, sizeof(hwndData));
  31. ;         hwndData->oldWindowProcedure = SetWindowLong(hwnd, GWL_WNDPROC, (LONG)&_CloseWindowProcedure);
  32. ;         hwndData->oldUserData = SetWindowLong(hwnd, GWL_USERDATA, (LONG)hwndData);
  33. ;     }
  34. ; }
  35. ;
  36. ; STDAPIV_(void) Utility_UninstallCloseHandler(HWND hwnd) {
  37. ;     If (hwnd) {
  38. ;         WindowUserData* hwndData = (WindowUserData*)GetWindowLong(hwnd, GWL_USERDATA);
  39. ;         If (hwndData) {
  40. ;             SetWindowLong(hwnd, GWL_USERDATA, hwndData->oldUserData);
  41. ;             SetWindowLong(hwnd, GWL_WNDPROC, hwndData->oldWindowProcedure);
  42. ;             Delete hwndData;
  43. ;         }
  44. ;     }
  45. ; }
  46. ;
  47. ; STDAPIV_(Int) Utility_GetCloseCount(HWND hwnd) {
  48. ;     If (hwnd) {
  49. ;         WindowUserData* hwndData = (WindowUserData*)GetWindowLong(hwnd, GWL_USERDATA);
  50. ;         If (hwndData) {
  51. ;             Int toReturn = hwndData->closeCount;
  52. ;             hwndData->closeCount = 0;
  53. ;             Return toReturn;
  54. ;         }
  55. ;     }
  56. ;     Return 0;
  57. ; }
  58. ;
  59. ; LRESULT CALLBACK _CloseWindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  60. ;     WindowUserData* hwndData = (WindowUserData*)GetWindowLong(hwnd, GWL_USERDATA);
  61. ;     If (hwndData) {
  62. ;         switch(uMsg) {
  63. ;             Case WM_CLOSE:
  64. ;             Case WM_DESTROY:
  65. ;                 hwndData->closeCount++;
  66. ;                 Return False;
  67. ;             Default:
  68. ;                 Return CallWindowProc((WNDPROC)hwndData->oldWindowProcedure, hwnd, uMsg, wParam, lParam);
  69. ;         }
  70. ;     } Else {
  71. ;         Return DefWindowProc(hwnd, uMsg, wParam, lParam);
  72. ;     }
  73. ; }
  74. ;
  75. ; BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {return TRUE;}
  76. ;----------------------------------------------------------------
  77. ; Linker Options: -static-libgcc -static-libstdc++
  78. ; Linker Libraries: user32
  79. ;----------------------------------------------------------------
  80.  
  81. SetBuffer BackBuffer()
  82.  
  83. Utility_InstallCloseHandler(SystemProperty("AppHWND"))
  84.  
  85. Local c
  86. While Not KeyHit(1)
  87.         c = c + Utility_GetCloseCount(SystemProperty("AppHWND"))
  88.         Cls
  89.        
  90.         Text MouseX(),MouseY(),c
  91.         Flip
  92. Wend
  93. Utility_UninstallCloseHandler(SystemProperty("AppHWND"))


Comments :


Mikorians(Posted 1+ years ago)

 Uh.. I don't have a C compiler


Guy Fawkes(Posted 1+ years ago)

 You want a more simple approach? Just use WinBlitz3D V.1.1 :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal