how to minimize an already open IE browser through vb.net? - vb.net

I need to know how we can minimize an already open Internet Explorer browser using vb.net. Everywhere i can only find code to minimize the form and not for the web browser. Any help will be appreciated. Thanks in advance.

Here is how to. But remember this is WindowsAPI. You should read, and learn more before doing any serious stuff.
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As ShowWindowCommands) As Boolean
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hWnd As Long = Process.GetProcessesByName("iexplore").First().MainWindowHandle
ShowWindow(hWnd, ShowWindowCommands.ForceMinimize)
End Sub
Enum ShowWindowCommands As Integer
Hide = 0
Normal = 1
ShowMinimized = 2
Maximize = 3
ShowMaximized = 3
ShowNoActivate = 4
Show = 5
Minimize = 6
ShowMinNoActive = 7
ShowNA = 8
Restore = 9
ShowDefault = 10
ForceMinimize = 11
End Enum
End Class
I'll explain.
The import in the first line is needed to use DllImport, which will be used subsequently. The lines of code beginning with <DllImport... imports a function from user32.dll. Since we are going to work with an external application, we take support from WindowsAPI, a set of services provided by windows, to manage that.
The function we're using has the ability to Minimize, Maximize, Hide or Restore an external window. The possible alternatives are listed in the Enum which is at the end of the code. pinvoke.net nicely lists down what they do, if you need a peek.
This code simply assigns a button click do all the work, but of course, this is an example, you should change this as you wish.
We then get the process we need, here it is iexplore, for internet explorer. You can find it in the task manager or tasklist command in Command Prompt. But use it without the .exe part. When we are getting the process, we receive a list: of course, multiple instances of iexplore might be running! I extracted the first one. (Be careful, though, if no iexplore is running, it'll throw an error - handle that.)
And then, get the handle of the main window! What is a handle, btw?
Use ShowWindow(hWnd, ShowWindowCommands.ForceMinimize) to minimize Internet Explorer with the API. (I had to force. It didn't work with Minimize = 6 value.)
Read more here on pinvoke and here on MSDN
EDIT:
OMG! Internet explorer is multi-process!
Instead of minimizing the first, minimize them all!
Change code inside Button1_Click to:
For Each p In Process.GetProcessesByName("iexplore")
' Since Internet Explorer always has its name in the title bar,
If p.MainWindowTitle.Contains("Internet Explorer") Then
Dim hWnd As Long = p.MainWindowHandle
ShowWindow(hWnd, ShowWindowCommands.ForceMinimize)
End If
Next

Related

How to make SendKeys act Synchronously in IBM Host Access Library

I use the IBM Host Access Class Library for COM Automation as a way to communicate with an IBM AS400 (aka iSeries, IBM i, green screen, 5250) through a terminal emulator. I notice that when you issue a "SendKeys" instruction, control returns to your application before the IBM emulator finishes with the command. This can lead to timing problems because you might then send another "SendKeys" instruction before the system is ready to accept it.
For example:
Imports AutPSTypeLibrary
Imports AutConnListTypeLibrary
Imports AutSessTypeLibrary
Sub Example
Dim connections As New AutConnList
connections.Refresh()
If connections.Count < 1 Then Throw New InvalidOperationException("No AS400 screen can currently be found.")
Dim connection As IAutConnInfo = DirectCast(connections(1), IAutConnInfo)
_Session = New AutSess2
_Session.SetConnectionByHandle(connection.Handle)
Dim _Presentation As AutPS = DirectCast(_Session.autECLPS, AutPS)
_Presentation.SendKeys("PM70[enter]", 22, 8)
_Presentation.SendKeys("ND71221AD[enter]", 22, 20)
End Sub
would work correctly when stepping through code in a debugger, but would fail when running normally because the second instruction was sent too soon.
One way to work with this is to put a timer or loop after each command to slow the calling program down. I consider this less than ideal because the length of time is not always predictable, you will often be waiting longer than necessary to accommodate an occasional hiccup. This slows down the run time of the entire process.
Another way to work around this is to wait until there is a testable condition on the screen as a result of your sent command. This will work sometimes, but some commands do not cause a screen change to test and if you are looking to abstract your command calling into a class or subroutine, you would have to pass in what screen condition to be watching for.
What I would like to find is one of the "Wait" methods that will work in the general case. Options like the autECLScreenDesc class seem like they have to be tailored to very specific conditions.
The autECLPS (aka AutPS) class has a number of Wait methods (Wait, WaitForCursor, WaitWhileCursor, WaitForString, WaitWhileString, WaitForStringInRect, WaitWhileStringInRect, WaitForAttrib, WaitWhileAttrib, WaitForScreen, WaitWhileScreen) but they also seem to be waiting for specific conditions and do not work for the general case. The general case it important to me because I am actually trying to write a general purpose field update subroutine that can be called from many places inside and outside of my .dll.
This example is written in VB.NET, but I would expect the same behavior from C#, C++, VB6, Java; really anything that uses IBM's Personal Communications for Windows, Version 6.0
Host Access Class Library.
The "Operator Information Area" class seems to provide a solution for this problem.
My general case seems to be working correctly with this implementation:
Friend Sub PutTextWithEnter(ByVal field As FieldDefinition, ByVal value As String)
If IsNothing(field) Then Throw New ArgumentNullException("field")
If IsNothing(value) Then Throw New ArgumentNullException("value")
_Presentation.SendKeys(Mid(value.Trim, 1, field.Length).PadRight(field.Length) & "[enter]", field.Row, field.Column)
WaitForEmulator(_Session.Handle)
End Sub
Private Sub WaitForEmulator(ByVal EmulatorHandle As Integer)
Dim Oia As New AutOIATypeLibrary.AutOIA
Oia.SetConnectionByHandle(EmulatorHandle)
Oia.WaitForInputReady()
Oia.WaitForAppAvailable()
End Sub
I give thanks to a user named "khieyzer" on this message board for pointing our this clean and general-purpose solution.
Edit:
After a few weeks debugging and working through timing and resource release issues, this method now reads like:
Private Sub WaitForEmulator(ByRef NeededReset As Boolean)
Dim Oia As New AutOIA
Oia.SetConnectionByHandle(_Presentation.Handle)
Dim inhibit As InhibitReason = Oia.InputInhibited
If inhibit = InhibitReason.pcOtherInhibit Then
_Presentation.SendKeys("[reset]")
NeededReset = True
WaitForEmulator(NeededReset)
Marshal.ReleaseComObject(Oia)
Exit Sub
End If
If Not Oia.WaitForInputReady(6000) Then
If Oia.InputInhibited = InhibitReason.pcOtherInhibit Then
_Presentation.SendKeys("[reset]")
NeededReset = True
WaitForEmulator(NeededReset)
Marshal.ReleaseComObject(Oia)
Exit Sub
Else
Marshal.ReleaseComObject(Oia)
Throw New InvalidOperationException("The system has stopped responding.")
End If
End If
Oia.WaitForInputReady()
Oia.WaitForAppAvailable()
Marshal.ReleaseComObject(Oia)
End Sub

In VB.NET, how can you toggle between whether your own or another process is in front on the screen?

This is not a duplicate. There are many other questions similar to this, but none of their answers are working for me, not even in Hello World apps.
Essentially I have a VB.NET program and an Adobe AIR program, and I need the VB.NET program to be able to toggle which one is in front of the other. I've been researching APIs for doing things like this in VB.NET, but none of them have been panning out. I've even reduced it to a Hello World program which toggles between itself and another .NET executable, but the most it will ever do is flicker orange on the Windows 7 taskbar at the bottom of the screen; whichever executable is in front will simply stay there regardless.
Most of the APIs I've seen on the Internet for this use user32.dll (this is a 32-bit machine, by the way), and they use functions like ShowWindow, SetForegroundWindow, and other kinds of things like that. Here is but one example that has been tried:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(ByVal pHWND As IntPtr) As _
<MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Private m_hwndSelf As IntPtr = Process.GetCurrentProcess().MainWindowHandle
Private m_procApp As Process
Private m_blnFlag As Boolean = True
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim prInfo As New ProcessStartInfo("somePath/someProg.exe", "")
prInfo.UseShellExecute = False
m_procApp = Process.Start(prInfo)
Timer1.Interval = 5000
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If m_blnFlag Then
SetForegroundWindow(m_procApp.MainWindowHandle)
Else
SetForegroundWindow(m_hwndSelf)
End If
m_blnFlag = Not m_blnFlag
End Sub
End Class
This can't be rocket science, and it's probably a mistake I'm making, but I do not see an adequate explanation when searching forums and the like. Why is this program not toggling which window is in front? Why does it simply flicker orange? Other, related functions don't seem to be working either, and neither do other ways of trying to get window handles, so what's wrong in general with my code? Thanks.
The problem lay in how I was attempting to get the window handle. Instead of using Process.MainWindowHandle, I should've been using FindWindow(String, String) (which is in user32.dll). In particular:
FindWindow(Nothing, "Program Name in the Taskbar")
Technically this hasn't been verified with using SetForegroundWindow(IntPtr), but it has been verified with another user32.dll function I was trying out earlier, ShowWindow(IntPtr, Integer).

Using CMD window to start/debug a NET program, changing command arguments internally

I found possible to use CMD window to start typical NET GUI application.
But not so clean, with some issues.
Here is my example code:
Public Class main_form
Private Declare Function AttachConsole Lib "kernel32.dll" (ByVal dwProcessId As Int32) As Boolean
Private Declare Function FreeConsole Lib "kernel32.dll" () As Boolean
Dim mykey As String = "A01F-FFB4-0402"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Application.CommandLineArgs.Count > 0 Then
If AttachConsole(-1) Then
Dim kill_Me As Boolean = False
Dim cArgs() As String = GetCommandLineArgs()
For c As Integer = 0 To cArgs.Length - 1
cArgs(c) = cArgs(c).ToLower()
If cArgs(c) = "getkey" Then
Console.WriteLine(mykey)
kill_Me = True
End If
Next
FreeConsole()
If kill_Me Then End
If (MessageBox.Show( _
"Asked command not present." & NewLine & "Start program with GUI normally?", "", _
MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = _
DialogResult.Cancel) _
Then End
End If
End If
Please ignore security issues, this is just an example.
Like people here concludes problem is in terminating console. Here are approach with sending key "ENTER" to it but this is obviously not good solution.
If I start example program from CMD window with: c:\mydir>myprogram.exe getkey|more then console behaves more "natural" than without switch "more".
1) But, if I start a program with c:\mydir>myprogram.exe getkey: is it possible with VBNET to add keyword "|more" to commandline argument "getkey" before execution so program will execute "getkey|more" instead of only "getkey"?
2) Is it acceptable to terminate GUI program with just command "End" in form's _Load handler like is showed in example? I try Application.Exit() and Me.Close() where program don't behaves well.
Your application (be it .Net or native, doesn't matter) can be a console application or GUI application, but not both. This has been discussed before by smarter people than me: How do I write a program that can be run either as a console or a GUI application:
Each PE application contains a field in its header that specifies which subsystem it was designed to run under. You can say IMAGE_SUBSYSTEM_WINDOWS_GUI to mark yourself as a Windows GUI application, or you can say IMAGE_SUBSYSTEM_WINDOWS_CUI to say that you are a console application. If you are GUI application, then the program will run without a console.
The subsystem determines how the kernel prepares the execution environment for the program. If the program is marked as running in the console subsystem, then the kernel will connect the program's console to the console of its parent, creating a new console if the parent doesn't have a console. (This is an incomplete description, but the details aren't relevant to the discussion.) On the other hand, if the program is marked as running as a GUI application, then the kernel will run the program without any console at all.
So if you have a GUI application then you cannot connect the application stdin/stderr to your console, so pipes will not work, ergo things like cat, more, less, tee are all a no-go for a GUI application. Not to mention that the CMD console will not wait for your application.
Try not to cheat. Use a GUI for GUI, use a console app for console. Share common code in libs.

VB.NET WebBrowser disable javascript

Is there a way to disable javascript webbrowser in vb.net?
works for me:
Private Function TrimScript(ByVal htmlDocText As String) As String
While htmlDocText.ToLower().IndexOf("<script type=""text/javascript"">") > -1
Dim s_index As Integer = htmlDocText.ToLower().IndexOf("<script type=""text/javascript"">")
Dim e_index As Integer = htmlDocText.ToLower().IndexOf("</script>")
htmlDocText = htmlDocText.Remove(s_index, e_index - s_index)
End While
Return htmlDocText
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString(yourUrl)
Dim wb As New WebBrowser
wb.Navigate("")
Do While wb.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim script As String = TrimScript(result)
wb.DocumentText = script
End Sub
The short answer is: No.
The slightly longer answer is: No, the web-browser control API does not allow disabling standard browser functionality.
No really...but if you getting that annoying error message that pops up saying a script is running then you can turn the property of the webbrowser's suppress-errors "true"
Which popup message do you want to disable? If it's the alert message, try this, obviously resolving the window or frame object to your particular needs, I’ve just assumed top-level document, but if you need an iframe you can access it using window.frames(0). for the first frame and so on... (re the JavaScript part)... here is some code, assuming WB is your webbrowser control...
WB.Document.parentWindow.execScript "window.alert = function () { };", "JScript"
You must run the above code only after the entire page is done loading, i understand this is very difficult to do (and a full-proof version hasn't been published yet) however I have been doing it (full proof) for some time now, and you can gather hints on how to do this accurately if you read some of my previous answers labelled "webbrowser" and "webbrowser-control", but getting back to the question at hand, if you want to cancel the .confirm JavaScript message, just replace window.alert with window.confirm (of course, qualifying your window. object with the correct object to reach the document hierarchy you are working with). You can also disable the .print method with the above technique and the new IE9 .prompt method as well.
If you want to disable JavaScript entirely, you can use the registry to do this, and you must make the registry change before the webbrowser control loads into memory, and every time you change it (on & off) you must reload the webbrowser control out and into memory (or just restart your application).
The registry key is \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ - the keyname is 1400 and the value to disable it is 3, and to enable it is 0.
Of course, because there are 5 zones under the Zones key, you need to either change it for the active zone or for all zones to be sure. However, you really don't need to do this if all you want to do si supress js dialog popup messages.
Let me know how you go, and if I can help further.

How do I close a currently opened MsgBox using VBA?

Is there any option to close the currently opened MsgBox using any code in VBA access form application?
Check out Randy Birch's response on this thread in microsoft.public.vb.general.discussion
He recommends creating a function in a .bas file called MsgBox. Doing so will cause VB to call your function rather than the built in one.
You'd then create your own MsgBox form and build in a timer to close your form after a set period of time. He provides links showing how to do this.
He also discusses a way to explicitly call the built in MsgBox function in case you need to do this.
Note: I've never done this but I've found that Randy Birch is a knowledgeable resource.
MsgBoxes are not intended to be programmatically closed, that's why it's difficult to do so. If you find yourself in a design where you must force close a MsgBox, you should probably re-evaluate your design.
I may be wrong but MsgBox is a blocking call creating a modal form so I don't think there is an easy way such as an option to do that. Do you have a specific use case for this ?
As MarkJ points out, could this could be a dialog generated by Access (rather than a VBA.MsgBox called in your own code)?
For example, when using table's 'dataview' in the Access UI to add a row you get a message, "You are about to append 1 record..." (or similar). Is this the kind of message you mean? If so, there are indeed ways to suppress them...
I used to have an easy answer to this: use the Windows Scripting Shell object, which has a 'Popup' function - a Message Box, just like the VBA MsgBox() function, with a 'SecondsToWait' parameter that provides exactly the timeout you wanted.
With CreateObject("Scripting.WsShell")
.Popup "Watch me disappear in 5 seconds", 5, Application.Name & ": test", vbInformation + vbOkCancel
End With
If you include a 'Cancel' button, it might still work: the available parameters are vbOkCancel, vbYesNoCancel, and vbRetryCancel.
If you're trying to close a dialog box you didn't initiate with your own msgBox() function call, that's unhelpful: and, as I've hinted above, the 'SecondsToWait' parameter doesn't really work these days - someone in Redmond really does't like the idea of one thread closing another thread's helpful warnings and important interruptions to the user's workflow.
However, you can launch a delayed Message Box 'Close' command using the API Timer() function - not quite 'close the currently opened MsgBox', as this requires advance warning that you intended to open it - but it's the closest thing I have, it fits into a self-contained VBA module, and I posted the code in an answer to a very similar StackOverflow question to yours.
I should warn you that the answer in question is using a sledgehammer to crack a nut, with a side order of lengthy explanation.
Rather than writing an alternative to the Access MsgBox from scratch, you might consider using (or at least studying) Arvin Meyer's Custom MessageBox Creator (downloadable on this page).
Message Boxes are meant to depict some information to the user. Hence programmatically closing is not a good design, unless you are not automating some process.
You can use sendkeys or win APIs.
I have had a similar problem; theoretically you can use the "SendKeys" function (see http://msdn.microsoft.com/en-us/library/8c6yea83%28VS.85%29.aspx). However, the MsgBox blocks the running so you cannot use the command. If you know when it going to pop up, you may run (from the script) external whshell that wait some time and then use the SendKeys. But if you know how to do that, tell me (here). Other similar possibility is to open different thread/process for it that will not be block, but I don't know if it is possible in VBA.
An easy to use Win32 based answer to actually be useful calling it. Using the below code you can easily do a timer-based PopUpBox. I like 1/2 second timer control so 2 = 1 second. Found this answer looking for the answer to this thread. Above answers don't seem to work. I want to use this when I want to show a quick message or quick answer default is normally right can be used for more.
Function Code usually in a Module:
Declare Function MessageBoxTimeout Lib "user32.dll" Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal uType As Long, _
ByVal wLanguageID As Long, _
ByVal lngMilliseconds As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Function PopUpBox(Optional stMessage As String _
= "Yes or No? leaving this window for 1 min is the same as clicking Yes.", _
Optional stTitle As String = "PopUp Window", _
Optional HalfSecTimer As Long = 120, Optional lgVBmsgType As Long = vbYesNo) As Long
Dim RetVal As Long
HalfSecTimer = HalfSecTimer * 500
RetVal = MessageBoxTimeout(FindWindow(vbNullString, Title), stMessage, stTitle, lgVBmsgType, _
0, HalfSecTimer)
PopUpBox = RetVal
End Function
Call Function Code
Examples: Actual code from my database
PopUpBox "Re-Linking and Closing dB", "Closing dB", 3, vbOKOnly
intAnswer = PopUpBox("Software Lock Down Active?", "Security", 10, vbYesNo)
I know this is an old post, put it seems there is a new a easy way to do this:
Sub MessageBoxTimer()
Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
'Set the message box to close after 10 seconds
AckTime = 10
Select Case InfoBox.Popup("Click OK (this window closes automatically after 10 seconds).", _
AckTime, "This is your Message Box", 0)
Case 1, -1
Exit Sub
End Select
End Sub
Sample code provided by: Tom Urtis
https://learn.microsoft.com/en-us/office/vba/excel/concepts/controls-dialogboxes-forms/automatically-dismiss-a-message-box