zoukankan      html  css  js  c++  java
  • 让窗体自适应屏幕分辨率的变化——已经比较准确的源代码

    我的上个帖子中的源代码有点问题,把窗体的尺寸调整放在了后面,应该放在前面,才能满足等比例放大的要求,现在改了过来,基本上应该比较正确了。以下源码可以直接引用,在你的窗体的FormCreate方法中调用

    FitDeviceResolution(Self);

    即可实现。

    //实现屏幕分辨率的自动调整
     procedure FitDeviceResolution(Sender: TForm);
      Const   //记录设计时的屏幕分辨率
        OriWidth=1125;
        OriHeight=844;
      Var
        i:Integer;     
        j: Integer;
        LocAnchors:Array of TAnchors;
        LocAlign:Array of TAlign;
        LocList:TList;
        LocFontSize:Integer;
        LocFont:TFont;
        LocFontRate:Double;
        LocCmp:TComponent;
        LocParentFont:Boolean;
      begin
        ScrResolutionRateH:=screen.height/OriHeight;
        ScrResolutionRateW:=screen.Width/OriWidth;
        if Abs(ScrResolutionRateH-1)<Abs(ScrResolutionRateW-1) then
          LocFontRate:=ScrResolutionRateH
        Else
          LocFontRate:=ScrResolutionRateW;
        LocList:=TList.Create;
        Try
          Try
            if (screen.width<>OriWidth)OR(screen.Height<>OriHeight) then
            begin
              Sender.Scaled:=False;
              For i:=Sender.ComponentCount-1 Downto 0 Do
              Begin
              LocCmp:=Sender.Components[i];
              If LocCmp Is TControl Then
              LocList.Add(LocCmp);    
              If  PropertyExists(LocCmp,'PARENTFONT') Then
              Begin
              LocParentFont:=Boolean(GetObjectProperty(LocCmp,'PARENTFONT'));
              LocParentFont:=False;
              End;          
              If PropertyExists(LocCmp,'FONT') Then
              Begin
              LocFont:=TFont(GetObjectProperty(LocCmp,'FONT')); 
              LocFontSize := Round(LocFontRate*LocFont.Size);
              LocFont.Size:=LocFontSize;
              End;       
              End;
              SetLength(LocAnchors,LocList.Count);
              SetLength(LocAlign,LocList.Count);
              For i:=0 to LocList.Count-1 Do
              With TControl(LocList.Items[i]) Do
              Begin
              LocAnchors[i]:=Anchors;
              LocAlign[i]:=Align;
              Align:=alNone;
              Anchors:=[akLeft,akTop];
              End;
              Sender.Top:=Round(Sender.Top*ScrResolutionRateH);
              Sender.Left:=Round(Sender.Left*ScrResolutionRateW);
              Sender.Height:=Round(Sender.Height*ScrResolutionRateH);
              Sender.Width:=Round(Sender.Width*ScrResolutionRateW);
              Sender.Font.size := Round(LocFontRate*Sender.Font.size);
              For i:=0 to LocList.Count-1 Do
              Begin
              With TControl(LocList.Items[i]) Do
              Begin
              Top:=Round(Top*ScrResolutionRateH);
              Left:=Round(Left*ScrResolutionRateW);
              Height:=Round(height*ScrResolutionRateH);
              Width:=Round(width*ScrResolutionRateW);
              End;
              End;
              For i:=0 to LocList.Count-1 Do
              TControl(LocList.Items[i]).Align:=LocAlign[i];
              For i:=0 to LocList.Count-1 Do
              TControl(LocList.Items[i]).Anchors:=LocAnchors[i];
            End;
          Except
            MyErrorDlg(LocCMP.Name);
          End;
        Finally
          LocList.Free;
        End;
      end;

  • 相关阅读:
    在定义SharePoint列表的SPD数据视图的时候需要注意的问题
    如何自定义改变SharePoint 中列表Web部件中所有行某列中的固定值为图片或其它HTML代码
    [C#3] 1扩展方法
    特效编辑器开发手记2——cocos2dx粒子系统的plist文件 深圳
    让人死去活来的cocos2dx安卓开发环境搭建(windows+eclipse+ndk 不用cygwin)【上图】 深圳
    《疾风》开发手记:NxOgre最新版本的搭建20111020 深圳
    Linux 操作系统下CPU多核心的绑定 深圳
    巧用Unix时间戳 深圳
    AS3加载AS2的swf文件报错 深圳
    GLUT函数说明(转载) 深圳
  • 原文地址:https://www.cnblogs.com/martian6125/p/9631290.html
Copyright © 2011-2022 走看看