* * *

Author Topic: How to handle Windows Messages  (Read 874 times)

HobbyDev

  • New member
  • *
  • Posts: 9
How to handle Windows Messages
« on: April 09, 2017, 12:39:43 pm »
Hi,

I am still trying to convert from Delphi to Lazarus. Now I am at this point that I don't know how to handle Windows Messages. I tried this code
Code: Pascal  [Select]
  1. TForm1 = class(TForm)
  2. ...
  3. public
  4. ...
  5. procedure WMPowerBroadcast(var MyMessage: TMessage); message WM_POWERBROADCAST;

but it seems like there is no message routed to my procedure. Is there anything available I can reed first to find out which messages I have to handle which way? Or does anybody has a nice example code to learn from?

Best Regards

ASerge

  • Sr. Member
  • ****
  • Posts: 340
Re: How to handle Windows Messages
« Reply #1 on: April 09, 2017, 04:40:17 pm »
I am still trying to convert from Delphi to Lazarus. Now I am at this point that I don't know how to handle Windows Messages.
This. Addition.

HobbyDev

  • New member
  • *
  • Posts: 9
Re: How to handle Windows Messages
« Reply #2 on: April 10, 2017, 08:24:58 pm »
Thanks for the link, ASerge! I found it prior to my post and was hoping to get something described in a detailed way, something I can read through to understand the way it works in Lazarus / Free Pascal. But it looks like I have to start this way %)

HobbyDev

  • New member
  • *
  • Posts: 9
Re: How to handle Windows Messages
« Reply #3 on: April 12, 2017, 08:36:28 pm »
Come back to the code ASerge posted
...
Code: Pascal  [Select]
  1. function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;
  2. begin
  3.   if uMsg=WM_NCHITTEST then
  4.   begin
  5.     result:=Windows.DefWindowProc(Ahwnd, uMsg, WParam, LParam);  //not sure about this one
  6.     if result=windows.HTCAPTION then result:=windows.HTCLIENT;
  7.     exit;
  8.   end;
  9.   result:=CallWindowProc(PrevWndProc,Ahwnd, uMsg, WParam, LParam);
  10. end;
Why do I have to call DefWindowProc before my handling takes place? I found an example from microsoft https://msdn.microsoft.com/de-de/library/windows/desktop/ms633570(v=vs.85).aspx#designing_proc:
Code: Pascal  [Select]
  1. LRESULT CALLBACK MainWndProc(
  2.     HWND hwnd,        // handle to window
  3.     UINT uMsg,        // message identifier
  4.     WPARAM wParam,    // first message parameter
  5.     LPARAM lParam)    // second message parameter
  6. {
  7.  
  8.     switch (uMsg)
  9.     {
  10.         case WM_CREATE:
  11.             // Initialize the window.
  12.             return 0;
  13.  
  14.         case WM_PAINT:
  15.             // Paint the window's client area.
  16.             return 0;
  17.  
  18.         case WM_SIZE:
  19.             // Set the size and position of the window.
  20.             return 0;
  21.  
  22.         case WM_DESTROY:
  23.             // Clean up window-specific data objects.
  24.             return 0;
  25.  
  26.         //
  27.         // Process other messages.
  28.         //
  29.  
  30.         default:
  31.             return DefWindowProc(hwnd, uMsg, wParam, lParam);
  32.     }
  33.     return 0;
  34. }
  35.  
  36.  
So they call it in the default branch after processing the different messages?
Whats right or wrong?
Second question is, does anyone has a list with the different return values from DefWindowProc for the standard windows messages?

ASerge

  • Sr. Member
  • ****
  • Posts: 340
Re: How to handle Windows Messages
« Reply #4 on: April 12, 2017, 10:12:35 pm »
So they call it in the default branch after processing the different messages?
Whats right or wrong?
If you are sure that the standard message processing is not required, do not call DefWindowProc.

Quote
Second question is, does anyone has a list with the different return values from DefWindowProc for the standard windows messages?
For each message of their own - you need to look in the documentation.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus