我有一个表单,我希望以其父类为中心显示,使用类似的东西。
create table dbo.TestDate (
PolicyNumber varchar(50),
PolicyEffectiveDate datetime,
AnnualStatementLine decimal(5,1),
CGL money,
CGLTria money,
CPL money,
CPLTria money,
EO money,
EOTria money,
Fees money,
RiskState varchar(2)
)
INSERT INTO dbo.TestDate (PolicyNumber, PolicyEffectiveDate , AnnualStatementLine, CGL , CGLTria , CPL ,CPLTria ,EO ,EOTria ,Fees ,RiskState )
values ('ENV560000001-00','2018-01-11 23:21:00',17.1,2000,160,674,54,341,0,250,'TX'),
('ENV560000002-00','2018-01-11 00:56:00',17.1,0,0,3238,259,0,0,250,'NV'),
('ENV560000003-00','2018-01-12 01:10:00',17.1,0,0,6045,484,0,0,250,'ND'),
('ENV560000004-00','2018-01-14 01:18:00',17.1,0,0,0,0,0,0,0,'ND')
select * from dbo.TestDate
如果在应用程序处于最小化状态时触发此代码并且应用程序恢复时出现问题,则表单显示在屏幕的右上角,而不是以其父对象为中心。
有谁知道如何解决这个问题?
标准Windows MessageBox对话框的行为正确,当应用程序从最小化状态恢复时,对话框位于正确的位置。
答案 0 :(得分:3)
我的建议是"缓存"最小化父表单位置时(覆盖WndProc()
方法将允许在>>最小化之前缓存表单位置)。
private Point CachedLocation;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0112) // WM_SYSCOMMAND
{
if (m.WParam == new IntPtr(0xF020)) // SC_MINIMIZE
{
// save the form location beofore it is minimized
CachedLocation= this.Location;
}
m.Result = new IntPtr(0);
}
base.WndProc(ref m);
}
现在,如果在调用子表单时最小化父表单,请使用缓存位置点(通过选中FormWindowState
Enum):
private void button1_Click(object sender, EventArgs e)
{
Form f = new Form();
if (this.WindowState == FormWindowState.Minimized)
{
f.Top = (CachedLocation.Y + (this.Height / 2)) - f.Height / 2;
f.Left = (CachedLocation.X + (this.Width / 2)) - f.Width / 2;
f.StartPosition = FormStartPosition.Manual;
f.ShowDialog();
}
else
{
f.StartPosition = FormStartPosition.CenterParent;
f.ShowDialog();
}
}