PDA

View Full Version : كار با HANDLE بعضي از OBJECT هاي خاص



lord_viper
چهارشنبه 08 تیر 1384, 06:20 صبح
با سلام
با تابع findwindowexمیشه پنجره childرو در formوالد پیدا کرد
اگه تو برنامه 7 تا buttonداشته باشیم و بخواهیم هندل button 3 5 7 رو پیدا کنیم با چه تابعی با ید ادرس دهی بشه؟فکر کنم enum child windowباشه کسی تابع کاملشو داره با پارامترهاش؟
با تشکر

vcldeveloper
چهارشنبه 08 تیر 1384, 16:06 عصر
تابع EnumChildWindows به همراه پارامترهاش در Windows SDK (به همراه Help دلفی روی سیستمت نصب شده) توضیح داده شده.

lord_viper
پنج شنبه 09 تیر 1384, 07:50 صبح
ببخشید اقای کشاورز من window sdk ندارم و عین همین کلمه رو در مدلهای مختلف نوشتم نشد اگه لطف کنین و تابع کاملش رو بنویسین ممنون میشوم

vcldeveloper
جمعه 10 تیر 1384, 03:27 صبح
من window sdk ندارم و عین همین کلمه رو در مدلهای مختلف نوشتم نشد اگه لطف کنین و تابع کاملش رو بنویسین ممنون میشوم
از Windows SDK:


The EnumChildWindows function enumerates the child windows that belong to the specified parent window by passing the handle of each child window, in turn, to an application-defined callback function. EnumChildWindows continues until the last child window is enumerated or the callback function returns FALSE.

BOOL EnumChildWindows(

HWND hWndParent, // handle to parent window
WNDENUMPROC lpEnumFunc, // pointer to callback function
LPARAM lParam // application-defined value
);


Parameters

hWndParent

Identifies the parent window whose child windows are to be enumerated.

lpEnumFunc

Points to an application-defined callback function. For more information about the callback function, see the EnumChildProc callback function.

lParam

Specifies a 32-bit, application-defined value to be passed to the callback function.



Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.

Remarks

The EnumChildWindows function does not enumerate top-level windows owned by the specified window, nor does it enumerate any other owned windows.
If a child window has created child windows of its own, this function enumerates those windows as well.
A child window that is moved or repositioned in the Z order during the enumeration process will be properly enumerated. The function does not enumerate a child window that is destroyed before being enumerated or that is created during the enumeration process.

This function is more reliable than calling the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.

lord_viper
شنبه 11 تیر 1384, 00:06 صبح
ببخشید اقای کشاورز خروجی این تابع رو چطور به تابع findeindowexپاس دم؟

vcldeveloper
شنبه 11 تیر 1384, 02:56 صبح
ببخشید اقای کشاورز خروجی این تابع رو چطور به تابع findeindowexپاس دم؟
لازم نیست خروجی این تابع رو (که از نوع BOOL هست) به FindWindowEx پاس بدید، شما باید برای این تابع یک Callback function تعریف کنید و پردازش هاتون بر روی هر یک از پنجره های Child رو در این تابع انجام بدید.

مثال:
کد زیر نام کلاس و متن هر یک از پنجره های فرزند فرم برنامه رو در اخل یک ListBox لیست می کنه:


//Callback function. It is called by EnumChildWindows on each child window enumeration
//You can do ur processes on each child window here
function EnumChildProc(h: HWND; List: TStringList): BOOL;stdcall;
var
Temp: string; //Text buffer
begin
//Get memory for the buffer
SetLength(Temp,255);
GetClassName(h,PAnsiChar(Temp),Length(Temp));
List.Add('Class = ' + Temp);
//Get Window's text
SendMessage(h,WM_GETTEXT,Length(Temp),Integer(PAns iChar(Temp)));
List.Add('Text = ' + Temp);
List.Add('-------');
//Returning FALSE results in stopping enumeration
Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//Enumerate child windows.
//Handle = Handle of the current form.
//@EnumChildProc = Address of the callback function.
//Integer(ListBox1.Items) = You can pass an arbitrary integer parameter to the callback function.
// Here I sent ListBox1.Items to the callback function.
EnumChildWindows(Handle,@EnumChildProc,Integer(Lis tBox1.Items));
end;


موفق باشید

lord_viper
دوشنبه 10 مرداد 1384, 07:04 صبح
با سلامی دوباره خدمت اقای کشاورز شرمنده اگه به این موضوع گیر میدم
من نمیخوام که همه childها رو بشمرم ببینید من یه فورم دارم با 6 تاbutton و 3تاeditboxمن کارم با butten 1,3,6وeditbox 1,2هستش میخواستم ببینم که این تابع رو چطور مقدار دهی کنم که هندل این childهای که مد نظرمه رو پیدا کنم راه سادهتری هم واسه این کارهست یا نه؟لطفا راهنمایی کنید
با تشکر

vcldeveloper
سه شنبه 11 مرداد 1384, 03:08 صبح
اگر نام هر کدوم از کامپوننت هایی که میخواید بهشون دسترسی داشته باشید رو میدونید:


var
btn : TButton;
begin
btn := TButton(Form1.FindComponent('Button1'));
btn.Caption := 'Hi';
end;

یا


var
h : THandle;
begin
h := FindWindowEx(Self.Handle,0,nil,'Button1');
if h > 0 then
begin
SetWindowText(h,'Hi');
end;
end;

اگر بعضی از خصوصیات کامپوننت مورد نظر رو میدونید:


var
i : integer;
begin
for i := 0 to Form1.ControlCount-1 do
if Controls[i].ClassName = 'TButton' then
if TButton(Controls[i]).Caption = 'Button1' then
TButton(Controls[i]).Caption := 'Hi';
end;

البته می تونید از EnumChildWindows هم برای رسیدن به هدفتون استفاده کنید.