Einzelnen Beitrag anzeigen

helgew

Registriert seit: 30. Jul 2008
125 Beiträge
 
#4

Re: Eigene Menuitems in taskbar-popup einschleusen

  Alt 30. Jul 2008, 23:19
mein aktueller Stand ist nun folgender :

Delphi-Quellcode:
program ppext;

{$APPTYPE CONSOLE}

uses
  Windows;

type
  TChar = {$IFDEF UNICODE} WideChar; {$ELSE} Char; {$ENDIF}

const
  MyMenuItemCaption = 'Set process priority ...';
  WH_MOUSE_LL = 14; // NT only?
var
  hMouseHook: HHook;
  hDLL: hModule;
  MouseHookProc: Pointer;

function CleanUpEnumWindowProc(handle:HWND; param: Pointer):BOOL; StdCall;
var
  hSystemMenu : THandle;
  bla : Array[0..255] of char;
  i,n: integer;
  itemtitle: string;
begin
  zeromemory(@bla[0],256);
  if IsWindowVisible(handle) then
  begin
    hSystemMenu := GetSystemMenu(handle,false);
    n := windows.GetMenuItemCount(hSystemMenu);
    for i := n-1 downto 0 do
    begin
      Windows.GetMenuString(hSystemMenu,i,@bla[0],256 div SizeOf(TChar)-1,MF_BYPOSITION);
      itemtitle := PAnsiChar(Pointer(@bla[0]));
      if itemtitle = MyMenuItemCaption then
      begin
        if windows.DeleteMenu(hSystemMenu,i,MF_BYPOSITION) then
        inc(integer(param^));
      end;
    end;
  end;
  result := true;
end;



function CreatingEnumWindowProc(handle:HWND;param: Pointer):BOOL; StdCall;
var
  hSystemMenu : THandle;
  bla : Array[0..255] of char;
  itemtitle: string;
  m: tagMENUITEMINFO;

  function HasPPMenuItem ( hMenu: THandle ): Boolean;
  var i,n: integer;
  begin
    result := false;
    n := windows.GetMenuItemCount(hMenu);
    for i := n-1 downto 0 do
    begin
      zeromemory(@bla[0],256);
      Windows.GetMenuString(hSystemMenu,i,@bla[0],256 div SizeOf(TChar)-1,MF_BYPOSITION);
      itemtitle := PAnsiChar(Pointer(@bla[0]));
      if itemtitle = MyMenuItemCaption then
      begin
        result := true;
        break;
      end;
    end;
  end;

begin
  if IsWindowVisible(handle) then
  begin
    hSystemMenu := GetSystemMenu(handle,false);
    if hSystemMenu <> 0 then
      if not HasPPMenuItem (hSystemMenu) then
      begin
        if InsertMenu( hSystemMenu,0,MF_ENABLED,0,MyMenuItemCaption) then
        inc(integer(param^));
      end;
  end;
  result := true;
end;



// main
var
  count: integer;
begin
  EnumWindows( @CreatingEnumWindowProc,Cardinal(Addr(count)));
  writeln(count, ' new menu items installed.'); count := 0;

  hMouseHook := 0;
  hDLL := LoadLibrary('MouseHookHandler.dll');
  if hDLL <> 0 then
  begin
    MouseHookProc := GetProcAddress(hDLL,'CallbackProc');
    hMouseHook := SetWindowsHookEx( WH_MOUSE_LL,MouseHookProc,hDLL,0);
    if hMouseHook <> 0 then writeln('Hook set.');
  end;

  readln;

  EnumWindows( @CleanUpEnumWindowProc,Cardinal(Addr(count)));
  writeln(count, ' items removed.');

  if hMouseHook <> 0 then
  begin
    UnhookWindowsHookEx(hMouseHook);
    writeln('Unhooked.');
  end;
  if hDLL <>0 then windows.FreeLibrary(hDLL);
  
  
  readln;
end.
dll:
Delphi-Quellcode:
library MouseHookHandler;

uses
  Windows;

{$R *.res}

var
  hMouseHook : hHook;



function CallbackProc(nCode: Integer; wParam: WPARAM;
                          lParam: LPARAM): LRESULT; stdcall;
begin
  // custom handling in background
  result := CallNextHookEx( hMouseHook, nCode, wParam, lParam);
end;

procedure SetHookHandle(h : HHOOK); stdcall;
begin
  hMouseHook := h;
end;


exports
   CallbackProc,
   SetHookHandle;

begin
end.

mal deinen Link anschauen...
  Mit Zitat antworten Zitat