Recent

Author Topic: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.  (Read 3364 times)

jamie

  • Hero Member
  • *****
  • Posts: 4582
After seeing lots of efforts of those trying to implement unsupported message events in the Class code I decided to contribute this simple test project and MsgHook unit...

 Please rip it apart..  :)

 Currently its for windows but it should work for Linux too if you use the LCLType and LCLIntf with a some name changes.



 
The only true wisdom is knowing you know nothing

denis.totoliciu

  • New Member
  • *
  • Posts: 29
Re: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.
« Reply #1 on: August 29, 2020, 09:57:01 pm »
Very nice work and very useful!
It would be great to have it integrated in LCL.
Small and efficient and also helps a lot in debugging source code based on LCL.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 762
Re: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.
« Reply #2 on: August 30, 2020, 09:48:35 am »
Using list for this is way too slow. Delphi uses MakeObjectInstance and FreeObjectInstance from Classes unit for this purpose. Their implementation is ugly, but it looks like, that it's made for max performance. Problem is - they use runtime code generation, that isn't cross-platform, I guess. I.e. platform-dependent inc file is needed.
« Last Edit: August 30, 2020, 09:51:09 am by Mr.Madguy »
29.12.2021 - migration to DynamicData 4.1 is completed - complete overhaul of data access driver.
My project still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 13 years old.

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.
« Reply #3 on: August 30, 2020, 10:58:08 am »
Hi, nice, it will be very helpfull in some case, like grab keys from any where. +1  :)

I'll test it with my "HotShortcutkeys manager" component, it should be solved my cross-plateform compatibility.  ;)

Cheers

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 762
Re: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.
« Reply #4 on: August 30, 2020, 04:53:35 pm »
I understand the concept isn't as fast as doing it at the raw level.

Also if all u need is a couple of msgs added the search is at the top and exits when found

If u look how the lcl is done now it uses the dispatch system and it seems to work well enough


Any ways this would be less load per class if a feature of adding mgs were put in.
Have a good day :)
There are several ways to solve this problem. Standard way - is to allocate space in window (i.e. cbWndExtra), store pointer to Form there and read it via GetWindowLong. But it requires modifying window class registration procedure. Dunno, why nobody uses it. May be because it's way too slow. Or may be because it works on Windows only. Perfect way - to generate code stub. But it's not cross-platform. Yeah, lists are cross-platform. But at least it would be better to turn list into Form->Message tree, sort lists and use binary search. May be even hashing. Hashing would be easy, as handle is used as key, i.e. it would be enough to use several most significant bits as hash value.
29.12.2021 - migration to DynamicData 4.1 is completed - complete overhaul of data access driver.
My project still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 13 years old.

Polarx79

  • Newbie
  • Posts: 4
Re: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.
« Reply #5 on: August 30, 2020, 07:03:09 pm »
Thanks for the contribution!  :)

pcurtis

  • Hero Member
  • *****
  • Posts: 935
Re: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.
« Reply #6 on: January 22, 2022, 11:43:45 pm »
Jamie, I have been testing msgHook and found a small problem.

When I add a message, then change formstyle from fsSystemontop to fsNormal LCL controls on the form disappear!

Any ideas?

Demo attached.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 935
Re: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.
« Reply #7 on: January 23, 2022, 07:42:29 am »
Thanks Jamie for the response.

The Remove - Change formstyle - Add is the way I solved the issue, but thought there might be a better solution. If its a LCL issue then I'll have to live with it.

Again thanks, and thanks for releasing the unit.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

PascalDragon

  • Hero Member
  • *****
  • Posts: 4005
  • Compiler Developer
Re: Easy Message hooking for forms. UNIT and TEST PROJECT Attached.
« Reply #8 on: January 24, 2022, 01:41:46 pm »
Using list for this is way too slow. Delphi uses MakeObjectInstance and FreeObjectInstance from Classes unit for this purpose. Their implementation is ugly, but it looks like, that it's made for max performance. Problem is - they use runtime code generation, that isn't cross-platform, I guess. I.e. platform-dependent inc file is needed.

These are two different use cases:
  • MakeObjectInstance is to add message handling capability to any class that does not have a Handle associated with it
  • The purpose of jamie's code is to have the form (which does have a Handle associated after all) handle messages that the LCL would otherwise discard; it's essentially a more comfortable variant of this

 

TinyPortal © 2005-2018