实现原理是启动一个应用程序,通过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
使用EnumWindows和EnumChildWindows函数以及相对的回调函数EnumWindowsProc和EnumChildWindowsProc获取所有顶层窗口以及它们的子窗口(有些窗口做了特殊处理,比如QQ是不能通过这个方法获得的)!
分成两个问题,到底哪个你不会?
Winform里多个窗体之间如何传递变量?
Winform里如何获取窗体相对于桌面(屏幕)的坐标?
一般用FindWindow。。。
Delphi中获取其它进程的窗口句柄,在Delphi中获取其它进程的窗口句柄,绝大部分人首先想到的会使用:FindWindow或者用GetWindow来遍历查找,如:
Delphi/Pascal code
handle := FindWindow(nil,PChar('窗口的标题'));
或者:
Delphi/Pascal code
procedure TForm1Button1Click(Sender: TObject);
var
hCurrentWindow: HWnd;
WndText:String;
begin
hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);
while hCurrentWindow <> 0 do
begin
WndText:=GetWndText(hCurrentWindow);
if UpperCase(WndText)='窗口的标题' then begin
end;
hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
end;
因为目前网络上绝大部分的代码都是介绍用这两种方法取得其它进程的窗口句柄。虽这两种方法都可以达到查找其它进程的窗口句柄的目的,但本人认为这两都方法存在较大的弊端。因为这两种方法都是根据其它进程的标题来查找的,如果其它进程的标题在运行时不断的发生变化,那么这两种方法就无法没办法用了。
介绍第三种通过进程的文件名来查找窗口句柄。首先通过进程快照得到要查找的进程ID(ProcessId),其次,再跟据ProcessId获取进程的窗口句柄。以下为本文章的代码:
Delphi/Pascal code
uses TLHelp32;
procedure TForm1Button1Click(Sender: TObject);
var
ProcessName : string; //进程名
FSnapshotHandle:THandle; //进程快照句柄
FProcessEntry32:TProcessEntry32; //进程入口的结构体信息
ContinueLoop:BOOL;
MyHwnd:THandle;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照
FProcessEntry32dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程
//循环例举
while ContinueLoop do
begin
ProcessName := FProcessEntry32szExeFile;
if(ProcessName = '要找的应用程序名exe') then begin
MyHwnd := GetHWndByPID(FProcessEntry32th32ProcessID);
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle); // 释放快照句柄
end;
//跟据ProcessId获取进程的窗口句柄
function TForm1GetHWndByPID(const hPID: THandle): THandle;
type
PEnumInfo = ^TEnumInfo;
TEnumInfo = record
ProcessID: DWORD;
HWND: THandle;
end;
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
var
PID: DWORD;
begin
GetWindowThreadProcessID(Wnd, @PID);
Result := (PID <> EIProcessID) or
(not IsWindowVisible(WND)) or
(not IsWindowEnabled(WND));
if not Result then EIHWND := WND;
end;
function FindMainWindow(PID: DWORD): DWORD;
var
EI: TEnumInfo;
begin
EIProcessID := PID;
EIHWND := 0;
EnumWindows(@EnumWindowsProc, Integer(@EI));
Result := EIHWND;
end;
begin
if hPID<>0 then
Result:=FindMainWindow(hPID)
else
Result:=0;
end;
我说的是组件的句柄,不是窗体的句柄
findwindowex();获取指定句柄窗口下的子控件,当然是有句柄的控件
窗体的句柄都知道,还能不知道里面组件的句柄?——windows标准组件
procedure TForm1Button2Click(Sender: TObject);
var
canvas1: TCanvas;
begin
Canvas1 := TCanvasCreate;
//这是OK。这真要感谢baidu,google
canvas1Handle := GetDc(panel1Handle);
canvas1TextOut(1,1,'hello');
canvas1Free ;
Form1CanvasTextOut(10, 10, 'fff');
end;
有的组件是没有句柄的
ShowMessage(IntToStr(TWinControl(Form1FindChildControl('Panel1'))Handle));
首先,你想要在某个窗口做一些事情,你就得让 *** 作系统知道你是在哪一个窗口做这些事情,而窗口的句柄就能起到识别哪一个窗口的作用;获取窗口句柄的方法(或者说是函数)有很多,有的函数是获得当前主窗口的句柄,有的函数获得子窗口的句柄,还有的函数可以获得你指定的那个窗口的句柄,你想获得哪个窗口的句柄就调用相应的函数就可以了; 然后,获得窗口句柄以后,你就可以利用这个窗口句柄做你那些想做的事情了。比如,设置窗口的风格,改变窗口的标题,隐藏这个窗口等等,如果你不知道能做什么,没事,等你实际用到的时候,需要你在某个窗口做事情的时候,你就知道了该做什么事情了。 希望对你有帮助。
以上就是关于delphi 如何获取其它应用程序窗体中的所有控件句柄全部的内容,包括:delphi 如何获取其它应用程序窗体中的所有控件句柄、Windows服务怎么获取窗口句柄、C# 通过窗口句柄获取窗口在桌面坐标位置等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)