在onresize里面写form1.postion:=poScreenCenter;报错?
    出现 'can't change visible in OnShow and Onhide ‘错误!
    写在Oncreate里面肯定是可以的,但为什么写onresize里面不行?
    form1.width和form1.heght都可以控制,form1.postion:=poScreenCenter;却报错?

解决方案 »

  1.   

    这明显不行啊,  只能在 OnCreate 里面。你在OnShow 里面 不能用这种方法的可以 Self.Left := (Screen.width - Self.Width ) div 2 这种方式
      

  2.   

    具体怎么居中我清楚,但是这个onresize事件是发生在什么时候,和show有什么关系了,具体能清楚给我讲解一下不?
      

  3.   

    好比在做一件矛盾的操作
     ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
    procedure TForm1.FormShow(Sender: TObject);
    begin
      Form1.Hide;
    end;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    在显示窗口时,隐藏窗口
      

  4.   


    经验只谈啊我以前也用过类似的语句,后来总是出错,就改为设置Self.Left的模式了
      

  5.   

    只有初始化的时候设置把,然后通过left 和 top 来设置的
      

  6.   

      
    具体在Onresize事件和form1.positon:=poScreenCenter是怎么个矛盾了?我的确不知道矛盾出在哪里?
    OnResize 就是重新给窗体和控件布局,但这句代码也是重新给窗体布局,也没什么冲突啊?
      

  7.   

    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;
      

  8.   

     楼上(爱蹄子的羊头)的源码研究了一遍,都是每个属性在delphi的实现的方法
     还是没看出什么门道?给指点指点,     
        小弟在此谢过了!
      

  9.   

      窗体产生的顺序:现oncreate-----onshow----onactivate,
      触发Oncreate时,窗体的一些初始化信息都已经准备好了,当然包括窗体大小位置信息只是没显示出来,  即hide。
      接着触发onshow事件,如果此时:form1.postion:=poScreenCenter或者form.visible:=false都会出现上述错误,也就是说form1.postion执行此句时操作了visible,具体上面没有看出操作visible的代码啊?
      

  10.   

    你看看 SetPostion 的 VCL 代码不就知道了.
      

  11.   

    procedure TCustomForm.SetWindowToMonitor;
    ...
    if FPosition = poScreenCenter then SetBounds(...)因为 form1.postion:=poScreenCenter 会改变 form1 的尺寸和位置,在 onResize 中
    执行到 form1.postion:=poScreenCenter 时会重新激活 onResize 事件,造成死循环。