Click here to Skip to main content
Licence CPOL
First Posted 11 Oct 2011
Views 5,843
Downloads 477
Bookmarked 14 times

Execute a program in a second monitor

By | 11 Oct 2011 | Article
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.

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)

About the Author

apis3445

Software Developer

Mexico Mexico

Member

Follow on Twitter Follow on Twitter
I'm a software developer. I like to develop web pages and desktop systems. I would like to learn about XNA, XCODE, ANDROID, KINECT.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questioni like it, but how i can use this? Pinmemberpieitch13:19 10 May '12  
AnswerRe: i like it, but how i can use this? Pinmemberapis344515:17 11 May '12  
QuestionThanks. A very interesting tip Pinmembervictorbos2:46 18 Oct '11  
QuestionGreat ! PinmemberRajeev Barde9:28 12 Oct '11  
QuestionGood tip. Not an article. PinPopularmemberVano Maisuradze4:01 11 Oct '11  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 11 Oct 2011
Article Copyright 2011 by apis3445
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid