在 Delphi 中捕获窗口名称 | 珊瑚贝

Capture windows name in Delphi


我在delphi中做一个捕获活动窗口的程序问题是代码没有做我想要的,我想要的是一个计时器在适当的时候识别活动窗口,以便附加活动窗口的名称而不是永远等到你看到一个不同的名字,问题是它总是显示没有做我想做的事。
如果问题不是我做的很好验证。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
procedure TForm1.Timer4Timer(Sender: TObject);

var
  ventana1: array [0 .. 255] of char;      
  nombre1: string;
  nombre2: String;

begin

  GetWindowText(GetForegroundWindow, ventana1, SizeOf(ventana1));

  nombre1 := ventana1;

  if not(nombre1 = nombre2) then
  begin
    nombre2 := nombre1;
    Memo1.Lines.Add(nombre2);
  end;

end;

  • 你能提供一个错误的例子和你期望的例子吗?”节目没有按照我的意愿行事”不足以让我们为您提供帮助。


你什么都不做初始化nombre2,所以nombre1 = nombre2永远不可能是真的。 nombre2 总是 nil.

在 if 语句中设置 nombre2 := nombre1; 也是没有意义的,因为当过程退出时该值立即丢失;定时器事件的下一次调用以 nombre2 = nil 重新开始,因为 nombre2 是一个新的局部变量,每次进入过程时都会初始化为 nil,每次退出过程时都会释放它。

将 nombre2 移动到表单实例变量中:

1
2
3
4
5
6
7
8
type
  TForm1 = class(TForm)
    // normal declarations here
    procedure Timer4Timer(Sender: TObject);
  private
    Nombre2: string;  // I’d use LastWindowName myself. :-)
 
  end;

现在,在你的计时器事件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
procedure TForm1.Timer4Timer(Sender: TObject);
var
  ventana1: array [0 .. 255] of char;      
  nombre1: string;  // I’d use ‘NewName’
begin
  GetWindowText(GetForegroundWindow, ventana1, SizeOf(ventana1));

  nombre1 := ventana1;

  if not(nombre1 = nombre2) then  // Now nombre2 still has the last value,
  begin                           // because it’s no longer a local variable
    nombre2 := nombre1;           // Store new”last window name”
    Memo1.Lines.Add(nombre2);
  end;
end


来源:https://www.codenong.com/18679207/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9571.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?