Solved

C++Builder: How to dynamically create a control using (HWND ParentWindow) constructor?

Posted on 2000-03-09
7
600 Views
Last Modified: 2010-04-04
The documentation says that the ParentWindow constructor is used when the control, say a TButton, is parented by a non-VCL window.

What I really wanna do is to put a TButton on a MS Visuall C++ CDialog.
But even when I try to dynamically create a TButton on my TForm within C++Builder using (HWD ParentWindow), I cannot get the button to show.

I know that normally we have to set the parent for the control for it to show. But when using ParentWindow, what do I need to do?

Thanks for a quick answer.

Chris
0
Comment
Question by:chrisyap
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:yk030299
ID: 2603287
it is Delphi Forum. sorry I can't answer now before I study CB.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2603368
Hi
it might be just the order in which events are done,look at this delphi example:
it will be easy enough to change to c++ builder,it puts a tbutton on the taskbar (by the clock).
so as long as you know correct hwnd of this dialog you should be able to do it.


procedure TForm1.FormCreate(Sender: TObject);
var h,hc : HWND;
begin
h := FindWindow('Shell_TrayWnd',nil);
hc := FindWindowEx(h,0,'TrayNotifyWnd',nil);
cc := TEdit.Create(nil);
cc.Width := 60;
cc.Height := 30;
cc.Ctl3D := true;
cc.Visible := true;
cc.ParentWindow := hc;
cc.OnClick := myClick;
cc.BringToFront;
end;

procedure tform1.myClick(Sender : tobject);
begin
Showmessage('Clicked');
end;


Regards Barry
0
 
LVL 17

Expert Comment

by:inthe
ID: 2603372
sorry it puts a tedit on the taskbar not a tbutton,but shouldnt matter both should work similar.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:chrisyap
ID: 2603752
Thanks, Barry.
At least now I know that I'm on the right path.

One more thing:
How the heck do you use FindWindow?
Where can we find class names like 'Shell_TrayWnd'?
What class name should I use if I wanna find my form or my CDialog?

Regards,
Chris
0
 

Author Comment

by:chrisyap
ID: 2603760
sorry, Barry. Didn't mean to reject your answer like that. Now only if you could answer me again....apologies.

Chris
0
 
LVL 17

Accepted Solution

by:
inthe earned 200 total points
ID: 2605881
hi,

to find window class names you can use several differen utilities:
spy++ from microsoft (comes with some of there apps(ms visual c++ etc)
you can create your own program to do it in vb:
http://support.microsoft.com/support/kb/articles/Q112/6/49.asp

you can use winsight32 that comes with some versions of delphi & c++ builder.
or you can use sinfo :
http://lcpx07.lc.ehu.es/JMA/win95.html
(sinfo is very useful utility that i really recommend)

to use findwindow there are a few ways.
{note im not sure off hand but nil may be null in c++ builder}
if you know theclassname of window:
findwindow(Classname,nil);
if you know the caption of the window:
findwindow(nil,WindowCaption);
if you know both :
findwindow(classname,Windowcaption);

this is not so good because notepads main classname is the same as the program name(this is unusual) anyway here an example:
var
h : hwnd;
begin
h := findwindow('notepad','notepad - untitled document');

note if you want to get to the text control inside notepad this is second window level down so you start using findwindowex() passing like:
var
h : hwnd;
begin
h := findwindow('notepad',nil);
h := findwindowex(h,0,'edit',nil);

('edit' is class name of the text control of notepad again a bad example not to be confused with TEdit ,i guess you understand ;-)
we passed the parent hwnd first,next param is always 0 (in delphi so you have to test that in c++)but then the class name were after and you could also pass last param as window caption if one exists but usualy not on child windows so again an nil/null is passed

if the window your after is 4-5 levels down you can keep using the findwindowex until proper window is found.
here is from win32.hlp:

HWND FindWindowEx(

    HWND hwndParent,      // handle to parent window
    HWND hwndChildAfter,      // handle to a child window
    LPCTSTR lpszClass,      // pointer to class name
    LPCTSTR lpszWindow      // pointer to window name
   );      
 

Parameters

hwndParent

Identifies the parent window whose child windows are to be searched.
If hwndParent is NULL, the function uses the desktop window as the parent window. The function searches among windows that are child windows of the desktop.

hwndChildAfter

Identifies a child window. The search begins with the next child window in the Z order. hwndChildAfter must be a direct child window of hwndParent, not just a descendant window.
If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.
Note that if both hwndParent and hwndChildAfter are NULL, the function searches all top-level windows.

lpszClass

Points to a null-terminated string that specifies the class name or is an atom that identifies the class-name string. If this parameter is an atom, it must be a global atom created by a previous call to the GlobalAddAtom function. The atom, a 16-bit value, must be placed in the low-order word of lpszClass; the high-order word must be zero.

lpszWindow

Points to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.

 

Return Values

If the function succeeds, the return value is the handle to the window that has the specified class and window names.



well hope that explained ok,
ignore if its a bit erratic in places.
if you download sinfo it will help you understand the class name stuff better as it adds every windows class name to a tree view and when a window is selected via mouse it gives you the details etc..

Regards Barry
0
 

Author Comment

by:chrisyap
ID: 2610619
Thanks Barry. Will try it out!

Regards,
Chris
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Correct Component for Shopping Cart. 2 95
can't find the executable in Simulator 1 77
SUM 2 INTEGER ARRAYS INTO 1 10 93
CheckListBox usage 3 48
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now