怎么才能取得panel的caption的left和top的值?就是取得caption文字距左和距顶部的值?

解决方案 »

  1.   

    它源代码是这么画的:
      Rect := GetClientRect;//获取Panel的矩形区
      with Canvas do
      begin
        if not ThemeServices.ThemesEnabled or not ParentBackground then
        begin
          Brush.Color := Color;
          FillRect(Rect);
        end;
        Brush.Style := bsClear;
        Font := Self.Font;
        FontHeight := TextHeight('W');//文字的高度
        with Rect do//计算文字的矩形区
        begin
          Top := ((Bottom + Top) - FontHeight) div 2;
          Bottom := Top + FontHeight;
        end;
        Flags := DT_EXPANDTABS or DT_VCENTER or Alignments[FAlignment];//使用了文字居中的标记
        Flags := DrawTextBiDiModeFlags(Flags);
        DrawText(Handle, PChar(Caption), -1, Rect, Flags);
      end;
    从上面源代码可以看出,文字的Rect的Top和Bottom是自己算的,而Left和Right则是用的Panel自身的。
    Canvas既然有TextHeight可以测出文本的高度,也有TextWidth可以计算文本的宽度。所以你完全可以自己计算。
      

  2.   

    panel的caption一般是水平居中 垂直居中
      

  3.   

    用Delphi 封装 的 Canvas.TextWidth  和 Canvas.TextHeight跟进去就可以看到用的什么API 了。