Form Shrinking Once upon KeyDown

Its Hard 1 Reputation point
2021-09-27T15:20:54.71+00:00

My Program resizes once smaller with smaller pictureboxes and buttons when I receive a keypress, I reproduced the behavior here, any ideas? Visual Studio 2019

Here is the bug problem https://drive.google.com/file/d/1e2h4UhgOosk6u7sl21T7EVSqDpCDXqaH/view?usp=sharing it on my PC it shrinks about 20% once on keypress

EDIT: The behaviour stopped by rebooting after setting the display scaling to 100% for both monitors in Start>Display Settings>Scale and layout. I needed to reboot after then rebuild project. But the problem is still there when I scale the display again.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;

namespace tesatbug
{
public partial class Form1 : Form
{
MediaPlayer audio1;

    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, Keys keyData)
    {
      // audio1 = new System.Windows.Media.MediaPlayer(); //uncomment to reproduce form shrink
        MessageBox.Show("cheese");  return true;

    }
        public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,174 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-09-28T02:23:21.373+00:00

    If you need to change the size of the form, you can modify its width and height properties and use a loop to make it an animation.

                for (int i = 0; i < 20; i++)  
                {  
                    this.Height -= 7;  
                    this.Width -= 10;  
                    Thread.Sl*eep(10);  
                }  
    

    You can also consider using WPF, which has rich animation effects.

    <Grid>  
            <Button Content="Click" Width="100" Height="30">  
                <Button.Triggers>  
                    <EventTrigger  RoutedEvent="Button.Click">  
                        <EventTrigger.Actions>  
                            <BeginStoryboard >  
                                <Storyboard  RepeatBehavior="Forever" AutoReverse="False">  
                                    <DoubleAnimation  Storyboard.TargetName="myWindow"  
                                          Storyboard.TargetProperty = "(Window.Height)"  
                                            To="300" Duration="0:0:5"/>  
                                    <Storyboard  RepeatBehavior="Forever" AutoReverse="False">  
      
                                        <DoubleAnimation  Storyboard.TargetName="myWindow"  
                                          Storyboard.TargetProperty = "(Window.Width)"  
                                            To="300" Duration="0:0:5"/>  
                                    </Storyboard>  
                                </Storyboard>  
                            </BeginStoryboard>  
      
                        </EventTrigger.Actions>  
                    </EventTrigger>  
                </Button.Triggers>  
            </Button>  
        </Grid>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments