EnumerateWindows在Delphi 7中有效,但在Delphi 10.3中无效 [英] EnumerateWindows works in Delphi 7 but not in Delphi 10.3

查看:83
本文介绍了EnumerateWindows在Delphi 7中有效,但在Delphi 10.3中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级Delphi后出现问题.

I have a problem after upgrading Delphi.

我有此代码:

function EnumerateWindows(hWnd: HWND; lparam:LPARAM):Bool;
var
  ClassName, TheText : Array [0..255] of char;
  sName : string;
begin
  Application.ProcessMessages;
  GetClassNAme(hWnd,Classname, 255);
  if GetWindowText(hWnd, TheText, 255) > 0 then
  begin
    sName := StrPas(TheText);
    if pos('NOTEPAD',UpperCase(sName)) > 0 then
      postMessage(FindWindow(ClassName, TheText), WM_CLOSE, 0, 0);
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  EnumWindows(@EnumerateWindows,0);
end;

在Delphi 7中,只要有NOTEPAD标题,上面的代码就可以正常工作,该程序将终止其进程,但是当我尝试使用Selphi 10.3时,上面的代码无法正常工作.当我打开记事本时,它不会终止进程(编译器中没有错误).有什么方法可以使代码在Delphi 10.3中工作?

In Delphi 7 the code above is working perfectly anytime there is NOTEPAD title and the program will kill its process but when I tried to use Selphi 10.3 the code above is not working. When i open Notepad it does not kill the process (there is no error in compiler). Is there any way to make the code work in Delphi 10.3?

推荐答案

您必须使用

  • 类型 AnsiChar AnsiString
  • 函数 GetClassNameW() GetWindowTextW() StrPasW() FindWindowW()>

...因为默认情况下 Delphi 7 使用ANSI,默认情况下WIDE使用 Delphi 10 .您还可以通过使用所有WIDE函数以及类型的 WideChar WideString 立即在 Delphi 7 中使用Unicode.根据雷米(Remy)的评论,先前的文本是错误的.

...because Delphi 7 uses by default ANSI and Delphi 10 by default WIDE. You could also use Unicode in Delphi 7 right away by using all the WIDE functions and the types WideChar and WideString. the previous text is wrong as per Remy's comment.

此外,没有进程被杀死: WM_CLOSE 仅请求关闭窗口-不能保证该进程真正关闭窗口(例如,对于 Notepad 而言,如果您是否要保存所做的编辑,仍可能会出现一个问题对话框),或者该过程在关闭该窗口后也将结束-进程可以不使用窗口而运行.要杀死一个进程,您必须使用 TerminateProcess() .

Furthermore no process is killed: WM_CLOSE merely requests a window to be closed - there is neither a guarantee that the process really closes that window (i.e. for Notepad it can still result in a question dialog if you want to save your edits or not), nor that the process would end after closing that window - processes can run without windows. To kill a process you have to use TerminateProcess().

这篇关于EnumerateWindows在Delphi 7中有效,但在Delphi 10.3中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆