从中心调整大小 [Winforms] [英] Resize from Center [Winforms]

查看:77
本文介绍了从中心调整大小 [Winforms]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式(父母和孩子)
子窗体的属性是 ControlBox:False, DoubleBuffer:True &FormBorderStyle:SizeableToolWindow(其余均为默认值).
我希望子窗体从中心(而不是从左上角)调整大小

i have 2 forms (parent & child)
child form's properties are ControlBox:False, DoubleBuffer:True & FormBorderStyle:SizeableToolWindow (rest all are default values).
i want child form to resize from center (and not from top left)

在子窗体 Resize 事件中,我有以下代码

In Child form Resize Event, i have following code

this.Location = System.Drawing.Point(x / 2 - this.Width / 2, y / 2 - this.Height / 2);
//tried this.CenterToParent();`

其中 x = 父表单的宽度和 y = 父表单的高度.

where x = parent form's width and y = parent form's height.

现在从父窗体显示子窗体后,在调整大小时闪烁很多,子窗体往往会回到原来的位置!
如何从中心创建平滑调整大小?
有没有办法将 Form 的调整大小原点更改为中心?
这个问题已经发布这里,但不明白解决方案

now after showing child form from parent form, while resizing it flickers a lot, child form tends to go back to its original location!
how to create smooth resize from center?
is there a way to change Form's resize origin to center?
this question is already posted here, but didn't understand solution

推荐答案

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnResize(EventArgs e)
        {
            this.Location = new System.Drawing.Point(this.Location.X / 2 - this.Width / 2, this.Location.Y / 2 - this.Height / 2);
            //tried this.CenterToParent();`
            base.OnResize(e);
        }

        const int WM_SYSCOMMAND = 0x112;
        const int SC_MAXIMIZE = 0xF030;
        const int SC_MAXIMIZE2 = 0xF032;

        protected override void WndProc(ref Message m)
        {
            if ((m.Msg == WM_SYSCOMMAND && m.WParam == new IntPtr(SC_MAXIMIZE)) || m.WParam == new IntPtr(SC_MAXIMIZE2))
            {
                this.Size = this.MaximumSize;
                m.Result = new IntPtr(0);
                //base.WndProc(ref m);
                //Eat the message so won't process further
            }
            else
            {
                m.Result = new IntPtr(0);
                base.WndProc(ref m);
            }
        }
    }
}

这是更好的链接:https://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc(v=vs.110).aspx

这篇关于从中心调整大小 [Winforms]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆