Click here to Skip to main content
15,798,200 members
Articles / Programming Languages / C#
Tip/Trick

Execute a program in a second monitor

Rate me:
Please Sign up or sign in to vote.
4.89/5 (13 votes)
12 Mar 2014CPOL2 min read 107.7K   8.7K   35   20
With C# and the Windows API, you can execute a program in a second monitor.

Introduction

I need to execute a slideshow on a second monitor. After a research, I found a way to get information about the monitor and how to specify the position for an exe. I couldn't find how to specify that the program begins on the second monitor. I could only find a way to open a program and then move the window of the program to the second monitor.

Background

In .NET, you can get information about monitors using the namespace System.Windows.Forms. More information is available here: Screen class.

You can control the settings for windows with functions available here: Windows Functions.

Using the code

The parameters for the command line are: the number of monitors, the name of the program, the arguments for executing the program.

Example: monitor notepad.exe text.txt 2

With the above instruction on a cmd, Notepad opens on the second monitor in full screen and with the name of the file text.txt.

Here is part of the code. First, we get the number of monitors in the CPU. After that, we get the information about the monitor for displaying the exe. And finally, move the window to that monitor.

C#
if (numberMonitor >=1 )
{
 if (Screen.AllScreens.Length < numberMonitor)
 {
    MessageBox.Show("The monitor doesn't exist");
    Close();
 }
 else
 {
    numberMonitor--;
    //Get the data of the monitor
    monitor = Screen.AllScreens[numberMonitor].WorkingArea;
    //change the window to the second monitor
    SetWindowPos(proceso.MainWindowHandle, 0,
    monitor.Left, monitor.Top, monitor.Width,
    monitor.Height, 0);
  }
}

Points of Interest

I didn't know that with .NET, you can get information about the screens and with the APIs for windows, you can control the position of any window if you have the handle to the window. And you can get the handle to any exe easily.

There are a lot functions in Windows APIs, for example, you can set the foreground any window, change to another window if you press ALT + TAB, etc.

With these functions, you can do for example:

  • A chat window that when receiving a message, can get to the front of other windows.
  • You can open other instances of your application and with buttons, you can change to other instances of your application.

I have examples of these functions that I explain in other articles.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer armheSistemas
Mexico Mexico
I'm a software developer. I like to develop web pages and desktop systems. I'm learning unity, xamarin, swift, android, security, UX

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ric W 149816525-Apr-22 10:12
Ric W 149816525-Apr-22 10:12 
Questionwin 10 x64 pro doesn`t work Pin
Member 1391306816-Jul-18 2:18
Member 1391306816-Jul-18 2:18 
AnswerRe: win 10 x64 pro doesn`t work Pin
Ivan Sørensen18-Jun-19 1:22
Ivan Sørensen18-Jun-19 1:22 
QuestionProgram crash in IE or Chrome when excute 2 times Pin
Member 1090453327-Jun-14 0:02
Member 1090453327-Jun-14 0:02 
QuestionNot notepad example Pin
Mabire Frédéric6-Feb-14 2:53
Mabire Frédéric6-Feb-14 2:53 
AnswerRe: Not notepad example Pin
apis344518-Feb-14 4:34
professionalapis344518-Feb-14 4:34 
AnswerRe: Not notepad example Pin
apis344510-Mar-14 18:51
professionalapis344510-Mar-14 18:51 
BugDoes not work on Windows 7 64-bit Pin
Member 1032969310-Oct-13 23:28
Member 1032969310-Oct-13 23:28 
GeneralRe: Does not work on Windows 7 64-bit Pin
apis344518-Feb-14 4:35
professionalapis344518-Feb-14 4:35 
GeneralRe: Does not work on Windows 7 64-bit Pin
apis344510-Mar-14 18:59
professionalapis344510-Mar-14 18:59 
QuestionLooked All over the net for this - Thanx Pin
AceApps14-Sep-13 13:32
AceApps14-Sep-13 13:32 
QuestionNot working out on Google Chrome.exe Pin
Member 449259430-May-12 14:31
Member 449259430-May-12 14:31 
AnswerRe: Not working out on Google Chrome.exe Pin
apis344518-Feb-14 4:36
professionalapis344518-Feb-14 4:36 
AnswerRe: Not working out on Google Chrome.exe Pin
apis344510-Mar-14 19:28
professionalapis344510-Mar-14 19:28 
Questioni like it, but how i can use this? Pin
pieitch10-May-12 14:19
pieitch10-May-12 14:19 
AnswerRe: i like it, but how i can use this? Pin
apis344511-May-12 16:17
professionalapis344511-May-12 16:17 
AnswerRe: i like it, but how i can use this? Pin
apis344510-Mar-14 19:30
professionalapis344510-Mar-14 19:30 
QuestionThanks. A very interesting tip Pin
victorbos18-Oct-11 3:46
victorbos18-Oct-11 3:46 
QuestionGreat ! Pin
Rajeev Barde12-Oct-11 10:28
Rajeev Barde12-Oct-11 10:28 
QuestionGood tip. Not an article. PinPopular
Vano Maisuradze11-Oct-11 5:01
Vano Maisuradze11-Oct-11 5:01 
I think it is a good tip. Not an article.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.