C# GUI Window FORM application going out of the screen - c#

I have made a GUI app (WIN FORM) which is running fine on 12 inch screen(no cropping of the form) but on other Laptops having screen > 12 inches Win Form is going beyond the taskbar and some portion of the Form is not visible to the user.I have fixed it currently by squeezing certain UI boxes on the Form .But why this is happening?How can I auto-rectify it for all the PC models.

You may want to have a look at Form.AutoSize property. Also, in the link, look at AutoSizeMode.
However, to be able to fix this you may need to rewrite the whole form.

It sounds like a fixed sized dialog (at least I suppose it wouldn't be a problem with a resizable form).
You should adjust your form height to the desktop size (and maybe width too). The code below will shrink the form if it is too large and displays the scrollbar to make possible to access the remaining area.
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
AdjustHeight();
}
private void AdjustHeight()
{
Rectangle screen = Screen.FromControl(this).WorkingArea;
int screenHeight = screen.Height;
if (screenHeight < Height)
{
Height = screenHeight;
AdjustFormScrollbars(true); // not needed if AutoScroll property is true
}
}

Related

How can i get my form to resize based on the user's screen resolution, and also have the picture inside the picturebox scale with it?

I feel like i've tried everything to no avail. I have been trying to resize my form1 based on the user's screen resolution. I also want to resize the picturebox inside the form (and also the background image within it)
So i started with this
int sizeChange = Screen.PrimaryScreen.WorkingArea.Size.Height - Size.Height
this should get the difference between the current picture size and the actual resolution
Height += sizeChange;
Width += sizeChange;
canvas.Height += sizeChange;
canvas.Width += sizeChange;
Then, with this i am trying to modify the size of the form and height based on the size difference in form size and screen resolution. Am i going about this all wrong? I've been trying different things all week (this is most recent solution) and feel like nothing is working.

How to create an 1 pixel wide window using C# and WinForm

I would like to create an application that have a small window displayed at the bottom corner of desktop. On startup, the window shall be very small and ideally, just a couple of pixels in width.
Here is the code I used to do it:
public partial class DurationT64 : Form
{
private Size fullSize;
private Point fullPos;
private Point compactPos;
public DurationT64()
{
InitializeComponent();
var workingArea = Screen.PrimaryScreen.WorkingArea;
this.MinimumSize = new Size(0, this.Height);
this.MaximumSize = new Size(this.Width, this.Height);
// fullPos: the window location when it is in full size form.
fullPos = new Point(workingArea.Right - this.Width, workingArea.Bottom - this.Height);
this.Location = fullPos;
// fullSize: the size of the windown when it is in full size form.
fullSize = new Size(this.Width, this.Height);
this.Size = fullSize;
// compactPos: the window location when it is in compact size form.
compactPos = new Point(workingArea.Right - 30, fullPos.Y);
this.Width = 1;
this.Location = compactPos;
}
}
As you can see that in this example, I intended to create a window of just 1 pixel in width, placed closed to the right edge of the primary monitor.
However, I realized that the window doesn't go as small as I was expected. It goes down to 20 pixels wide but no less than that. Please refer to this screen capture image below for example:
an image shows that the window is wider than it suppose to be
I did some research regards to this problem and noticed that there was a solution proposed by Zach Johnson (#zach-johnson) back in 2009. Here is the link to it Overcome OS Imposed Windows Form Minimum Size Limit.
However, nether methods proposed in that link (the intercepting WM_ message one proposed by Zach and the SetBoundsCore one proposed by #Ace) works for me.
Can anyone please give me some solution to this question? Preferably, a solution purely based on C#/Winform and does not rely on native Win32 window message loop, if possible.
Many thanks!
It is rather straight-forward, Winforms ensures that the window cannot be made smaller than the system-imposed minimum size of a window, exposed as the SystemInformation.MinWindowTrackSize property in .NET. This is a "safety" setting, it ensures that the user cannot make the window too small when he resizes it, thus losing control over it. Same consideration applies to code.
Bypassing this limit requires no magic, you need to do two things:
Set the FormBorderStyle property to None so the user cannot resize the window.
Set the size after the window is created. The Load event is best.
Some comments about your existing code: be careful about tinkering with the Width/Height/Size properties, you are doing too much of it in your constructor and it cannot work correctly. In the constructor they don't yet match the actual size of the window. And will not be close at all on modern machines with high-resolution monitors, auto-scaling to match the DPI of the video adapter is important today. You have to postpone until the window is created and scaling is complete, the Load event is the proper place for code like this. One of the few reasons to actually use Load.
And note that your Location property calculation is inadequate, it does not consider the location of the taskbar. It doesn't work on my machine, I like the taskbar on the right.
Minimum repro:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}
protected override void OnLoad(EventArgs e) {
this.Width = 1;
base.OnLoad(e);
}
}
Do keep in mind that you'll need hawk-eyes to find it back on the screen :)

What's the safest way to adjust my WinForms application main form starting size and position at run time?

A WinForms application of mine makes serious use of screen area and would better be given as much as possible. At the same time I would not like to ignore small screen users. I also find it a bad manner to make an application to start in maximized mode by default. Moreover, I seek to deliver reasonable behavior without need to store and read configuration in files/registry.
So what I seek to do is to set the main form size at the application start time, setting it to something like Width = Screen.PrimaryScreen.WorkingArea.Width * 85 / 100; (and the same thing with height).
And this works but the problem is that the form bottom and right edges usually go beyond the screen in this case even though it is perfectly possible to fit the screen nicely if positioned properly.
What is the way to adjust the main form position effectively?
Maybe you did not set the formposition.
First , Set
this.StartPosition = FormStartPosition.Manual;
If you didn't set it , the this.locatin command won't ever work.
Next
this.Location = new Point(0, 0);
It should works
Set the Location to center the form based on the size you've chosen:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Width = Screen.PrimaryScreen.WorkingArea.Width * 85 / 100;
this.Height = Screen.PrimaryScreen.WorkingArea.Height * 85 / 100;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width * 15 / 200, Screen.PrimaryScreen.WorkingArea.Height * 15 / 200);
}
Be sure StartPosition is set to manual.
I usually attempt to load an application with the same position and size it had when it last run, with this information persisted in the registry.
There are a few complications in trying to do this - the screen resolution may have changed since the app last ran, etc. To do so you might need to:
handle SizeChanged and remember the size of the form whenever its WindowState is Normal (no point in remembering it when it's maximized or minimized.
similarly remember its location whenever it's moved.
persist this information when the app is closed
on startup, use the persisted information to position and size the form, adjusted to ensure the whole form fits on the screen and has a sensible minimum size.

Windows mobile autoresize with panels

I've got an app which will run on two different devices - one with a screen size of 240x320, the other 480x640.
For all forms bar one the VS generated code is fine:
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
For one form i'm capturing a signature. I'm doing this by a panel with a graphics handler; capturing mouse down and move events; this generates a list of vector points which I can draw lines with.
On the smaller res screen this is fine. On the higher res, I can't display my lines.. and I think this is because the panel is beyond the windows form size.
The form is created with a size of 240 x 268; a standard size I think - i've not manually set it, VS does this for me.
In order to get the panel in the right spot on the high res device, the co-ordinates are 3, 290; ie, 290 is past 268. Also the width of the panel is 448 which is somewhat larger than 240.
I'm using .net 2.0 (can't use later). I think I need to resize the form to make it larger but I do want to keep the existing re-sizing for the other controls on the form.
I'm not sure how to do this.
Make the form dock to fill, then use the Anchor properties to ensure controls inside the form resize as expected.
If you want the option of customizing how an individual control resizes, then DONT set the anchor properties on it, and instead handle the Resize event and perform custom resizing/repositioning within code there.
eg
private void form_Resize(object sender, EventArgs e)
{
// Center the control without changing width. Other controls are anchored.
this.control.Left = (this.Width - this.control.Width) / 2;
}
I'm writing this answer for the benefit of those who may have a similar problem in the future. PaulG pointed me in the right direction but I found the root cause to be something else.
The PDA project i've got uses "FormFactor WindowsMobile 6 Classic" which has a default size of 240 x 268.
Changing this to "Windows Mobile 6 Professional VGA" created a much larger form size.
This allowed me to get things positioned correctly for the larger size; then AutoScaleMode to DPI; and manually resizing the panel smaller made it all work.
IE, going from larger to smaller was easy; I didn't get smaller to larger working.

WM6 .Net Form Effects C#

I would like to know if it's possible to create nice form effects on the compact framework.
My plan is that when a user selects an image on the main form this is opened in a new form, this currently works. What I now want to do is make the form that contains the fullsize picture to load off the edge (left or right) of the screen at around 4 pixels high then slide into view. Once the form is fully on the screen then expand the height until it hits the max for the screen.
On close i would like to reduce the height back down to the 4 pixels high and the slide off the edge again before disposing the form.
I've tried the code below when instantiating the form and the dp.Top property was always 0 regardless of dp.Width == 240
DisplayPicture dp = new DisplayPicture(ImageUrl);
dp.WindowState = FormWindowState.Normal;
dp.Left = dp.Width * -1;
dp.Top = (dp.Height / 2) - 2;
dp.Height = 4;
dp.ShowDialog();
Within the DisplayPicture form i also have the following to try and move the form but as it isn't setting the Top property this code doesn't matter yet.
void t_Tick(object sender, EventArgs e)
{
if (this.Left < 0)
this.Left += 5;
if (this.Left > -1)
{
this.Left = 0;
if (this.Height < pictureBox1.ClientRectangle.Height)
{
this.Height += 4;
this.Top -= 2;
}
if ((this.Left == 0) && (this.Top == 0))
t.Enabled = false;
}
}
Any help would be greatly appreciated!
TIA
OneSHOT
To do this, start with a PictureBox control that has your image loaded. Set the Height to 4, the Width to the width of your form, and (very important) set the SizeMode of the PictureBox to StretchImage.
Next, position the PictureBox off the screen by setting Top to 0 and Left to -Width. Put a Timer control on your form with an interval of 100 (or whatever), and have its event gradually move the PictureBox to the right until its Left property is 0. Once you reach that point, have the timer event gradually increase the Height until it reaches the height of the form.
You'll probably have to deal with flicker, but this should get you started.
Update: I just read your question a little closer, and realized that you actually want to move the form itself from offscreen to full screen. This is not possible if you want the entire form (including the title bar at the top) to animate in this way, but you can sort of do this by setting the form's FormBorderStyle (or I think it's just called BorderStyle in the Compact Framework) to None. With the BorderStyle set to None, changing the Height, Width, Top and Left properties will actually have a visible effect on the form (although the form will be borderless). These properties are otherwise ignored completely in Windows Mobile, which is probably why your code didn't appear to be doing anything.
Update 2: here is my answer to a similar WM question, which may help you make your animated window look like a real window.

Categories

Resources