
获得句柄这个我不知道,不好意思,但是,如果你想获得ToolStrip内combox的对象,然后进行 *** 作的话,可已这用来 *** 作。
ToolStripComboBox TCB = (ToolStripComboBox)DemoToolItems["DemoHandl2"];
DemoHandl2 为ToolStrip 内某个下拉框控件的名称的属性。
其他的类似,但是你要知道你要获得的是什么类型的控件才可以强转哈,不然就报错咯!
OptionExplicitPrivateDeclareFunctionFindWindowLibuser32AliasFindWindowA(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsLongPrivateDeclareFunctionGetWindowTextLibuser32AliasGetWindowTextA(ByValhwndAsLong,ByVallpStringAsString,ByValcchAsLong)AsLongPrivateDeclareFunctionGetWindowLibuser32(ByValhwndAsLong,ByValwCmdAsLong)AsLongPrivateDeclareFunctionGetDesktopWindowLibuser32()AsLongPrivateDeclareFunctionGetClassNameLibuser32AliasGetClassNameA(ByValhwndAsLong,ByVallpClassNameAsString,ByValnMaxCountAsLong)AsLongPrivateConstGW_HWNDFIRST=0PrivateConstGW_HWNDNEXT=2PrivateConstGW_CHILD=5PrivateSubCommand1_Click()CallList1_ClickEndSubPrivateSubCommand2_Click()DimhwndAsLongDimsAsString,tAsStringList1Clearhwnd=GetDesktopWindow()s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem桌面:&hwnd&类名:&s&标题:&t&vbCrLfhwnd=GetWindow(hwnd,GW_CHILDOrGW_HWNDFIRST)s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem窗口:&hwnd&类名:&s&标题:&t&vbCrLfWhilehwnd<0hwnd=GetWindow(hwnd,GW_HWNDNEXT)s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem窗口:&hwnd&类名:&s&标题:&t&vbCrLfWendEndSubPrivateSubForm_Load()Command1Caption=获取所有控件Command2Caption=遍历所有窗体EndSubPrivateSubEnumAllHandles(ByValhwndAsLong)DimhnAsLongDimfirsthdAsLongDimsAsString,tAsStringfirsthd=GetWindow(hwnd,GW_CHILD)firsthd=GetWindow(firsthd,GW_HWNDFIRST)hn=firsthdDoWhilehn<0s=String(256,Chr(0))GetClassNamehn,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthn,t,255t=Replace(t,Chr(0),)Text1Text=Text1Text&句柄:&hn&父句柄:&hwnd&类名:&s&标题:&t&vbCrLfTreeView1NodesAddk&hwnd,tvwChild,k&hn,句柄:&hn&类名:&s&标题:&tEnumAllHandleshnhn=GetWindow(hn,GW_HWNDNEXT)Ifhn=firsthdThenExitDoLoopEndSubPrivateSubList1_Click()IfList1ListIndex=-1ThenExitSubTreeView1NodesClearTreeView1NodesAdd,,k&Trim(Str(Val(Mid(List1Text,4)))),List1TextText1Text=EnumAllHandlesVal(Mid(List1Text,4))TreeView1Nodes(k&Trim(Str(Val(Mid(List1Text,4)))))Expanded=TrueEndSub'添加两个按钮一个文本框一个列表框和一个树形图
实现原理是启动一个应用程序,通过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
看看你身后的质疑,使用ATL。
HWND HWND函数GetDlgItem(IDC_LIST)在,
CListCtrl的LC(HWND); / /或者使用附加要求。
在手柄上一般使用ATL,MFC类指针一般。
例如,函数GetDlgItem返回HWND,ATL,MFC返回的CWnd
看了你后面的追问,用到了ATL。
HWND hWnd = GetDlgItem(IDC_LIST);
CListCtrl lc(hWnd); //或者用Attach也行。
ATL中一般使用句柄,MFC中一般是类指针。
比如GetDlgItem,ATL返回HWND,MFC返回CWnd
以上就是关于WINFORM 如何获取toolStrip内控件的句柄全部的内容,包括:WINFORM 如何获取toolStrip内控件的句柄、vb中 如何获得窗体中所有控件的句柄、delphi 如何获取其它应用程序窗体中的所有控件句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)