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

How to copy data from one column to another at the same datatable in c#
asp.net CustomValidator running when RegularExpressionValidator fails.
Using .NET to write SVG Format, what am I doing wrong with this c# implementation?
Login unsuccessful in 3 column table
Obtain current Oauth access token C#
How to scroll down while page loading - GeckoFx C#
How to convert a 2D binary object in to a Dictonary<string, object> in C# using Binary Formatter
From where would a referenced assembly's culture info be loaded? And how to set it to “neutral”?
How to make a Form load before opening another?
Unity .dae object not rotating on touch with c# script
Pass text of Listview.ItemTemplate to antoher frame uwp
WPF Prism initializing tabs with ICollection using Navigation
AJAX call returns 'undefined'
C# Linking windows form with already created classes and console
C# responsible form on excel
How to perform ctrl+p for print in c# selenium?

Categories

HOME
asp.net
airflow
display
microcontroller
x86-64
hex
redhawksdr
app-store
coordinates
aggregation-framework
travis-ci-cli
browserstack
android-webview
wkhtmltopdf
fetch
aws-sdk-cpp
aggregate
webseal
openstack-horizon
appsettings
pylint
django-rest-auth
xna
rndis
oracle-golden-gate
datastax-enterprise
adobe-captivate
cloudformation
units-of-measurement
jbossfuse
delphi-2010
selenium3
textview
jql
imdb
qt-designer
jcr
scene7
bootcamp
wit-ai
lfe
wicket-tester
autodesk-data-management
apiman
nshttpurlresponse
amazon-fire-tv
activeandroid
serverless-architecture
skype-bots
visualizer
fluent-nhibernate-mapping
kendo-scheduler
disassembly
qt3d
google-scholar
sqlproj
android-tabs
dbi
git-push
altova
bytearray
weblogic-maven-plugin
database-administration
boost-propertytree
factorization
ooad
gtkwave
textblob
firewire
euro
vspackage
perfect-scrollbar
asp.net-2.0
quickfixn
method-overloading
dojox.grid
parsefloat
twitter-finagle
iwork
kuka-krl
db2-connect
lastinsertid
thucydides
scidb
myrrix
azure-pack
qgraphicsscene
usersettings
android-library
bho
biztalk-deployment
phone-state-listener
ntdll
rbm
mail-gem
teamsystem
php-gettext
socketasynceventargs
win-prolog
flexbuilder
qvariant
n2
subdirectories
dynamic-scope
coderush-xpress
onresize

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