?
Solved

Getting hWnd from titlebar

Posted on 1997-07-24
6
Medium Priority
?
497 Views
Last Modified: 2012-05-04
If I know the title of a program ie. "MS-WORD", is it possible to get the hWnd (window handle) of that application (form[s])?
0
Comment
Question by:PersonX070197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:ESI
ID: 1428826
Yes its possible to retrieve the hWnd just by the caption (or a part of the caption) displayed in the main form.
Is it what u need before I dig in my API docs ?
0
 
LVL 2

Expert Comment

by:vorlon
ID: 1428827
PersonX,
Yes. In order to get the hWnd of the application knowing only it's titlebar, you have to use the FindWindow API function.

Here is some information of that function:
Declare Function FindWindow& Lib "user32" alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName as string)

Description:
Finds the first top level window in the window list that meets the specified conditions.

Return Value:
Long - Handle of the window found. Zero if no window was found. Sets GetLastError

Use in VB:
No problem. One common use of FindWindow is to obtain the handle of the hidden window of class ThunderRTMain that is part of every running Visual Basic executable. You can then use the GetWindowText API to retrieve the window name of this window which is also the appliation title.

Parameters:
lpClassName - String: Pointer to null terminated (C Language) string containing the name of the class of the window, or zero to  accept any class.

lpWindowName - String: Pointer to null terminated (C Language) string containing the window text (or title), or zero to accept any window title.

Hope this helps.
0
 
LVL 3

Expert Comment

by:ESI
ID: 1428828
What is this ThunderRTMain hidden window ? What's the use to get a handle on it ? I've seen too there is a "CONJURE.MAK" hidden window when VB5 is running.
Any infos welcomed !
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:vorlon
ID: 1428829
I have no idea what the CONJURE.MAK hidden window or the ThunderRTMain class do. I simply copied the description of the function from my reference. I've used this function before manny times and it does what it's support to do.

Have you ever noticed that sometimes you can't run multiple VB applications simultaneously? Well maybe these windows/class names have something to do with that.

Just my $0.02.
vorlon
0
 

Author Comment

by:PersonX070197
ID: 1428830
I'm sure that the API you gave me works, but it doesn't seem to work for me... You see, let's say I want to terminate "Notepad" I  would first declare the API (Findwindow and Destroywindow), Find the titlebar of notepad, possibly "Untitled - Notepad" then my code:
x=FindWindow& (0,"Untitled - Notepad"")
x=DestroyWindow (x)
....It should work according to the code you pasted above,  (If I replace the values with all 0's it should work for sure right?
[FindWindow& (0,0) - should return ANY window, well it doesnt)
So am I messing something up here or is it mess'd or what?
0
 
LVL 3

Accepted Solution

by:
wolfcrag earned 200 total points
ID: 1428831
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long

Is this the declaration you used? If not, try it out. If it is,  try String instead of Any in both parameters, and then use the following code:

hwndWin = FindWindow(vbNullString, "Untitled - Notepad")
Throw = DestroyWindow(hwndWin)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 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