I have dual monitors and I am working on a product that allows you to record your computer screen. Currently, I am using the following code:
Rectangle screenArea = Rectangle.Empty;
foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens){
screenArea = Rectangle.Union(screenArea, screen.Bounds);
}
Which inevitably (in my case) records the entirety of the desktop. With the aspect ratio of both screens, where "screenArea" is the area being recorded. Is there a way in which I can specify the active monitor in which the program is running on?
Thanks for any help,
Christopher.
Maybe this could help.
How do I determine which monitor my .NET Windows Forms program is running on?
Also, there is PrimaryScreen Property:
http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.primaryscreen(v=vs.110).aspx
You can get an array of Screens that you have using this code.
Screen[] screens = Screen.AllScreens;
You can also figure out which screen you are on, by running this code (this is the windows form you are on)
Screen screen = Screen.FromControl(this); //this is the Form class
In short check out the Screen class and static helper methods, they might help you.
MSDN Link, doesn't have much..I suggest messing around in the code by yourself.
Related
I was wondering if it would be possible to capture the whole view of the screen while the windows magnifier tool is running in fullscreen mode. Essentially, what I am looking for is while I'm looking with my eyes at a certain point on the screen, and only that certain point of the screen is shown on my monitor, I want to have the ability to printscreen the entirety of the original screen without closing out of magnifier. No clue how to word it better and clearer than that. I know it's possible to get screen pixels info through the Graphics class like so:
this.graphics.Clear(Color.Black);
this.graphics.CopyFromScreen(pictureBoxPoint.X, pictureBoxPoint.Y, 0, 0, size);
But this does not exclude the magnifier's influence on the screen, and only grabs it as it's seen by the user. What I would ideally like to be able to do is grab the entirety of the screen as it's supposed to be seen if the magnifier tool wasn't zooming in only on one part, if that makes sense. The entire screenspace even if only part is visible.
I had a quick look at the Magnification API docs page, but I have no idea if that is actually where I should be looking. Is there something else I can do? I'd appreciate it if someone pointed me in the right direction because I seem to be going in circles.
Thanks a lot!
Have a look at the github project karna-magnification, this will allow you to make a windows form object into the view portal and magnify anything under the cursor. To capture the magnification, you just need to capture the contents of the form.
Once you reference the github project, in the constructor for the form you want to use as the magnifier view panel, Add
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Magnifier mg = new Magnifier(this);
}
}
}
from there add code to use the graphics context of the form to capture the image. That part I will leave to you.
How do I detect if window is not visible to the user and then move it to a position that is visible to allow the user to size it to their liking.
The problem:-
I have a program that save the position and size of the form this works perfectly.
Unfortunately, many users now have multiple screen graphics cards that can be switched between duplicate and extended.
Therefore, if the user closes the form (the program saves where it was) and the user switches his system to say extended from duplicate then opens the program.
The program form is now not visible because it has moved off the limits of the current screen system :(
The user can see it running on the task bar. However clicking the icon only gives a brief flash of minimising or maximising the program and right clicking only has the option to close.
I need to trap for not visible to the user and then auto resize to any active window. The user can then resize/move as they see fit.
I would welcome any ideas/pointers
This answer was provided for the same question on the MSDN forums - essentially, it's a quick check of the bounds of the screen, and checks to see if the program is within those bounds. If not, it moves it to 0, 0:
using System.Windows.Forms;
class Form1 : Form {
protected void EnsureVisible() {
foreach (Screen scrn in Screen.AllScreens) {
// You may prefer Intersects(), rather than Contains()
if (scrn.Bounds.Contains(this.Bounds)) {
return;
}
}
this.Location = new Point( 0, 0 );
}
}
You could make use of the AllScreens property to get a list of all the screens connected to the computer. Using that, you can check the bounding boxes of each screen and test if your window lies outside all of them. If so, then reset the position.
And Jared Harley beat me to it, while I was typing this.
Is there a way to make make a windows form application full screen and black out your secondary monitors? So the main display is on your primary display and all your other monitors are just completely black?
You can use the Screen class which give you informations about the current active screens.
// Form myFrm
Rectangle r = new Rectangle();
foreach (Screen s in Screen.AllScreens)
{
if ( s != Screen.CurrentScreen ) // Blackout only the secondary screens
r = Rectangle.Union(r, s.Bounds);
}
myFrm.Top = r.Top;
myFrm.Left = r.Left;
myFrm.Width = r.Width;
myFrm.Height = r.Height;
myFrm.TopMost = true; // This will bring your window in front of all other windows including the taskbar
I can think of one way, and that would be to find out how many monitors there are on the computer, and their layout relative to each other, then create your primary window at 0,0, maximize it and set it to be TopMost, then do the same for the other displays, placing them at the screen locations corresponding to the top left of each monitor of the computer.
The only thing I can think of that would benefit from this in a WinForms environment is an app designed to give a test; the app would cover the entire desktop (except the taskbar; you'd have to disable the Start menu) and pretty much ensure that the user couldn't look at anything except the testing program. It will give you a minimal performance advantage.
Most of the apps that black out all the monitors except the main display are basically using DirectX to control the screen directly (through the lower-level interface to the graphics card). If you're using WinForms to make your program, you're about 50 levels of abstraction above using DirectX.
One way would be to create a second form in your application. One that, when given a set of dimensions, will open without border or menu and with the background set to black.
When your application runs, enumerate your monitors (count them to see how many) and find their dimensions. Your primary one will start at 0,0.
Then spawn a copy of that second form for each of your monitors (except your primary one) and give that form the dimensions of the monitor. They will then turn each screen black.
You will need to remember to keep a handle to each of the forms and terminate them when you terminate your main form.
I made an application on 1366x768 resolution which is my laptop's current resolution, being a beginner, i din't care about the resolution issues.
When i took that application and executed it on a lower resolution PC , then all was messed up. The form was going somewhere else, most of its part hidden.
So, please help me to how to make my winform resolution independent, or how to get the current screen resolution and set it into my winform so that it changes itself accordingly.
The property
Screen.PrimaryScreen.WorkingArea
is very useful for form sizing and positioning.
For example this code:
this.Width = Screen.PrimaryScreen.WorkingArea.Width/2;
this.Height = Screen.PrimaryScreen.WorkingArea.Height/2;
this.Top = (Screen.PrimaryScreen.WorkingArea.Top + Screen.PrimaryScreen.WorkingArea.Height)/4;
this.Left = (Screen.PrimaryScreen.WorkingArea.Left + Screen.PrimaryScreen.WorkingArea.Width)/4;
(The Screen class is a member of System.Windows.Forms namespace)
will place the form in which it is executed in the middle of the screen and size it to half the screen.
The WorkingArea var is used to exclude stuff like the task bar and other docked items on the desktop when calculating the size of the screen.
Hope this helps.
See here: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/1be40692-80fa-42ba-b316-9fe13a935b4f/ and here: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/d5f2cd8d-d24d-4574-ba3a-96e22e3d1b56/
General approach is to start with the min resolution you'll be working with. Then you can scale your forms up (using proper docking and anchors).
I want to draw directly on the desktop in C#. From searching a bit, I ended up using a Graphics object from the Desktop HDC (null). Then, I painted normally using this Graphics object.
The problem is that my shapes get lost when any part of the screen is redrawn. I tried a While loop, but it actually ends up drawing as fast as the application can, which is not the update rate of the desktop.
Normally, I would need to put my drawing code in a "OnPaint" event, but such thing does not exist for the desktop.
How would I do it?
Example code: https://stackoverflow.com/questions/1536141/how-to-draw-directly-on-the-windows-desktop-c
I posted two solutions for a similar requirement here
Basically you have two options.
1- Get a graphics object for the desktop and start drawing to it. The problem is if you need to start clearing what you have previously drawn etc.
Point pt = Cursor.Position; // Get the mouse cursor in screen coordinates
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
g.DrawEllipse(Pens.Black, pt.X - 10, pt.Y - 10, 20, 20);
}
2- The second option that I provide in the link above is to create a transparent top-most window and do all your drawing in that window. This basically provides a transparent overlay for the desktop which you can draw on. One possible downside to this, as I mention in the original answer, is that other windows which are also top-most and are created after your app starts will obscure your top most window. This can be solved if it is a problem though.
For option 2, making the form transparent is as simple as using a transparency key, this allows mouse clicks etc. to fall through to the underlying desktop.
BackColor = Color.LightGreen;
TransparencyKey = Color.LightGreen;
When you draw to HDC(NULL) you draw to the screen, in an unmanaged way. As you've discovered, as soon as windows refreshes that part of the screen, your changes are overwritten.
There are a couple of options, depending upon what you want to achieve:
create a borderless, possibly
non-rectangular window. (Use
SetWindowRgn to make a window
non-rectangular.) You can make this a child of the desktop window.
subclass the desktop window. This is not straightforward, and involves
injecting a DLL into the
Explorer.exe process.
To get an OnPaint for the desktop you would need to subclass the desktop window and use your drawing logic when it receives a WM_PAINT/WM_ERASEBKGND message.
As the thread you linked to says, you can only intercept messages sent to a window of an external process using a hook (SetWindowsHookEx from a DLL).
As mentioned a transparent window is another way to do it, or (depending on the update frequency) copying, drawing and setting a temporary wallpaper (as bginfo does).
This is difficult to do correctly.
It will be far easier, and more reliable, to make your own borderless form instead.