c#


How do I set the focus to the Desktop from within my C# application


Winforms App. .Net 3.5.
I need to set the focus from my C# application to the user's desktop (almost like simulating a mouse click on the desktop).
Can someone please show me how to do this with C#? I just want to set focus on the desktop so the focus is no longer on my application but I want to do this from within my application.
Edit: An answer below works by setting the focus to the desktop, but it minimizes all the open windows on the user's desktop.
Is there a way I can maybe set the focus to the next open window on the desktop instead? I just want to get the focus off of my application (without minimizing my application or hiding it). I just want to move focus to somewhere else. Maybe the desktop was not the best choice if it will minimize all the user's open windows/applications.
You can add this COM object in your project:
Microsoft Shell Controls And Automation
And then just call:
Shell32.ShellClass shell = new Shell32.ShellClass();
shell.MinimizeAll();
This will minimize all the windows and then focus the desktop. Otherwise, if you have your window non-full screen then you can simulate the mouse click using:
//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
You can calculate coordinates by looking at your window startup location plus height/width and select a available space (that will be the desktop indeed).
This should do it for you.
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1 {
class Program {
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;
static void Main(string[] args) {
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero);
System.Threading.Thread.Sleep(2000);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
}
}
}
Get Next Window
I don't have a code example ready for these two but I'm going to give you the links to both. The first think you need to do is call GetWindow. After doing that you'll want to call SwitchToThisWindow passing in the pointer you received from GetWindow.

Related Links

in C# winform, I got: “only truetype fonts are supported. This is not a TrueType Font”
EF Code first: Insert Many to many
Create datagridview with combobox in the second column
How to stop a thread on .dll unload
C# get => modify => set paradigm. how to shorter it into one line function call?
LINQ 2 SQL: AddObject and InsertOnSubmit
How can I let my customers have their own domains redirect to my server and identify them? [closed]
Provider for Microsoft Excel version 2.0
How to modify data grid view columns with data table bindings
How to make a “Read only” file?
Insert statement when having GUID as primary key
Linq to SQL using global datacontext to keep track of user changes?
Problems in executing AT commands at modem startup
How to Call WebReqeuest.Create on a Webservice
MVC3 how to use #<text></text> as html helper parameter
Matching multiple domains in an XPath expression

Categories

HOME
javafx
ftp
stanford-nlp
intellij-idea
webvr
x86-64
bitbucket
ms-application-insights
multipartform-data
doctrine2
guidewire
printf
glibc
datazen-server
opengraph
devexpress-mvc
grunt-contrib-uglify
csproj
folder
key-bindings
kubectl
tunnel
campaign-monitor
dotnetnuke-module
lex
susy
compiler-warnings
definition
irr
taiga
servicebus
ocpjp
opos
remap
lucee
wink
sharpssh
petapoco
codesniffer
timefield
cedar-bdd
access-violation
xcode7.1
vga
sprockets
hotkeys
jmap
cexception
video-embedding
containstable
personality-insights
uinavigationitem
jsoncpp
textblob
gmaps4jsf
data-quality-services
nexusdb
alpha-beta-pruning
fedena
mind-manager
yosemite
redmine-plugins
javacompiler
ios-enterprise
drawable
triplestore
behance-api
pyalgotrade
rda
connections
slimscroll
cvi
ardor3d
extensibility
latex-suite
exi
qmainwindow
nx
blotter
quit
junit-rule
socketasynceventargs
xpolog
jqtransform
xhtmlrenderer
designated-initializer
simultaneous
dongle
newspeak
lts
html-help-workshop
ironpython-studio
hotfix

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App