Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding and controlling other windows

Posted on 1999-07-12
24
Medium Priority
?
137 Views
Last Modified: 2010-05-02
How to programmatically gain control over the state of other windows on the screen using Win32API calls from within VB6.
0
Comment
Question by:psiegers
[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
  • 9
  • 7
  • 7
  • +1
24 Comments
 
LVL 1

Expert Comment

by:rondeauj
ID: 1523848
What are you wanting to control?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523849
This example will give you a lot of what you want.

Create a big form with a button and a timer, and paste in the following code.

When the project is run, click on a window from another application and then click the command button to see what happens.

Let me know if you need to know more...
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523850
Option Explicit

Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_MINIMIZE = 6
Private Const SW_MAXIMIZE = 3
Private Const SW_RESTORE = 9

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long

Dim Hw As Long
Dim lResult As Long

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_STYLE = (-16)

Private Const WS_POPUP = &H80000000

'The following constants are no longer used, but I'll leave them here for reference _
 purposes
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_NOPARENTNOTIFY = &H4&
Private Const GWL_HWNDPARENT = (-8)


Private Const WS_MAXIMIZEBOX = &H10000 'Enables/Disables maximise button
Private Const WS_MINIMIZEBOX = &H20000 'Enables/Disables minimise button
'If both are switched "off", both minimise and maximise buttons disappear.

Private Const WS_THICKFRAME = &H40000
'Enables/Disabled sizing

Private Const WS_SYSMENU = &H80000
'Shows/Removes control box

Private Const WS_HSCROLL = &H100000
'Shows a horizontal scroll bar

Private Const WS_VSCROLL = &H200000
'shows a vertical scroll bar

 Private Const WS_BORDER = &H800000
Private Const WS_CAPTION = &HC00000
'Both constants seem to be based on the value &H800000. It's not clear how they work. _
 when either &H400000 or &H800000 are removed, the form's caption disappears when _
 resizing.

Private Const WS_VISIBLE = &H10000000

Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private colChildren As New Collection

Private Sub Command4_Click()

SendKeys ("%{ESC}")
DoEvents
Timer1.Interval = 1
Timer1.Enabled = True

End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Dim lngCount As Long

For lngCount = 1 To colChildren.Count
    SetParent colChildren(lngCount), 0
Next

End Sub

Private Sub Form_Resize()

Command4.Left = ScaleWidth - 120 - Command4.Width
Command4.Top = ScaleHeight - 120 - Command4.Height

End Sub

Private Sub Timer1_Timer()

Hw = GetForegroundWindow
Timer1.Enabled = False

SetParent Hw, Me.hwnd

Dim lngStyle As Long
lngStyle = GetWindowLong(Hw, GWL_STYLE)
SetWindowLong Hw, GWL_STYLE, lngStyle Or WS_POPUP

colChildren.Add Hw

Me.WindowState = vbNormal

End Sub

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:caraf_g
ID: 1523851
Sorry - as you can see I called the command button "Command4", so either replace all instances with "Command1" or make sure your command is called the same :-)
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523852
PS - you can use the ShowWindow API with the SW_... constants to maximize, minimize or restore other windows in your application.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523853
For example, this alternative code for the timer event will maximize the window you clicked on:

Private Sub Timer1_Timer()

Hw = GetForegroundWindow
Timer1.Enabled = False

ShowWindow Hw, SW_MAXIMIZE

colChildren.Add Hw

Me.WindowState = vbNormal

End Sub

0
 

Author Comment

by:psiegers
ID: 1523854
I would like more information about the following:
suppose I have an application with 3 windows, of which two are IE4/5 browser windows, and one stand-alone application window. If I click on a button in the first browser window, then the second comes. How to control this window so that it doesn't get duplicated or triplicated every time I click on the first windows button?
Then, clicking a button on the second browser window, my stand-alone application opens. From here, I don't want users to be able to return to any of the browser windows. How to archieve this?
Giving a more detailed solution of these two questions will fully qualify for the amount of 200 points.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523855
Ah, I don't know.... That's a bit beyond me, I must admit. Your best bet is to reject the answer and to hope for another expert to be able to help you further.
0
 

Author Comment

by:psiegers
ID: 1523856
I would like more information about the following:
suppose I have an application with 3 windows, of which two are IE4/5 browser windows, and one stand-alone application window. If I click on a button in the first browser window, then the second one opens. Q1: How to control this window so that it doesn't get duplicated or triplicated every time I click on the first windows button?
Then, clicking a button on the second browser window, my stand-alone application opens. From here, I don't want users to be able to return to any of the browser windows. Q2: How to archieve this?
Giving a more detailed solution of these two questions will fully qualify for the amount of 200 points.

0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1523857
you just want to close the browser windows?? or hide them?
0
 

Author Comment

by:psiegers
ID: 1523858
I want to be able to use the windows like in a normal VB application, i.e. a kind of Modal form, which prevents the return to the previous ones.
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1523859
can you maybe set the active 'Modal' one to be topmost?
0
 

Author Comment

by:psiegers
ID: 1523860
My application consists of three separate windows:
1. IE browser window
2. another IE browser window
3. an VB application window.
These windows do not have the control VB normally offers, and I want to know if it is possible to somehow simulate the VB behaviour using the win32api.
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1523861
that part was clear the first time, I am just wondering kind of control you need.  There are api calls to simulate just about any of VB's behavior's, could you you be a bit more specific on which other behavior's you want to immitate besides modality?
0
 

Author Comment

by:psiegers
ID: 1523862
Another behaviour which I want to control is that only one instance of every window is running at any time.
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1523863
Your best bet is to create your own browser window using the webbrowser control.  I can email you a sample if you need more detail on how to do this.
0
 

Author Comment

by:psiegers
ID: 1523864
Do you think there is no solution to my problem then?
If you are, sure I would like to know more about that control, but I would like to know first if it is a big change to use it, because I don't have the time to develope a different application.
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1523865
not really a big change, the sample I'll send you is a fully functioning web browser, in a VB form (hence the control you need)
0
 

Author Comment

by:psiegers
ID: 1523866
OK, I'll agree on that. Please send me the sample then, so that I can decide whether or not to use it.
Anyway, thanks in advance for trying to help me out!
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1523867
Your address??
0
 

Author Comment

by:psiegers
ID: 1523868
psiegers@infosel.com.mx
0
 
LVL 2

Accepted Solution

by:
BeedleGuis earned 800 total points
ID: 1523871
as per my email . . let me know if the dll your creating will work, or if you need more help with it.
0
 

Author Comment

by:psiegers
ID: 1523873
I have accepted an answer to my question and I would like to close it, but my options won't let me.
How do I do that? Anyone can help me?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523874
The question has an Accepted Answer, so it is already closed. You must have done this at an earlier stage!

Once a question is answered, and the answer is accepted, it becomes a "Previously Answered Question - PAQ", and will remain visible for reference by other users.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

670 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