
GetWindowLong返回的是int类型,你这用的是long,这是P/Invoke,内存并不会清零。你改成int 就可以了。从GetWindowLong声明处改
你把这个值转成十六进制显示,就很容易发现问题了。
获取系统当前进程名和进程ID,注意在编写本单元时,应注意引用"TLHelp32"单元"use TLHelp32"。
LISTVIEW中:
源代码如下:
var
Form1: TForm1;
Summ: Word;
implementation{$R dfm}
procedure TForm1N2Click(Sender: TObject);
var
ContinueLoop: BOOL;
NewItem: TListItem;
begin
ListView1ItemsBeginUpdate;
ListView1ItemsClear;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
//CreateToolhelp32Snapshot函数得到进程快照
FProcessEntry32dwSize := Sizeof(FProcessEntry32); //初始化
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
//Process32First 得到一个系统快照里第一个进程的信息
Summ := 0;
while ContinueLoop do
begin
Summ := Summ + 1;
NewItem := ListView1ItemsAdd; //在ListView1显示
NewItemImageIndex := -1;
NewItemCaption := ExtractFileName(FProcessEntry32szExeFile);//进程名称
NewItemsubItemsAdd(FormatFloat('00', Summ));//序号
NewItemsubItemsAdd(IntToStr(FProcessEntry32th32ProcessID));//进程ID
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
ListView1ItemsEndUpdate;
end;
LISTBOX中:
源代码如下:
procedure TForm1Button1Click(Sender: TObject);
var
ProcessName : string; //进程名
ProcessID : integer; //进程表示符
i : integer;
ContinueLoop:BOOL;
FSnapshotHandle:THandle; //进程快照句柄
FProcessEntry32:TProcessEntry32; //进程入口的结构体信息
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照
FProcessEntry32dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程
//循环例举
while ContinueLoop do
begin
ProcessName := FProcessEntry32szExeFile;
ProcessID := FProcessEntry32th32ProcessID;
ListboxItemsadd('应用程序名 :'+ProcessName +'#进程ID:'+ inttostr(ProcessID));
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
实现原理是启动一个应用程序,通过ProcessID得到窗体句柄,然后对其设定父窗体句柄为本程序某控件句柄(本例是窗体内一个Panel的句柄),这样就达成了内嵌的效果。
新建窗体,上面放置一个Panel控件,名为pnlApp,然后按下面代码编写:
unit frmTestEmbedApp;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
pnlApp: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hWin: HWND = 0;
implementation
{$R dfm}
type
// 存储窗体信息
PProcessWindow = ^TProcessWindow;
TProcessWindow = record
ProcessID: Cardinal;
FoundWindow: hWnd;
end;
// 窗体枚举函数
function EnumWindowsProc(Wnd: HWND; ProcWndInfo: PProcessWindow): BOOL; stdcall;
var
WndProcessID: Cardinal;
begin
GetWindowThreadProcessId(Wnd, @WndProcessID);
if WndProcessID = ProcWndInfo^ProcessID then begin
ProcWndInfo^FoundWindow := Wnd;
Result := False; // 已找到,故停止 EnumWindows
end
else
Result := True; // 继续查找
end;
// 由 ProcessID 查找窗体 Handle
function GetProcessWindow(ProcessID: Cardinal): HWND;
var
ProcWndInfo: TProcessWindow;
begin
ProcWndInfoProcessID := ProcessID;
ProcWndInfoFoundWindow := 0;
EnumWindows(@EnumWindowsProc, Integer(@ProcWndInfo)); // 查找窗体
Result := ProcWndInfoFoundWindow;
end;
// 在 Panel 上内嵌运行程序
function RunAppInPanel(const AppFileName: string; ParentHandle: HWND; var WinHandle: HWND): Boolean;
var
si: STARTUPINFO;
pi: TProcessInformation;
begin
Result := False;
// 启动进程
FillChar(si, SizeOf(si), 0);
sicb := SizeOf(si);
siwShowWindow := SW_SHOW;
if not CreateProcess(nil, PChar(AppFileName), nil, nil, true,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, si, pi) then Exit;
// 等待进程启动
WaitForInputIdle(pihProcess, 10000);
// 取得进程的 Handle
WinHandle := GetProcessWindow(pidwProcessID);
if WinHandle > 0 then begin
// 设定父窗体
WindowsSetParent(WinHandle, ParentHandle);
// 设定窗体位置
SetWindowPos(WinHandle, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
// 去掉标题栏
SetWindowLong(WinHandle, GWL_STYLE, GetWindowLong(WinHandle, GWL_STYLE)
and (not WS_CAPTION) and (not WS_BORDER) and (not WS_THICKFRAME));
Result := True;
end;
// 释放 Handle
CloseHandle(pihProcess);
CloseHandle(pihThread);
end;
procedure TForm1FormClose(Sender: TObject; var Action: TCloseAction);
begin
// 退出时向内嵌程序发关闭消息
if hWin > 0 then PostMessage(hWin, WM_CLOSE, 0, 0);
end;
procedure TForm1FormCreate(Sender: TObject);
const
App = 'C:\Windows\Notepadexe';
begin
pnlAppAlign := alClient;
// 启动内嵌程序
if not RunAppInPanel(App, pnlAppHandle, hWin) then ShowMessage('App not found');
end;
procedure TForm1FormResize(Sender: TObject);
begin
// 保持内嵌程序充满 pnlApp
if hWin <> 0 then MoveWindow(hWin, 0, 0, pnlAppClientWidth, pnlAppClientHeight, True);
end;
end
以上就是关于C#中用GetWindowLong获取其它窗口中控件的ID的问题全部的内容,包括:C#中用GetWindowLong获取其它窗口中控件的ID的问题、listview中如何获取控件id、delphi 如何获取其它应用程序窗体中的所有控件句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)