VB.Net Hide a window through code (Not is .Net window) - windows

I have a quick question:
Somebody have a code snippet to hide a window completely.

Along the same lines as your other question, you can use the handle of the window you want to hide and call the ShowWindow API function using 0 for nCmdShow will hide the window.
Declaration
Private Declare Auto Function ShowWindow Lib "user32" (ByVal hwnd As Integer, nCmdShow As Integer) As Boolean
Usuage
ShowWindow(handle, 0)

use this Me.Visible = False or check this out http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx

Related

Returning "String" value from Console Application to Batch File

Currently, my console application can return Integer values from the console application via the kernel32 function ExitProcess.
Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
How do I return string values from the console application to the batch file?
I want to return string values like Successfully transformed 100 batches... etc.
On most platforms (and also in Windows) process exit codes are integer values, but you could write string data to the standard output stream by using the GetStdHandle and WriteFile functions.
Update As requested, I´ll serve you an example.
First, you´ll need to import some more Windows functions and define the required constants. In addition to the before-mentioned GetStdHandle and WriteFile methods, the AttachConsole and FreeConsole methods are also required.
Private Const ATTACH_PARENT_PROCESS As Long = -1
Private Declare Function AttachConsole Lib "Kernel32" ( _
ByVal dwProcessId As Long) As Long
Private Declare Function FreeConsole Lib "Kernel32" () As Long
Private Const STD_OUTPUT_HANDLE As Long = -11&
Private Declare Function GetStdHandle Lib "Kernel32" ( _
ByVal nStdHandle As Long) As Long
Private Declare Function WriteFile Lib "Kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
lpOverlapped As Any) As Long
In my sample project, I just added a Module and defined a Sub Main method - this serves as the entry point for the app. Please notice, that you don´t get any output from the app when running it from the VB6 IDE debugger. You´ll need to compile it, and run it from a terminal window (for instance cmd.exe).
The first thing to do is to attach to the console of the parent process (which is the console of the terminal window). Otherwise, the GetStdHandle method will return zero.
Dim handle As Long
AttachConsole (ATTACH_PARENT_PROCESS)
handle = GetStdHandle(STD_OUTPUT_HANDLE)
Once the console handle is obtained, the WriteFile method can be used to print text to the console.
Dim s As String
Dim numberOfBytesWritten As Long
s = "Hello World."
WriteFile handle, s, Len(s), numberOfBytesWritten, ByVal 0&
Before finally calling ExitProcess, the FreeConsole method is used to detach the process from the parent console.

Subclassing an Edit control with SHAutoComplete applied

I'm new, so sorry if the question isn't posed exactly as you're get used to.
I inevitably need to subclass an Edit control for which SHAutoComplete() has been called like this:
// initialization
if FAILED( CoInitialize(NULL) ) exit(1);
// creation of an Edit control
HWND hEdit=CreateWindow( WC_EDIT, ... );
// calling SHAutoComplete - I essentially understand this as a subclassing behind the scenes
SHAutoComplete( hEdit , SHACF_AUTOSUGGEST_FORCE_ON|SHACF_FILESYSTEM );
// subclassing an Edit box for which SHAutoComplete has been called
WNDPROC autoCompleteWndProc=SubclassWindow( hEdit , __myNewWndProc__ ); // using macro
Suppose the __myNewWndProc__ function looks like a classic window procedure:
LRESULT CALLBACK __myNewWndProc__(HWND hEdit, UINT msg, WPARAM wParam, LPARAM lParam){
// handle subclass-specific messages
switch (msg){ ... }
// handle SHAutoComplete-specific messages
return autoCompleteWndProc(hEdit,msg,wParam,lParam); // <-- here's the problem (read further)
}
The problem is, it doesn't work. The application crashes with error:
Process returned -1073741819 (0xC0000005)" (access violation)
pointing the problem at line marked in the above listing.
The question is what am I doing wrong?
(I've experienced the same problem when subclassing from ComboBoxEx, but I managed to work around it, but can't find any trick with the SHAutoComplete() problem.)
SubclassWindow() is a wrapper for SetWindowLongPtr():
#define SubclassWindow(hwnd, lpfn) \
((WNDPROC)SetWindowLongPtr((hwnd), GWLP_WNDPROC, (LPARAM)(WNDPROC)(lpfn)))
When you use that type of subclassing, you MUST use CallWindowProc() to call the previous window procedure when needed, DO NOT call the procedure directly:
LRESULT CALLBACK __myNewWndProc__(HWND hEdit, UINT msg, WPARAM wParam, LPARAM lParam)
{
//...
// handle SHAutoComplete-specific messages
return CallWindowProc(autoCompleteWndProc,hEdit,msg,wParam,lParam);
}
The reason for that is stated in the documentation:
SetWindowLongPtr function
Calling SetWindowLongPtr with the GWLP_WNDPROC index creates a subclass of the window class used to create the window. An application can subclass a system class, but should not subclass a window class created by another process. The SetWindowLongPtr function creates the window subclass by changing the window procedure associated with a particular window class, causing the system to call the new window procedure instead of the previous one. An application must pass any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc. This allows the application to create a chain of window procedures.
CallWindowProc function:
lpPrevWndFunc [in]
Type: WNDPROC
The previous window procedure. If this value is obtained by calling the GetWindowLong function with the nIndex parameter set to GWL_WNDPROC or DWL_DLGPROC, it is actually either the address of a window or dialog box procedure, or a special internal value meaningful only to CallWindowProc.
You are probably running into the latter case, which will crash trying to call something that is not directly callable.
With that said, you should not be using SetWindowLongPtr() to subclass. Use SetWindowSubClass() instead. Read the following articles for more details:
Subclassing Controls
Safer subclassing

SetTimer IDEvent return Value Problems

Currently I am trying to implement a timer class in VBA. For that purpose I use the SetTimer and KillTimer functions of the Windows API...
This is the interface from Msdn:
UINT_PTR WINAPI SetTimer(
__in_opt HWND hWnd,
__in UINT_PTR nIDEvent,
__in UINT uElapse,
__in_opt TIMERPROC lpTimerFunc
);
And this is the way I declared the function wihtin my VBA-Module:
Private Declare Function SetTimer Lib "user32" (ByVal Handle As Long, _
ByVal TimerIDHandle As Any, _
ByVal ElapseTime As Long, _
ByVal AddressOfAndYourHandlerFunctionName As Long) As Long
'TimerIDHandle is of Type Any so I can pass Nothing to the function
I then Call the function this way:
Dim TimerID As Long
TimerID = SetTimer(Application.hWndAccessApp, ByVal 0&, Timer.Timeout, AddressOf TimeOutHandler)
As Vba does not accept a "Null" I have tried implementing "ByVal 0&". Is this the right way to do it?
Anyway...I call this function several times from the same Application and the function always returns 1 as an Identifier even though, according to Msdn, the function should return a unique ID for each timer that is created in the Window handle of the current Access Application.
Furthermore when I created only one timer the Callback function gets Called, but the Timer ID is given as 0, whereas the Settimer-Function returned a 1 at the time of initialization.
Here is my Callback Function Header:
Private Sub TimeOutHandler(ByVal WindowHandle As Long, _
ByVal TimerMessage As Long, _
ByVal TimerID As Long, _
ByVal ElapsedTime As Long)
Where am I wrong?
Any help is greatly appreciated of course ;-)
It worked. If it failed then it would have returned 0. You get a 0 for TimerID in the callback because you passed a 0 for the nIDEvent argument when you created the timer. You'll need to use the value that SetTimer returned to call KillTimer(). Think of it as a timer 'handle'.
You'll never get this code working in 64-bit mode so just declare the 2nd argument as Long.

Change focus to another window in VB.NET

I am using VB.NET and need to activate a certain window. Is this possible? If so, how?
You will need to use the Win32 API to do this.
First, find window you want to bring to front by calling FindWindow to obtain its handle, and then use SetForegroundWindow API to bring it to the foreground.
PInvoke contains declarations for these methods.
There are 2 solutions, one using Window API and another using pure VB.Net
you can use SetForegroundWindow(iHandle)
example with FindWindow to obtain Window handle
Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
Public Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Dim hWnd As Integer
hWnd = FindWindow(strClassName, strWindowCaption)
If hWnd > 0 Then
SetForegroundWindow(hWnd)
End If
you can use AppActivate(iProcessId)
example with GetActiveAppProcess() to obtain input Window active process in an hook program
Dim hWnd As IntPtr
Dim inputProcess = GetActiveAppProcess()
hWnd = GetActiveAppProcess().MainWindowHandle
AppActivate(inputProcess.Id)
'you can also use SetForegroundWindow
'SetForegroundWindow(inputProcess..MainWindowHandle)
SendKeys.Send("^v")

Displaying text on dialog box

How to display text on dialog-box? for example - I want to display a countdown timer, should I go with static text control?
Use a static text control and then call the function
BOOL SetWindowText(
HWND hWnd,
LPCTSTR lpString
);
to set the text at runtime.
See online Reference

Resources