{*******************************************************}
{                                                       }
{       Tocsg.User32                                    }
{                                                       }
{       Copyright (C) 2022 sunk                         }
{                                                       }
{*******************************************************}

unit Tocsg.User32;

interface

uses
  System.SysUtils, Winapi.Windows;

const
  MSGFLT_ADD    = 1;
  MSGFLT_REMOVE = 2;

  MSGFLT_RESET = 0;
  MSGFLT_ALLOW = 1;
  MSGFLT_DISALLOW = 2;

  WM_COPYGLOBALDATA = $0049;

type
  PChangeFilterStruct = Pointer;

  TChangeWindowMessageFilter = function(dwMsg, dwFlag: DWORD): Boolean; stdcall;
  TChangeWindowMessageFilterEx = function(h: hWnd; dwWndMsg: DWORD; wAction: WORD; pChangeFilterData: PChangeFilterStruct): BOOL; stdcall;

//function ChangeWindowMessageFilter(dwMsg, dwFlag: DWORD): Boolean; // Winapi.Windows.pas 여기에 생김
function ChangeWindowMessageFilterEx(h: hWnd; dwWndMsg: DWORD; wAction: WORD; pChangeFilterData: PChangeFilterStruct): BOOL;

implementation

var
  _hUser32: THandle = 0;
  _fnChangeWindowMessageFilter: TChangeWindowMessageFilter = nil;
  _fnChangeWindowMessageFilterEx: TChangeWindowMessageFilterEx = nil;

function InitUser32Procedure: Boolean;
begin
  if _hUser32 = 0 then
  begin
    _hUser32 := GetModuleHandle(user32);
    if _hUser32 <> 0 then
    begin
      @_fnChangeWindowMessageFilter := GetProcAddress(_hUser32, 'ChangeWindowMessageFilter');
      @_fnChangeWindowMessageFilterEx := GetProcAddress(_hUser32, 'ChangeWindowMessageFilterEx');
    end;
  end;
  Result := _hUser32 <> 0;
end;

//function ChangeWindowMessageFilter(dwMsg, dwFlag: DWORD): Boolean;
//begin
//  Result := false;
//  if InitUser32Procedure and Assigned(_fnChangeWindowMessageFilter) then
//    Result := _fnChangeWindowMessageFilter(dwMsg, dwFlag);
//end;

function ChangeWindowMessageFilterEx(h: hWnd; dwWndMsg: DWORD; wAction: WORD; pChangeFilterData: PChangeFilterStruct): BOOL;
begin
  Result := false;
  if InitUser32Procedure and Assigned(_fnChangeWindowMessageFilterEx) then
    Result := _fnChangeWindowMessageFilterEx(h, dwWndMsg, wAction, pChangeFilterData);
end;

end.
