在MDI应用程序的父窗体的中心打开模态窗口

如何解决在MDI应用程序的父窗体的中心打开模态窗口

| 我正在Winform应用程序上工作,想在父窗体的中心打开模式窗体。在Winform应用程序中有: MDI表单(作为启动表单打开,并充当所有容器) 单击MDI表单的菜单项之一时-打开MDI子表单 在第2步中打开的MDI Child的按钮之一上单击时,将打开-模态表单-我们需要在MDI Child表单的中心打开(在第2步中打开) 因此,对于在第一中心打开模态形式,我做的明显解决方案是
TestModalForm obj = new TestModalForm()
obj.StartPosition = FormStartPosition.CenterParent;
obj.showdialog(this);
但是上述解决方案无法正常工作,因为模式形式始终将MDI形式视为其父项。因此,我尝试第二种解决方案:在那儿,我在“模态加载”窗口中编写了将方法放置在中央的方法,如下所示:
        private void MakeWinInCenter()
        {
            if (this.Owner != null)
            {
                Form objParent = null;
                int TopbarHeight = 0;
                if (this.Owner.IsMdiContainer && this.Owner.ActiveMdiChild != null)
                {
                    objParent = this.Owner.ActiveMdiChild;
                    TopbarHeight = GetTopbarHeight(this.Owner);
                }
                else
                    objParent = this.Owner;

                Point p = new Point((objParent.Width - this.Width) / 2,(objParent.Height - this.Height) / 2);
                p.X += objParent.Location.X;
                p.Y += TopbarHeight + objParent.Location.Y;
                this.Location = p;
            }
            else
            {
              //If owner is Null then,we have reference of MDIForm in Startup Class - use that ref and opens win in center of MDI
                if (Startup.MDIObj != null)
                {
                    this.Left = Convert.ToInt32((Startup.MDIObj.Width - this.Width) / 2);
                    this.Top = Convert.ToInt32((Startup.MDIObj.Height - this.Height) / 2);
                }
            }

        }

        private int GetTopbarHeight(Form MDIForm)
        {
            int TopbarHeight = 0;
            MdiClient objMDIClient = null;
            foreach (Control ctl in MDIForm.Controls)
            {
                if (ctl is MdiClient)
                {
                    objMDIClient = ctl as MdiClient;
                    break;
                }
            }
            if (objMDIClient != null)
            {
                TopbarHeight = MDIForm.Height - objMDIClient.Size.Height;
            }
            return TopbarHeight;
        }
当以最大化的形式打开MDI表单时,上述解决方案是完美的。但是,当我们通过调整MDI表单的大小(即未以最大化的形式)或将MDI表单移动到其他屏幕进行检查时-如果有多个屏幕,上述解决方案将无法正常工作,并且不会在MDI子表单的中心打开模式表单 还看了这个问题,但对我的问题没有帮助。 任何人都可以有任何建议或解决方案来解决问题。 谢谢     

解决方法

        尝试这个:
TestModalForm.showdialog(this.MdiParent);
    ,        您的方法似乎过于复杂。为什么不这样做呢?
// All code goes in your MDI Child form

// Create the modal form
TestModalForm obj = new TestModalForm();

// Find center point of MDI Parent form
Point centerPoint = new Point(this.MdiParent.Top + this.MdiParent.Height / 2,this.MdiParent.Left + this.MdiParent.Width / 2);
// Set the location and show the modal form
obj.StartPosition = FormStartPosition.Manual
obj.Location = centerPoint;
obj.ShowDialog(this);
    ,        我认为你应该这样做
//for modal form set its strat position to centerparent like this

**this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;**

// this will open the modalform in center of parent form
    ,        在MDI格式中,
center to parent
不能正常工作,请参阅文档MSDN,始终建议使用
center to screen
,但是我为此使用了一种变通方法,毕竟它与位置定位有关,因此我为
center to parent
并在子窗体form8ѭ上使用它,以便每次加载孩子时都将其居中于父对象:
private void CenterToParentOverride()
   {
      this.Location = new Point(
          this.MdiParent.ClientSize.Width / 2 - this.Width / 2,this.MdiParent.ClientSize.Height / 2 - this.Height / 2);
   }
    ,        要在父级为MDI子级时在其父级的中心使用ѭ10来显示表单,可以使用以下代码 该代码应该从MDI子窗体中的按钮运行,并在MDI子窗体的中央以模式对话框形式显示另一个窗体:
var dialog = new Form(); //The form which you want to show as dialog
//this.Parent point to the MdiClient control which is the container of this
var p = this.Parent.PointToScreen(this.Location); 
p.Offset((this.Width - dialog.Width)/2,(this.Height - dialog.Height)/2);
dialog.StartPosition = FormStartPosition.Manual;
dialog.DesktopLocation = p;
dialog.ShowDialog();
在上面的代码中,由于当前表单是MDI子窗体,因此
this.Parent
指向the13ѭ控件,该控件是MDI子窗体的容器,因此我们可以使用其
PointToScreen
方法传递MDI子窗体(此)的位置来获取MDI子窗体的屏幕位置。 MDI的孩子。然后,如果使用MDI子项与对话框宽度和高度之差的一半来偏移该位置,并将对话框的“ 15”设置为“ 16”,然后使用“ 10”显示它。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res