C# Switch off or cover duplicated monitor - c#

I'm developing an app in C#. This app will run on a PC and also in a WACOM tablet, which is a duplicated monitor of the PC.
I want to switch off or cover the tablet with an image because the client can't see the beginning and the end of the process. When the time is right, the tablet will turn on or the screensaver will be removed so that the client can interact and, once the client's actions are finished, the WACOM tablet returns to the initial state. How can I do this?
I've been searching and I've found how to switch off the principal monitor but I don't know how to switch off only the tablet. Also some kind of screensaver would be right, but I didn't found how to put an image only in one screen.

You didn't mention the framework you are working, assuming you can reference WinForms, here is a way to show a form maximized on a specific screen:
System.Windows.Forms.Screen[] screens;
screens = System.Windows.Forms.Screen.AllScreens;
System.Windows.Forms.Screen selectedScreen = screens[1]; // choose your preffered monitor
// Sets the form to show maximized on the selected screen:
form.Left = currentScreen.Bounds.Width;
form.Top = currentScreen.Bounds.Height;
form.StartPosition = FormStartPosition.Manual;
form.Location = currentScreen.Bounds.Location;
Point p = new Point(currentScreen.Bounds.Location.X, currentScreen.Bounds.Location.Y);
form.Location = p;
form.WindowState = FormWindowState.Maximized;
form.Show();
If you disable form borders and have a PictureBox docked to "Fill" displaying your selected image you will achieve your intended goal and cover your tablet screen with an image.

Related

WinForms: How to fix incorrect scaling of Forms in high-DPI, multi-monitor environments (PerMonitorV2) with different resolution/scaling

In a Winforms app, I followed the general guidance in how-to-write-winforms-code-that-auto-scales-to-system-font-and-dpi-settings and in .Net Framework high-dpi support to enable PerMonitorV2 DPI Scaling. I am using .NET Framework 4.8 on a system post Windows 10 Anniversary Update (1607) to make use of the latest high DPI support features.
The DPI scaling looks great if the app is started on the primary monitor or any monitor with the same scaling as the primary monitor but the scaling is completely wrong if any Form (the main Form of the app or a secondary top level Form) is first shown on a display with different DPI than the primary monitor. For example, if the app is started on a 4k/250% scaling laptop screen (with four other monitors at 1920x1080/100% scaling) then the Form gets displayed at 1:1 scaling on the 4k screen and shows up as a tiny 1 square inch on that screen (with only the Title and MenuBar correctly scaled): Image Showing Bad Scaling.
The issue appears to be caused by the fact that the CurrentAutoScaleDimensions of the Form are not being set correctly in these cases. They appear to be set to the current "dimensions" of the primary screen and not the screen that the Form is being shown on. However, if the Form is first shown on the primary screen and then moved to a screen with different DPI, the CurrentAutoScaleDimensions do get correctly updated to reflect the actual DPI of the destination screen and the Form gets scaled correctly. So, for example, if I set the primary screen to be the 4k/250% screen and then start the app on a 1920x1080/100% screen, the CurrentAutoScaleDimensions (incorrectly) get set to those for the 4k/250% screen and result in the Form being extremely overscaled. But if I start the app on the primary 4k/250% screen, it gets scaled correctly when first shown and then also scales correctly as it is dragged to other monitors (and back). In summary, when a Form is first shown, the CurrentAutoScaleDimensions seem to be always getting set to the primary monitor dimensions and not the dimensions of the screen that the Form is being shown on.
Does anyone know of remedy for this situation?
The best solution I have been able to come up with is to force the application to always start up on the Primary Screen regardless of what screen it was launched from. This seems to fix all issues with initial scaling of the main Form and all subsequent scaling as it is dragged between monitors seems fine:
public Form1()
{
this.Font = SystemFonts.IconTitleFont;
// Force the main Form of the app to always open on the primary screen
// to get scaling to work.
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
this.StartPosition = FormStartPosition.Manual;
InitializeComponent();
...
For secondary Forms generated by the application, I have found that they get scaled correctly if I first show them on the primary screen, then hide and redisplay them on the screen containing the application's main Form (where I want them to be displayed). In some cases, I found it was sufficient to just call f.PerformLayout() rather than actually showing the form on the primary screen, but that did not work in all cases.
private void secondFormToolStripMenuItem_Click(object sender, EventArgs e)
{
FormExtra f = new FormExtra(this);
// First display the Form on the primary screen to make scaling correct
f.StartPosition = FormStartPosition.Manual;
f.Location = Screen.PrimaryScreen.WorkingArea.Location;
f.Show();
// Then hide it and move it where we really want it
f.Hide();
f.StartPosition = FormStartPosition.CenterParent;
f.ShowDialog();
}
This solution is not ideal in that the form briefly flashes on another screen which seems rather kludgy. However, this solution does seem to result in correct scaling of all Forms in the application regardless of what monitor the app is started on. I have tested it on two different multi-monitor configurations so far.

Showing Winforms on dual-screen setup

I have a winforms application in C#, with about 10 forms to navigate through.
On all of them, I have set StartPosition as CenterScreen but I use a laptop with a second screen plugged in.
Now the application starts randomly on one of my screens. Also even if the app is on one screen, MessageBoxes pop up on the second one. How can I set the application to always start at the main screen, the one set as "1" in Windows.
I also want to bind the application to the screen it is showing at so that MessageBoxes would appear on the same screen as the app. Another thing I would like to have is so that new forms would show on the same position as previous forms, not going back to the screen where the application started.
What should I use to control the positions of the forms?
I tried changing StartPosition to CenterParent, but that doesn't seem to change anything.
Try this out on starting page ( first form):
private void MyForm_Load(object sender, EventArgs e)
{
this.Location = Screen.AllScreens[0].WorkingArea.Location;
this.StartPosition = FormStartPosition.CenterScreen;
}
More information here : Showing a Windows form on a secondary monitor?
This question is about showing program on secondary screen. just change 1 index to 0 on arrays if you used answers of that.
Edit:
Just for emphasize Bryce Wagner comment for other people read this question :
If you're calling it from a form or user control, you'll want to call MessageBox.Show(this, "Message") instead.
Combining all answers here and in similar questions, the working solution for showing app on primary screen is this:
private void FormLogin_Load(object sender, EventArgs e)
{
this.Location = Screen.AllScreens[0].WorkingArea.Location;
ReallyCenterToScreen();
}
protected void ReallyCenterToScreen()
{
Screen screen = Screen.FromControl(this);
Rectangle workingArea = screen.WorkingArea;
this.Location = new Point()
{
X = Math.Max(workingArea.X, workingArea.X + (workingArea.Width - this.Width) / 2),
Y = Math.Max(workingArea.Y, workingArea.Y + (workingArea.Height - this.Height) / 2)
};
}
And to keep the MessageBoxes on the center screen is this:
MessageBox.Show(this, "Message")
Sources are the comment of #Bryce Wagner to my question, the answer from #Mohamad Shahrestani and from this question answer by #Sarsur.A
I have a laptop with a second screen. Windows 10 is in "Extend" mode. None of the code examples helped with my dialog ignoring the manual location.
It turns out that NVidia has some helpful options in their driver. One will decide for you where you want your form or your dialog. If your code just doesn't seem to behave, try the "nView Desktop Manager" -> "Windows Manager" -> "Dialog box repositioning" and Open windows on settings.

Making the image go full screen in slideshow application in winforms

I have an winforms application for slideshow of images.im using a list for storing the list of image paths,picture box to display image and on click of [slideshow] button..the slideshow happens...everything is working fine but i have one problem with full screen of image... on click of slideshow,image should open in full screen same like windows image viewer. please help me,keep in mind,only image should go full screen.not the full form.
i got below code in almost all the sites but Windowstate shows an error.
targetForm.WindowState = FormWindowState.Maximized;
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.TopMost = true;
targetForm.Bounds = Screen.GetBounds(targetForm);
please help me out.
thanks in advance.
I think you are trying to use different layouts for windowed and full screen mode.
When you switch to full screen you can:
programatically change PictureBox to fill the form with pictureBox.Dock = DockStyle.Fill
open another form (in full screen) that contains only the PictureBox that fills the form
Note:
First method won't work as expected if you have other controls with Dock set to something else than DockStyle.None

Getting a text area in the system tray

i was just wondering what i need to research to be able to have a programme that is in the system tray, when the user clicks the programme icon, just above the system tray a small text area appears allowing the user to type in a search condition. There is plenty of resources for c# and getting your programme in the system tray, but then it just opens as normal, which is not quite what i am looking for.
Thanks
One way to accomplish this is to use a standard WinForms window which contains a single text box and has no border. This window can then be displayed and positioned as normal (likely using many of the existing samples) but will appear as a floating text box.
var form = new MyTextBoxForm();
form.FormBorderStyle = BorderStyle.None;
form.StartPosition = FormStartPosition.Manual;
// position the form
form.ShowDialog();
Handle the NotifyIcon.Click event and show your form in the desired location.
For example:
var screen = Screen.PrimaryScreen;
form.Left = screen.WorkingArea.Right - form.Width;
form.Top = screen.WorkingArea.Bottom - form.Height;
Maybe with this Make your program in the system + add a menu you could try editing the menu, like you'd do a regular menu with toolstrips.... and change the label by a textbox.
Just a random idea.

How do I determine which monitor my .NET Windows Forms program is running on?

I have a C# Windows application that I want to ensure will show up on a second monitor if the user moves it to one. I need to save the main form's size, location and window state - which I've already handled - but I also need to know which screen it was on when the user closed the application.
I'm using the Screen class to determine the size of the current screen but I can't find anything on how to determine which screen the application was running on.
Edit: Thanks for the responses, everyone! I wanted to determine which monitor the window was on so I could do proper bounds checking in case the user accidentally put the window outside the viewing area or changed the screen size such that the form wouldn't be completely visible anymore.
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.
If you remember the window's location and size, that will be enough. When you set the position to the previously used position, if it happened to be on the second monitor it will go back there.
For example, if you have 2 monitors, both sized 1280x1024 and you set your window's left position to be 2000px, it will appear on the second monitor (assuming the second monitor is to the right of the first.) :)
If you are worried about the second monitor not being there when the application is started the next time, you can use this method to determine if your window intersects any of the screens:
private bool isWindowVisible(Rectangle rect)
{
foreach (Screen screen in Screen.AllScreens)
{
if (screen.Bounds.IntersectsWith(rect))
return true;
}
return false;
}
Just pass in your window's desired location and it will tell you if it will be visible on one of the screens. Enjoy!
You can get the current Screen with
var s = Screen.FromControl(this);
where this is the Form (or any control on the Form). As about how to remember that is a little tricky, but I would go for the index in the Screen.AllScreens array, or maybe s.DeviceName. In either case, check before using the settings on startup, to prevent using a monitor that was disconnected.
The location of the form will tell you which screen the form is on. I don't really understand why you'd need to know what screen it is on, because if you restore it using the location you saved it should just restore to the same location (maybe you can expand as to why).
Otherwise you can do something like this:
Screen[] scr = Screen.AllScreens;
scr[i].Bounds.IntersectsWith(form.Bounds);
Each screen has a Bounds property which returns a Rectangle. You can use the IntersectsWith() function to determine if the form is within the screen.
Also, they basically provide a function that does this as well on the Screen class
Screen screen = Screen.FromControl(form);
You can use the 'Screen' object:
System.Windows.Forms.Screen
Start playing with something like this:
Screen[] screens = Screen.AllScreens;
for (int i = 0; i < screens.Length ; i++)
{
Debug.Print(screens[i].Bounds.ToString());
Debug.Print(screens[i].DeviceName);
Debug.Print(screens[i].WorkingArea.ToString());
}
It may get you what you need

Categories