WM_ message List for destroy a window/exe application (GetMessage loop) lazarus / 21-General ---------------------------------------------------------------------- paule32 · 2025-05-24 11:43 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TWinControl = class(TControl)  private    FHandle: Integer;  protected    function HandleMessage(id: HWND; MSG: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT;  public    constructor Create;    destructor Destroy; override;  published    property Handle: Integer read FHandle;  end; function TWinControl.HandleMessage(id: HWND; MSG: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT;begin  writeln('wmsg: ' + IntToStr32(msg));  case msg of    WM_COMMAND: begin      writeln('control-ID: ' + IntToStr32(LoWord(wParam)));      writeln('code: ' + intToStr32(HiWord(wParam)));    end;    WM_CLOSE: begin      writeln('close');      DestroyWindow(id);    end;    WM_DESTROY: begin      writeln('des');      PostQuitMessage(0);      Halt(0);    end else begin      result := DefWindowProcA(id, msg, wParam, lParam);    end;  end;  result := 0;end;...  while GetMessageA(_msg, 0, 0, 0) do  begin    TranslateMessage(_msg);    DispatchMessageA(_msg);    p.HandleMessage(p.FHandle, _msg.Message, _msg.wParam, _msg.lParam);  end; the global window Proc as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GlobalWndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;var  winCtrl: TWinControl;  cs: PCREATESTRUCTA;begin  if msg = WM_NCCREATE then  begin    cs := PCREATESTRUCTA(lParam);    winCtrl := TWinControl(cs^.lpCreateParams);        if Assigned(winCtrl) then    begin      SetWindowLongPtrA(hwnd, GWLP_USERDATA, NativeInt(winCtrl));      winCtrl.FHandle := hWnd;      Exit(DefWindowProcA(hWnd, Msg, wParam, lParam));    end;  end;    winCtrl := TWinControl(GetWindowLongPtrA(hWnd, GWLP_USERDATA));    if Assigned(winCtrl) then  Exit(winCtrl.HandleMessage(hWnd, Msg, wParam, lParam)) else  Exit(DefWindowProcA(hWnd, Msg, wParam, lParam));end; When I click on the X (Close Button) I get Code 96 - but: const   WM_CLOSE              = $0010;   WM_COMMAND        = $0111;   WM_DESTROY          = $0002;   WM_NCCREATE         = $0081;   WM_QUIT                 = $0012; dont seem effected (the writeln Functions are not called - maybe the messages are wrong ?  ). When I move the mouse over the Window, I get code 512. So, after closeing the Window the message loop still idle. ---------------------------------------------------------------------- Remy Lebeau · 2025-05-26 18:53 Quote from: paule32 on May 24, 2025, 01:43:04 pmWhen I click on the X (Close Button) I get Code 96 Is that decimal or hex? Either way, there is no official Window message 96/$60 or 150/$96, so just ignore it. More importantly, why is your Handle declared as an Integer instead of a NativeInt or better a THandle? And why is your message loop calling HandleMessage() directly? DispatchMessage() will have already delivered the message to the window. You should be handling window messages in an overridden WndProc() method, or in individual message handlers. This code makes me think you are trying to implement your own TWinControl class manually instead of using the native one provided by the LCL. ---------------------------------------------------------------------- paule32 · 2025-05-27 11:15 Hi Remy  - yes, I try to implement own TWinControl - the Message 96 is a Hit-Test for Non-Client Area that I capture with WM_NCLButtonDown (also a Left-Mouse-Click) because I don't fiddle out why WM_SYSCOMMAND is not captured. - it could be a Problem with your Note by using NativeInt / THandle - by calling HandleMessage() I can get in touch with TControl Classes and Components - which I don't can easy reach by a normal WndProc - because I thinking that one WndProc is bound only one by a Pointer in single use - but a DLL shall handle more WndProc - by Pointer's, too - yes - but then the Pointer's a bound with a T*Class - you can follow the Posting: "I lost in Code" where I have already publish older Code of a Forms.pas, that shall handle Form Dialogs: https://dpaste.com/DBG6VPRZC All Code are experimental and not for productive systems, yet. ---------------------------------------------------------------------- Remy Lebeau · 2025-05-28 05:27 Quote from: paule32 on May 27, 2025, 01:15:35 pm- yes, I try to implement own TWinControl Why? Your implementation is not processing messages correctly. For one thing, all of your TWinControl and descendant classes are registering a single class name, which is just wrong. Different types of windows need different class names so they can customize their windows as needed without interfering with other window classes. But also, the message loop is simply the wrong place to handle messages for a window. The correct place is in the wndproc callback that is registered with RegisterClass() before the window is created, or subclassed after creation with SetWindowLongPtr(GWL_WNDPROC) or SetWindowSubclass(). You are calling HandleMessage() inside of your GlobalWndProc(), which is the correct thing to do. HandleMessage() does not belong in your modal message loop. Since you don't have a ModalResult property in your TForm class (which I suggest you add), you will need to instead use the return value of DispatchMessage(). Do not call HandleMessage() again after it has already been called by GlobalWndProc() inside of DispatchMessage(). And, don't forget to handle WM_QUIT in your modal loop, too. Something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i repeat  if GetMessageA(_msg, 0, 0, 0) then  begin    TranslateMessage(_msg);    ret := DispatchMessageA(_msg);    if (_msg.hwnd = p.FHandle) and       (_msg.Message = WM_NCLBUTTONDOWN) and       (LoWord(_msg.wParam) = HTCLOSE) and       (res = -100) then break;  end else  begin    PostQuitMessage(_msg.wParam);    break;  end;until False; Note, however, that WM_NCLBUTTONDOWN is the wrong message to detect when the window is closed. Use WM_CLOSE or WM_DESTROY for that instead. This would be simpler with a ModalResult property added, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i p.ModalResult := 0;repeat  if GetMessageA(_msg, 0, 0, 0) then  begin  TranslateMessage(_msg);    DispatchMessageA(_msg);  end else  begin  PostQuitMessage(_msg.wParam);    break;  end;until p.ModalResult <> 0;ShowWindow(p.FHandle, SW_HIDE); ... function TWinControl.HandleMessage(id: HWND; MSG: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT;begin  case msg of    ...    WM_CLOSE: begin      writeln('close');      ModalResult := 1;    end;    ...  end;end; Quote from: paule32 on May 27, 2025, 01:15:35 pm- the Message 96 is a Hit-Test for Non-Client Area that I capture with WM_NCLButtonDown (also a Left-Mouse-Click) WM_NCLButtonDown is 161 not 96. Like I said earlier, there is no standard window message 96. Also, 96 is not a defined value for the wParam of WM_NCLButtonDown, either. So, where exactly are you seeing 96? Quote from: paule32 on May 27, 2025, 01:15:35 pmI don't fiddle out why WM_SYSCOMMAND is not captured. It is because you are not calling DefWindowProc() when handling WM_NCLBUTTONDOWN. DefWindowProc() is who generates WM_SYSCOMMAND. Always call DefWindowProc() for all messages you don't process, don't just blindly return 0 for them. Quote from: paule32 on May 27, 2025, 01:15:35 pm- it could be a Problem with your Note by using NativeInt / THandle That is the least of your problems. Quote from: paule32 on May 27, 2025, 01:15:35 pm- by calling HandleMessage() I can get in touch with TControl Classes and Components - which I don't can easy reach by a normal WndProc Yes, a normal WndProc can access controls/component classes. Especially given that the WndProc (or, in your case, HandleMessage()) is a non-static class method, so you need access to the control/component class in order to call it. Which your GlobalWndProc() is doing. Quote from: paule32 on May 27, 2025, 01:15:35 pmI thinking that one WndProc is bound only one by a Pointer in single use - but a DLL shall handle more WndProc - by Pointer's, too - yes - but then the Pointer's a bound with a T*Class This is why the standard TWinControl.WndProc() is virtual. A central shared WndProc is registered for all control classes, and it delegates to a per-control WndProc based on the HWND being sent to.