在onresize里面写form1.postion:=poScreenCenter;报错?

Panda_Wang_ 2012-01-09 02:09:22
在onresize里面写form1.postion:=poScreenCenter;报错?
出现 'can't change visible in OnShow and Onhide ‘错误!
写在Oncreate里面肯定是可以的,但为什么写onresize里面不行?
form1.width和form1.heght都可以控制,form1.postion:=poScreenCenter;却报错?
...全文
150 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Panda_Wang_ 2012-01-11
  • 打赏
  • 举报
回复
谢谢了!有道理,结贴。
npkaida 2012-01-11
  • 打赏
  • 举报
回复
procedure TCustomForm.SetWindowToMonitor;
...
if FPosition = poScreenCenter then SetBounds(...)

因为 form1.postion:=poScreenCenter 会改变 form1 的尺寸和位置,在 onResize 中
执行到 form1.postion:=poScreenCenter 时会重新激活 onResize 事件,造成死循环。

爱蹄子的羊头 2012-01-10
  • 打赏
  • 举报
回复


procedure TCustomForm.SetWindowToMonitor;
var
AppMon, WinMon: HMONITOR;
I, J: Integer;
ALeft, ATop: Integer;
LRect: TRect;
begin
if (FDefaultMonitor <> dmDesktop) and (Application.MainForm <> nil) then
begin
AppMon := 0;
if FDefaultMonitor = dmMainForm then
AppMon := Application.MainForm.Monitor.Handle
else if (FDefaultMonitor = dmActiveForm) and (Screen.ActiveCustomForm <> nil) then
AppMon := Screen.ActiveCustomForm.Monitor.Handle
else if FDefaultMonitor = dmPrimary then
AppMon := Screen.PrimaryMonitor.Handle;
WinMon := Monitor.Handle;
for I := 0 to Screen.MonitorCount - 1 do
if (Screen.Monitors[I].Handle = AppMon) then
if (AppMon <> WinMon) then
begin
for J := 0 to Screen.MonitorCount - 1 do
begin
if (Screen.Monitors[J].Handle = WinMon) then
begin
if FPosition = poScreenCenter then
begin
LRect := Screen.Monitors[I].WorkareaRect;
SetBounds(LRect.Left + ((RectWidth(LRect) - Width) div 2),
LRect.Top + ((RectHeight(LRect) - Height) div 2), Width, Height);
end
else
if FPosition = poMainFormCenter then
begin
SetBounds(Screen.Monitors[I].Left + ((Screen.Monitors[I].Width - Width) div 2),
Screen.Monitors[I].Top + ((Screen.Monitors[I].Height - Height) div 2),
Width, Height)
end
else
begin
ALeft := Screen.Monitors[I].Left + Left - Screen.Monitors[J].Left;
if ALeft + Width > Screen.Monitors[I].Left + Screen.Monitors[I].Width then
ALeft := Screen.Monitors[I].Left + Screen.Monitors[I].Width - Width;
ATop := Screen.Monitors[I].Top + Top - Screen.Monitors[J].Top;
if ATop + Height > Screen.Monitors[I].Top + Screen.Monitors[I].Height then
ATop := Screen.Monitors[I].Top + Screen.Monitors[I].Height - Height;
SetBounds(ALeft, ATop, Width, Height);
end;
end;
end;
end
else
begin
if FPosition = poScreenCenter then
begin
LRect := Screen.Monitors[I].WorkareaRect;
SetBounds(LRect.Left + ((RectWidth(LRect) - Width) div 2),
LRect.Top + ((RectHeight(LRect) - Height) div 2), Width, Height);
end;
end;
end;
end;

Panda_Wang_ 2012-01-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gxgyj 的回复:]
好比在做一件矛盾的操作
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
procedure TForm1.FormShow(Sender: TObject);
begin
Form1.Hide;
end;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在显示窗口时,隐藏窗口
[/Quote]
具体在Onresize事件和form1.positon:=poScreenCenter是怎么个矛盾了?我的确不知道矛盾出在哪里?
OnResize 就是重新给窗体和控件布局,但这句代码也是重新给窗体布局,也没什么冲突啊?
e8923704 2012-01-10
  • 打赏
  • 举报
回复
你看看 SetPostion 的 VCL 代码不就知道了.
Panda_Wang_ 2012-01-10
  • 打赏
  • 举报
回复
窗体产生的顺序:现oncreate-----onshow----onactivate,
触发Oncreate时,窗体的一些初始化信息都已经准备好了,当然包括窗体大小位置信息只是没显示出来, 即hide。
接着触发onshow事件,如果此时:form1.postion:=poScreenCenter或者form.visible:=false都会出现上述错误,也就是说form1.postion执行此句时操作了visible,具体上面没有看出操作visible的代码啊?
Panda_Wang_ 2012-01-10
  • 打赏
  • 举报
回复
楼上(爱蹄子的羊头)的源码研究了一遍,都是每个属性在delphi的实现的方法
还是没看出什么门道?给指点指点,
小弟在此谢过了!
jacknes009 2012-01-09
  • 打赏
  • 举报
回复
只有初始化的时候设置把,然后通过left 和 top 来设置的
hongss 2012-01-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 warrially 的回复:]
这明显不行啊, 只能在 OnCreate 里面。

你在OnShow 里面 不能用这种方法的

可以 Self.Left := (Screen.width - Self.Width ) div 2 这种方式
[/Quote]

经验只谈啊

我以前也用过类似的语句,后来总是出错,就改为设置Self.Left的模式了
gxgyj 2012-01-09
  • 打赏
  • 举报
回复
好比在做一件矛盾的操作
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
procedure TForm1.FormShow(Sender: TObject);
begin
Form1.Hide;
end;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在显示窗口时,隐藏窗口
Panda_Wang_ 2012-01-09
  • 打赏
  • 举报
回复
具体怎么居中我清楚,但是这个onresize事件是发生在什么时候,和show有什么关系了,具体能清楚给我讲解一下不?
爱蹄子的羊头 2012-01-09
  • 打赏
  • 举报
回复
这明显不行啊, 只能在 OnCreate 里面。

你在OnShow 里面 不能用这种方法的

可以 Self.Left := (Screen.width - Self.Width ) div 2 这种方式

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧