Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Topic[话题] by (100 points)

In TForm1.WndProc Mouse wheel can detected but "My Menu Item" click not detected. what is the problem?

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, FMX.Forms,
  FMX.Platform.Win, FMX.Types, FMX.Layouts, FMX.Memo, FMX.Memo.Types,
  FMX.Controls, FMX.Controls.Presentation, FMX.ScrollBox;

const
  SC_MyMenuItem = WM_USER + 1;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FHwnd: HWND;//Save the window handle
    FOldWndProc: LONG;//Save the original message processing function
  public
    function WndProc(HWND: HWND; Msg: UINT; wParam: wParam; lParam: lParam): LRESULT;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

function WindowProc(HWND: HWND; Msg: UINT; wParam: wParam; lParam: lParam): LRESULT; stdcall;
begin
 //Because during normal development, you need to access the methods or controls inside the window, etc.
 //For convenience, so here is a message forwarding
  Result := Form1.WndProc(HWND, Msg, wParam, lParam);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 //Obtain the handle of the main window. Under the FMX framework, Handle is no longer the handle of this window, you need to convert it
  FHwnd := FmxHandleToHwnd (Handle);

  AppendMenu(GetSystemMenu(FHwnd, FALSE), MF_SEPARATOR, 0, '');
  AppendMenu(GetSystemMenu(FHwnd, FALSE), MF_STRING, SC_MyMenuItem, 'My Menu Item');

 //Save the original WindowProc address
  FOldWndProc := GetWindowLongPtr(FHwnd, GWL_WNDPROC);
 //Get message processing rights
  SetWindowLongPtr(FHwnd, GWL_WNDPROC, NativeInt(@WindowProc));
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 //Because the message delivered by Windows cannot be processed after the window is destroyed, a memory access error will occur
 //So before the window is destroyed, the message processing power should be transferred to the original WindowProc
  SetWindowLongPtr(FHwnd, GWL_WNDPROC, FOldWndProc);
end;

function TForm1.WndProc(HWND: HWND; Msg: UINT; wParam: wParam; lParam: lParam): LRESULT;
begin
  Result := 0;

 //Test the mouse wheel message here

  if Msg = WM_MOUSEWHEEL then
  begin
    Memo1.Lines.Add('You used the mouse wheel');
    Exit;
  end;

  if Msg = SC_MyMenuItem then
  begin
    Memo1.Lines.Add('My Menu Item Clicked');
    Exit;
  end;

//  if Msg = 293 then
//  begin
//    Memo1.Lines.Add('My Menu Item Clicked');
//    Exit;
//  end;

  Result := CallWindowProc(Ptr(FOldWndProc), HWND, Msg, wParam, lParam);
end;

end.
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...