1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| ...
const
WM_INPLACEEDIT_EXIT = WM_USER + $100;
type
TStringGridHelper = Class(TStringGrid);
...
TForm1 = class(TForm)
...
procedure FormCreate(Sender: TObject);
procedure StringGrid1GetEditText(Sender: TObject; ACol, ARow: Integer;
var Value: String);
procedure StringGrid1Exit(Sender: TObject);
...
OldWndProc, NewWndProc: TFarProc;
hCurrentInplaceEdit: HWND;
procedure HookInplaceEdit(hw: HWND);
procedure InplaceEditWndProc(var Message: TMessage);
procedure UnHookInplaceEdit(hw: HWND);
procedure InplaceEditOnExit(var Message: TMessage); message WM_INPLACEEDIT_EXIT;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
hCurrentInplaceEdit := 0;
OldWndProc := nil;
NewWndProc := nil;
end;
procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol,
ARow: Integer; var Value: String);
begin
if Assigned(TStringGridHelper(StringGrid1).InplaceEditor) then
HookInplaceEdit(TStringGridHelper(StringGrid1).InplaceEditor.Handle);
end;
procedure TForm1.StringGrid1Exit(Sender: TObject);
begin
if Assigned(TStringGridHelper(StringGrid1).InplaceEditor) then
UnHookInplaceEdit(TStringGridHelper(StringGrid1).InplaceEditor.Handle);
end;
procedure TForm1.HookInplaceEdit(hw: HWND);
begin
if hCurrentInplaceEdit = 0 then
begin
OldWndProc := TFarProc(GetWindowLong(hw, GWL_WNDPROC));
NewWndProc := Classes.MakeObjectInstance(InplaceEditWndProc);
SetWindowLong(hw, GWL_WNDPROC, LongInt(NewWndProc));
hCurrentInplaceEdit := hw;
//SendMessage(Self.Handle, WM_INPLACEEDIT_EXIT, 1, 0);
end;
end;
procedure TForm1.UnHookInplaceEdit(hw: HWND);
begin
if hCurrentInplaceEdit > 0 then
begin
SetWindowLong(hw, GWL_WNDPROC, LongInt(OldWndProc));
hCurrentInplaceEdit := 0;
if Assigned(NewWndProc) then
begin
Classes.FreeObjectInstance(NewWndProc);
NewWndProc := nil;
end;
//SendMessage(Self.Handle, WM_INPLACEEDIT_EXIT, 2, 0);
end;
end;
procedure TForm1.InplaceEditWndProc(var Message: TMessage);
begin
//call old proc
Message.Result := CallWindowProc(OldWndProc, hCurrentInplaceEdit, Message.Msg,
Message.wParam, Message.lParam);
if Message.Msg = WM_KILLFOCUS then
begin
PostMessage(Self.Handle, WM_INPLACEEDIT_EXIT, 0, MakeLong(StringGrid1.Col, StringGrid1.Row));
end;
end;
procedure TForm1.InplaceEditOnExit(var Message: TMessage);
begin
if Message.WParam = 0 then
begin
StringGrid1.Cells[Message.LParamLo, Message.LParamHi] := '111';
end;
end; |
Partager