Form gets bigger after saving size and location properties - c#

Something is wrong with how settings are being saved/read from in Windows Forms apps.
It's not a problem with my code. But I don't know what's wrong.
Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(Properties.Settings.Default.Size.Width, Properties.Settings.Default.Size.Height);
this.Location = new Point(Properties.Settings.Default.Location.X, Properties.Settings.Default.Location.Y);
}
Form1_Closing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Location = this.Location;
Properties.Settings.Default.Size = this.Size;
Properties.Settings.Default.Save();
}
Now. You can see that the code is right.
Why then, does the Form get bigger and Bigger and BIgger and BIGger and BIGGer and BIGGEr and BIGGER every single time I reopen my app?
It's supposed to stay the same size! You know, since I saved it and everything... Why?

The Size property includes the non-client area of the form, which can fluctuate wildly depending on whether you have enabled themes, Aero, etc.
Try using the ClientSize property instead.

Related

C# Timer not working when using animated gif

I am busy with creating an GUI which should have some animations in it. My idea was to use an animated gif as background with controls on top. This all is working fine until I wanted to add an timer to update some values each second, then the timer does not work. If I set enabled setting of the picture box to false the timer is running. For me it sounds like having some performance issues, although 140mb of ram and only a few percent of CPU is used.
Both the picture box and timer are the standard from Visual Studio 2019, where I program in C#.
The animated gif is 50mb and is in the Systems.Windows.Forms.PictureBox.
The timer I have used is the standard timer: System.Windows.Forms.Timer
Is there an timer that does work in combination with the animated gif? Or is there a picture box which can process animated gifs with better performance? Or should I step out from an animated gif to something else?
Currently the code is nothing more then changing the text of a button:
Currently it is nothing more then changing the text of an button:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "1234";
}
private void timer1_Tick(object sender, EventArgs e)
{
button1.Text = "test";
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Start();
}
Settings of the timer and animated gifs etc are in the proporties of that container.
Thanks in advance.
Edit: allready tried compressing the gif, with this the image is still around 40m and has the same problem. Smaller will result in too much loss of quality. An real small animated gif does not give the issue. So is there a workaround to use the big animated gif? :)
To give an idea what I would like to achieve:
https://motionarray.com/stock-motion-graphics/hud-video-frame-345823
This I would like to use with buttons and sliders on top.
Firstly, I'd suggest trying to compress your animated gif down. 50MB is large, and a quick Google search will show you many different sites that will compress it down to something smaller. Having a gif that size in your program is giving you performance issues.
I quickly recreated a small example with an animated gif with two buttons and a timer control, and the timer was ticking fine without the background messing up its animation. Make sure you have the timer set to disabled in the Properties tab, and then simply set the timer to enabled when you want it to start ticking.
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "1234";
}
private void timer1_Tick(object sender, EventArgs e)
{
button1.Text = "test";
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

Windows10, A form's width size seems like keeping 1.25 times larger when I open the form

I want to restore the form's size and I used the below codes.
I got an idea from this thread.
And I found out on some machines (in particular, Windows10) a form's width keeps doubled when I reopen this specific form.
I'm guessing that it might be scaling issue of screen resolution but I can't reproduce it.
Since I didn't calculate but store the form's size directly, I have no idea where to look.
I'm going to use a remote assistant and try to figure out why it happened. I will share what I find.
private void Form_Load(object sender, EventArgs e)
{
this.RestoreWindowPosition();
}
private void SaveWindowPosition()
{
Properties.Settings.Default.ReviewFormState = this.WindowState;
if (this.WindowState == FormWindowState.Normal)
{
Properties.Settings.Default.ReviewFormLocation = this.Location;
Properties.Settings.Default.ReviewFormSize = this.Size;
}
else
{
Properties.Settings.Default.ReviewFormLocation = this.RestoreBounds.Location;
Properties.Settings.Default.ReviewFormSize = this.RestoreBounds.Size;
}
Properties.Settings.Default.ReviewFormHasSetDefaults = true;
Properties.Settings.Default.Save();
}
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
this.SaveWindowPosition();
}
private void RestoreWindowPosition()
{
if (Properties.Settings.Default.ReviewFormHasSetDefaults)
{
this.WindowState = Properties.Settings.Default.ReviewFormState;
this.Location = Properties.Settings.Default.ReviewFormLocation;
this.Size = Properties.Settings.Default.ReviewFormSize;
}
}
After a remote session
As I said earlier, I finished a remote session. I found out this is because of a scale issue. But still, it seems pretty strange. I've checked [display settings - scale and layout] menu and it was 125%. Then I open the form and repeat. I found the form size is growing. I think it was 1.25 times on the first try. What I did was I changed the scale on 100%, then did the same thing open and repeat. For this time, I didn't find anything; the form was in the same size.
My middle step conclusion is that I need to save a pure size of the form. I will translate according to the scale. Then I guess, I can get a real size of the form. I'm not really sure I will try and share what I find for letting someone out in the loop.
#Jimi and #Louis Go
I hope that one day I can help you too. Thank you for helping me.
Jimi:
Make your application DpiAware if haven't. Start from here: How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?, then take a look at the app.config settings and to High DPI support in Windows Forms (consider and test the PerMonitorV2 setup). Take into consideration the Form's AutoScaleMode. See the difference between Dpi and Font.

How to run winform with showing taskbar?

I have a C#.2017 project, Home form is none border (set in properties). It always start with maximize and startposition is windowsdefaultlocation/manual (I tried). I try many code but it still runs and hide taskbar.
I want to run the form in none border, maximize/full screen mode, the form doesn't hide the taskbar of windows 10.
Tried this links:
https://social.msdn.microsoft.com/Forums/windows/en-US/e81dc341-720e-474a-9c37-75eac3a130cb/howto-show-window-form-on-top-of-taskbar-in-every-resolution?forum=winforms
https://www.c-sharpcorner.com/UploadFile/shubham0987/display-app-in-full-screen-with-windows-taskbar/
How to display a Windows Form in full screen on top of the taskbar?
private void Form1_Load(object sender, EventArgs e)
{
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
//Screen currentScreen = Screen.FromHandle(this.Handle);
//this.Size = new System.Drawing.Size(currentScreen.Bounds.Width, currentScreen.Bounds.Height);
}
It doesn't help me anything. If you have some solution better help me please.
Much thank to all.
Setting a Forms WindowState to Maximized when your BorderStyle is none will always lay over the taskbar. This is a typical behaviour of a "Fullscreen" application.
However you are on the right track. If you want to have a semi-fullscreen experience without laying over the taskbar you have to set the Location and Size of your Form manually.
Important here is that you not only set these values manually but also take away control from the OS itself as it will always try to position a Form by some ruleset.
private void Form1_Load(object sender, EventArgs e)
{
//Hiding the Border to simulate a fullscreen-experience
this.FormBorderStyle = FormBorderStyle.None;
//Telling the operating system that we want to set the start position manually
this.StartPosition = FormStartPosition.Manual;
//Actually setting our Width, Height, and Location
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}
Just a little side node: You might want to think about people with multiple screens and on which screen your application should appear (maybe let the user decide by some setting etc).
Maybe this one is quite similar to something I need:
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size ;

How to make picturebox change image smooth and fast

I got a picture box that got an original image in my program. When i hover with my mouse over it it changes picture to another picture and when i leave it changes back.
But..
There is such a delay to change to the right picture if i hover over it or not. It takes like 1 second before it changes, What should i change to improve the speed of the change? This is the code i am using at the moment:
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Image = ABC_Bok.Properties.Resources.BokVänsterhörn_1;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = ABC_Bok.Properties.Resources.BokVänsterhörnet;
}
This is the third time today that I've seen this issue. MouseHover is raised when the mouse pointer STOPS OVER a control. If you want something to happen as soon as the mouse pointer goes over the control then you want MouseEnter, just as you're using MouseLeave for the change back again.

Why do my WinForms controls flicker and resize slowly?

I'm making a program where I have a lot of panels and panels in panels.
I have a few custom drawn controls in these panels.
The resize function of 1 panel contains code to adjust the size and position of all controls in that panel.
Now as soon as I resize the program, the resize of this panel gets actived. This results in a lot of flickering of the components in this panel.
All user drawn controls are double buffered.
Can some one help me solve this problem?
To get rid of the flicker while resizing the win form, suspend the layout while resizing. Override the forms resizebegin/resizeend methods as below.
protected override void OnResizeBegin(EventArgs e) {
SuspendLayout();
base.OnResizeBegin(e);
}
protected override void OnResizeEnd(EventArgs e) {
ResumeLayout();
base.OnResizeEnd(e);
}
This will leave the controls intact (as they where before resizing) and force a redraw when the resize operation is completed.
Looking at the project you posted, the flickering is really bad when you have the first tab selected, with the gradient-filled group boxes. With the second or third tab showing, there's hardly any flicker, if at all.
So clearly the problem has something to do with the controls you're showing on that tab page. A quick glance at the code for the custom gradient-filled group box class gives away the more specific cause. You're doing a lot of really expensive processing each time you draw one of the groupbox controls. Because each groupbox control has to repaint itself each time the form is resized, that code is getting executed an unbelievable number of times.
Plus, you have the controls' background set to "Transparent", which has to be faked in WinForms by asking the parent window to draw itself first inside the control window to produce the background pixels. The control then draws itself on top of that. This is also more work than filling the control's background with a solid color like SystemColors.Control, and it's causing you to see the form's pixels being drawn while you resize the form, before the groupboxes have a chance to paint themselves.
Here's the specific code I'm talking about from your custom gradient-filled groupbox control class:
protected override void OnPaint(PaintEventArgs e)
{
if (Visible)
{
Graphics gr = e.Graphics;
Rectangle clipRectangle = new Rectangle(new Point(0, 0), this.Size);
Size tSize = TextRenderer.MeasureText(Text, this.Font);
Rectangle r1 = new Rectangle(0, (tSize.Height / 2), Width - 2, Height - tSize.Height / 2 - 2);
Rectangle r2 = new Rectangle(0, 0, Width, Height);
Rectangle textRect = new Rectangle(6, 0, tSize.Width, tSize.Height);
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(r2);
gp.AddRectangle(r1);
gp.FillMode = FillMode.Alternate;
gr.FillRectangle(new SolidBrush(Parent.BackColor), clipRectangle);
LinearGradientBrush gradBrush;
gradBrush = new LinearGradientBrush(clipRectangle, SystemColors.GradientInactiveCaption, SystemColors.InactiveCaptionText, LinearGradientMode.BackwardDiagonal);
gr.FillPath(gradBrush, RoundedRectangle.Create(r1, 7));
Pen borderPen = new Pen(BorderColor);
gr.DrawPath(borderPen, RoundedRectangle.Create(r1, 7));
gr.FillRectangle(gradBrush, textRect);
gr.DrawRectangle(borderPen, textRect);
gr.DrawString(Text, base.Font, new SolidBrush(ForeColor), 6, 0);
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
if (this.BackColor == Color.Transparent)
base.OnPaintBackground(pevent);
}
And now that you've seen the code, red warning flags ought to go up. You're creating a bunch of GDI+ objects (brushes, pens, regions, etc.), but failing to Dispose any of them! Almost all of that code should be wrapped in using statements. That's just sloppy coding.
Doing all of that work costs something. When the computer is forced to devote so much time to rendering controls, other things lag behind. You see a flicker as it strains to keep up with the resize. It's no different than anything else that overloads a computer (like a computing the value of pi), it's just really easy to do so when you use as many custom drawn controls like you do here. Transparency is hard in Win32, and so is a lot of custom 3D painting. It makes the UI look and feel clunky to the user. Yet another reason that I don't understand the rush away from native controls.
You really only have three options:
Deal with the flicker. (I agree, this is not a good option.)
Use different controls, like the standard, built-in ones. Sure, they may not have a fancy gradient effect, but that's going to look broken half of the time anyway if the user has customized their Windows theme. It's also reasonably hard to read black text on a dark gray background.
Change the painting code within your custom controls to do less work. You may be able to get by with some simple "optimizations" that don't cost you any of the visual effects, but I suspect this is unlikely. It's a tradeoff between speed and eye candy. Doing nothing is always faster.
I successfully eliminate flicker when form resize using this code. Thanks.
VB.NET
Public Class Form1
Public Sub New()
Me.SetStyle(ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.SupportsTransparentBackColor, True)
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Me.Update()
End Sub
End Class
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Resize += Form1_Resize;
this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
this.Update();
}
}
So I ran into this same problem - my control with a transparent background was repainting like 34 times, and what worked for me was:
On my form that contained the control
protected override void OnResize(EventArgs e)
{
myControl.Visible = false;
base.OnResize(e);
myControl.Visible = true;
}
And the same in the control:
protected override void OnResize(EventArgs e)
{
this.Visible = false;
base.OnResize(e);
this.Visible = true;
}
This reduced the amount of repainting to 4, which effectively eliminated any flicker when the control was being resized.
Maybe a good solution for you will be to use Form.ResizeBegin and Form.ResizeEnd events.
On ResizeBegin set main panel visibility to false, on ResizeEnd set main panel visibility to true.
This way panels will not be redrawn while someone is resizing your form.
While hooking into ResizeBegin and ResizeEnd is the right idea, instead of hiding the main panel's visibility I'd instead delay any resize calculations until ResizeEnd. In this case, you don't even need to hook into ResizeBegin or Resize - all the logic goes into ResizeEnd.
I say this for two reasons. One, even though the panel is hidden, the resize operation will likely still be expensive and so the form won't feel as responsive as it should unless the resize calculations are delayed. Two, hiding the pane's contents while resizing can be jarring to the user.
I had the same problem.
It seams that this is happening because you are using rounded corners. When I set CornerRadius property to 0, the flickering was gone.
So far I have only found the following workaround. Not the nicest one, but it stops the flickering.
private void Form_ResizeBegin(object sender, EventArgs e)
{
rectangleShape.CornerRadius = 0;
}
private void Form_ResizeEnd(object sender, EventArgs e)
{
rectangleShape.CornerRadius = 15;
}
I Had A Similar Issue Like This. My Entire Form Was Resizing Slowly And The Controls Painted Them In An Ugly Manner. So This Helped Me:
//I Added This To The Designer File, You Can Still Change The WindowState In Designer View If You Want. This Helped Me Though.
this.WindowState = FormWindowState.Maximized;
And In The Resize Event, Add This Code To The Beginning
this.Refresh();

Categories

Resources