Restore application from system tray - c#

What is the best way to programmatically restore and give focus to a third-party application (say, GoogleTalk or Twhirl) running in the system tray? I am writing my utility in C#, but I obviously have no control over the third-party application.

Use something like FindWindow /FindWindowEx to find the hidden window and get its window handle and then call ShowWindow ( handle, SW_NORMAL) to unhide it.
Use a tool like Spy++ (can be found in the visual studio tools menu) to find the parameters which can be passed on to FindWindow to locate the desired window.

Use an API call to send mouse click events to the system tray? Google WM_SENDMSG SendMessage Win32 API for a starting point
There's also another API call for setting focus once the window's back up.

Related

Receive messages of other window

What I'm trying to do: receive messages of other process window (Spy++).
For example: I opened new notepad window: http://i.stack.imgur.com/vNg6h.png
And then I moved the mouse over the notepad window, the windows will send this message to the notepad window (or to the main window child window/s):
WM_MOUSEMOVE xPos=100,yPos=200
I want to receive this message exactly when then mouse event is happening (by event or while loop) (probably while loop with Application.doEvents() and Threading.Thread.Sleep(1)).
What I have tried:
create messages listener by the ManagedSpyLib (if you will import this library you can't compile your project... and if you will succeed to import this library it's not built for this mission)
I also searched about it and this is the most relevant result: http://www.codeproject.com/Articles/3923/InterSpy-An-integrated-Windows-message-trace-and-f
but this is a c++ project and I'm trying to do it in C# or VB.NET.
Good example is better than explanation for me. :)
The hooks that you need for this are WH_CALLWNDPROC and WH_GETMESSAGE. And these require DLL injection. You cannot inject managed code and so you will need to use unmanaged code for the hooking. You can then get your unmanaged injected DLL to communicate back to your C# application, but you will have to concede defeat on your attempts to write the hooking code in C#.
You need to look at the HOOK. That's how SPY++ works.
However, .Net is not very suitable for this, because you have to use a lot of WIN32 API functions.

Control OS Mouse Event using Windows Form - C#

The program I am trying to do is to simulate the mouse event of a operating system using keyboard with Windows Form. Right now I am able to change the cursor and do different actions like mouse clicking inside the Form (when the Form is on the Top).
The problem is I would like to extend it to the whole Operating System, which means even if my Windows Form is not at the top, I am still able to control my cursor and do all sorts of mouse event on other applications while the Form is running. How should I do to implement this ?
You might want to look at this library Global System Hooks in .NET which uses global system hooks to detect all mouse and keyboard events include those outside of your application.
You can synthesize OS-wide keystrokes, mouse motions, and button clicks using the Win32 SendInput() API. You can call it from C# using P/Invoke. Sample code can be found here: SendInput on PInvoke.net.
I remember back in the day I used the SendInput (and a screenshot API) to create a Minesweeper bot in C# (2.0 I think). It could solve an Expert puzzle in about one second. I wish I still had the source code to sample here, but I don't.
EDIT: It appears someone has already created a nice .NET warpper for the SendInput(): Windows Input Simulator on CodePlex.

Windows CE restore other app window in C#

How can I restore other application window from my application?
I know only process name for this application.
I search code for .Net 3.5
Thanks and hope for you reply
It depends on what you know about that other Window, but generally speaking you could P/Invoke to FindWindow and pass in the window's caption. That will get you the target Window's handle. With that you call BringWindowToTop, ShowWindow and/or SetWindowPos (again via P/Invoke) depending on the Window's state and your desires.

SetParent() on a VB6 application from a C# app

I am trying to use the win32 API to set the parent of an application to a panel in my C# application.
When I inspect it using Spy++ the application loads in 2 main forms ThunderRT6Main and ThunderRT6MDIForm both with the same title.
I have found the handle of both of these and tried to call SetParent on them both but the window does nothing and does not move anywhere...
Is there something funky I need to do when invoking SetParent() on a VB6 app?
ThunderRT6Main is the hidden owner of every top level form in VB6. It sets the application icon, the one you see in the Applications tab of Task Manager. You can read this interesting article
A window can have a parent or an owner but not both
Basicly you might need WS_CHILD set before calling SetParent.

Bringing Window to the Front in C# using Win32 API

I am writing an application that needs to bring window of an external app to the foreground, and not necessarily steal focus (there is a setting the user can toggle to steal/not steal focus).
What is the best way to go about this using the win32 API? I have tried SetForeground() but it always steals focus and does not consistenly work.
What is the best way to go about this? Any thoughts?
SetForegroundWindow is supposed to steal focus and there are certain cases where it will fail.
The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window
Try capturing the focus with SetCapture prior to making the call. Also look into different ways of bringing the window to the front: SetForeGroundWindow, SetActiveWindow, even simulating a mouse click can do this.
What is the difference between SetForeGroundWindow, SetActiveWindow, and BringWindowToTop? It appears as if they all do the same thing.
According to MSDN, SetForeGroundWindow will activate the window and direct keyboard focus to it. This attempts to work even when your process is in the background. SetActiveWindow does the same thing as SetForeGroundWindow, but it doesn't do anything if your application isn't the frontmost application. Finally, BringWindowToTop only brings the window to the top, and doesn't change the keyboard focus.
You can try the BringWindowToTop function to not steal focus. I haven't used it, but it seems to be what you're looking for.
Have you tried using SetWindowPos. This is the canonical function for moving, resizing and setting z-order in Windows. There is a SWP_NOACTIVATE flag you can use. Look at http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx. I have not tried this on a window belonging to another process, but it is probably worth a try.
SetWindowPos + SWP_NOACTIVATE does the job.
You could use FindWindow to get the HWND of the window, then use the BringWindowToTop function found in the Win32 API.

Categories