应该是为所有的Button1加上caption,但却只变最后一个,还不停的变,请指点(100)

  • 主题发起人 主题发起人 wenqy
  • 开始时间 开始时间
W

wenqy

Unregistered / Unconfirmed
GUEST, unregistred user!
应该是遍历,为所有的Button1加上caption,但却只加最后一个,其caption不断的累加,且不能停止,请指点procedure TForm1.SpeedButton1Click(Sender: TObject);var FormHandle , PanelHanlde , ButtonHandle: THandle; I : Integer;begin FormHandle := FindWindow(nil, PChar('Form1' I := 0; if FormHandle <> 0 then begin ButtonHandle := FindWindowEx(FormHandle, 0, PChar('TButton'), nil); repeat Inc(I); SendMessage(ButtonHandle, WM_SETTEXT, 0, Integer(pchar(IntToStr(I)))); ButtonHandle := FindWindowEx(FormHandle, 0, PChar('TButton'), nil); until ButtonHandle=0; end;end;
 
你的代码有问题,只对最后一个Button进行了循环赋值。
 
而且 ButtonHandle 永不为 0,所以程序进入了死循环。
 
这段代码是网上找的,我怎么就看不出问题!请帮忙修改一下,马上结贴
 
var g_Count: Integer;function EnumChildWindowsProc(AChildHandle: THandle; LParam: Integer): Boolean; stdcall;var Name: array[0..20] of Char; n: Integer; NameStr: string;begin n := GetClassName(AChildHandle, @Name[0], 20); if n > 0 then begin SetString(NameStr, Name, n); if NameStr = 'TButton' then begin Inc(g_Count); SendMessage(AChildHandle, WM_SETTEXT, 0, Integer(PChar(IntToStr(g_Count)))); end; end; Result := True;end;procedure TForm1.btn3Click(Sender: TObject);var FormHandle: THandle;begin FormHandle := FindWindow(nil, PChar('Form1')); g_Count := 0; EnumChildWindows(FormHandle, @EnumChildWindowsProc, 0);end;
 
把楼上的简化一下,你试试:unit SetCaption;interfaceuses Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls;type TForm1 = class(TForm) btn1: TButton; procedure btn1Click(Sender: TObject); end;var Form1: TForm1;implementation{$R *.dfm}function EnumChildWindowsProc(lHwnd, lParam: Integer): Boolean; stdcall;var ClassName: array[0..254] of Char;begin GetClassName(lHwnd, @ClassName, 254); if StrPas(ClassName) = 'TButton' then SendMessage(lHwnd, WM_SETTEXT, 0, Integer(PChar('SSS'))); Result := True;end;procedure TForm1.btn1Click(Sender: TObject);var wHandle: HWND; szText: array[0..254] of Char;begin wHandle := GetWindow(Handle, GW_HWNDFIRST); while (wHandle <> 0) do begin if GetWindowText(wHandle, @szText, 255) > 0 then if StrPas(@szText) = 'TestFrm' then // 根据目标窗体的标题查找该窗体句柄 begin EnumChildWindows(wHandle, @EnumChildWindowsProc, 0); // 枚举目标窗体的控件 Break; end; wHandle := GetWindow(wHandle, GW_HWNDNEXT); end;end;end.
 
把repeat循环里面的ButtonHandle := FindWindowEx(FormHandle, 0, PChar('TButton'), nil);改为ButtonHandle := FindWindowEx(FormHandle, ButtonHandle, PChar('TButton'), nil);看看
 
谢谢三位,接分
 
后退
顶部