5,388
社区成员
发帖
与我相关
我的任务
分享
if (GetParent(Msg.hwnd) = IPEdit) and (Msg.message = WM_KEYDOWN)
and (Msg.wParam in [VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN]) then
begin
case Msg.wParam of
VK_LEFT, VK_UP: D := -1;
VK_RIGHT, VK_DOWN: D := 1;
end;
SendMessage(Msg.hwnd, EM_GETSEL, WPARAM(@P1), 0);
SendMessage(Msg.hwnd, EM_SETSEL, P1 + D, P1 + D);
SendMessage(Msg.hwnd, EM_GETSEL, WPARAM(@P2), 0);
if P1 = P2 then
begin
GetWindowRect(IPEdit, R1);
GetWindowRect(Msg.hwnd, R2);
Inc(D, (R2.Left - R1.Left) div ((R1.Right - R1.Left) shr 2));
SendMessage(IPEdit, IPM_SETFOCUS, (D + 4) mod 4, 0);
end;
Handled := True;
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, CommCtrl, StdCtrls, ComCtrls, ToolWin;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure AppMsg(var Msg: TMsg; var Handled: Boolean);
end;
var
Form1: TForm1;
IPEdit: HWND;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := AppMsg;
IPEdit := CreateWindow('SysIPAddress32', 'IPEdit', WS_CHILD or WS_VISIBLE,
100, 100, 121, 21, Handle, 0, HInstance, nil);
end;
procedure TForm1.AppMsg(var Msg: TMsg; var Handled: Boolean);
var
P1, P2, D: Integer;
R1, R2: TRect;
begin
if (GetParent(Msg.hwnd) = IPEdit) and (Msg.message = WM_KEYDOWN)
and (Msg.wParam in [VK_LEFT, VK_RIGHT]) then
begin
D := Msg.wParam - 38;
SendMessage(Msg.hwnd, EM_GETSEL, WPARAM(@P1), 0);
SendMessage(Msg.hwnd, EM_SETSEL, P1 + D, P1 + D);
SendMessage(Msg.hwnd, EM_GETSEL, WPARAM(@P2), 0);
if P1 = P2 then
begin
GetWindowRect(IPEdit, R1);
GetWindowRect(Msg.hwnd, R2);
Inc(D, (R2.Left - R1.Left) div ((R1.Right - R1.Left) shr 2));
SendMessage(IPEdit, IPM_SETFOCUS, D, 0);
end;
Handled := True;
end;
end;
end.