⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 suititlebar.pas

📁 SUIPack是一款为Delphi和C++Builder开发的所见即所得的界面增强VCL组件
💻 PAS
📖 第 1 页 / 共 4 页
字号:
    ParentForm : TCustomForm;
begin
    inherited;

    if csDesigning in ComponentState then
        Exit;
    if m_InButtons <> -1 then
    begin
        if m_Buttons.Items[m_InButtons].ButtonType = suiControlBox then
        begin
            ParentForm := GetParentForm(self);
            if ParentForm <> nil then
                ParentForm.Close();
        end;

        Exit;
    end;

    for i := 0 to m_Buttons.Count - 1 do
    begin
        if (m_Buttons.Items[i].ButtonType = suiMax) and
            m_Buttons.Items[i].Visible then
        begin
            m_Buttons.Items[i].DoClick();
            Exit;
        end;
    end;
end;

destructor TsuiTitleBar.Destroy();
begin
    m_Buttons.Free();
    m_Buttons := nil;

    m_Sections.Free();
    m_Sections := nil;

    m_DefPopupMenu.Free();
    m_DefPopupMenu := nil;

    m_Buf.Free();
    m_Buf := nil;

    m_Buf2.Free();
    m_Buf2 := nil;

    inherited Destroy();
end;

procedure TsuiTitleBar.DrawButtons(Buf: TBitmap);
var
    i : Integer;
    Btn : TsuiTitleBarButton;
    nControlBox : Integer;
    BtnRect : TRect;
    nLeft : Integer;
    nLeft2 : Integer;
    MousePoint : TPoint;
    BtnBuf : TBitmap;
    Cap : String;
    Icon : TIcon;
    FormBorderWidth : Integer;
    OutUIStyle : TsuiUIStyle;
begin
    if GetParentForm(self) = nil then
        Exit;
    nLeft := Buf.Width - m_RightBtnXOffset;
    FormBorderWidth := SendMessage(GetParentForm(self).Handle, SUIM_GETBORDERWIDTH, 0, 0) - 1;
    Inc(nLeft, FormBorderWidth);
    nControlBox := -1;

    BtnBuf := TBitmap.Create();

    if UIStyle = FromThemeFile then
    begin
        BtnBuf.PixelFormat := pf24Bit;
        BtnBuf.TransparentMode := tmFixed;
        BtnBuf.TransparentColor := m_TransColor;
    end;

    GetCursorPos(MousePoint);
    MousePoint := ScreenToClient(MousePoint);
    m_InButtons := -1;

    for i := 0 to m_Buttons.Count - 1 do
    begin
        Btn := m_Buttons.Items[i];
        if not Btn.Visible then
            continue;
        if (Btn.m_PicNormal.Graphic = nil) or (Btn.m_PicMouseOn.Graphic = nil) or (Btn.m_PicMouseDown.Graphic = nil) then
            continue;

        BtnBuf.Width := Btn.PicNormal.Width;
        BtnBuf.Height := Btn.PicNormal.Height;
        BtnBuf.Transparent := Btn.Transparent;

        Btn.m_PicNormal.Graphic.Transparent := false;

        if Btn.ButtonType = suiControlBox then
        begin
            nControlBox := i;
            continue;
        end;

        if Btn.ButtonType <> suiClose then
            Dec(nLeft, Btn.PicNormal.Width + 1 + m_ButtonInterval)
        else
            Dec(nLeft, Btn.PicNormal.Width);
        if nLeft + Btn.PicNormal.Width + 1 > Buf.Width then
            nLeft := Buf.Width - Btn.PicNormal.Width - 1;

        BtnRect := Rect(nLeft, m_BtnTop, nLeft + Btn.PicNormal.Width, m_BtnTop + Btn.PicNormal.Height);
        if InRect(MousePoint, BtnRect) then
        begin
            m_InButtons := i;

            if m_MouseDown then
            begin
                if Btn.PicMouseDown <> nil then
                    BtnBuf.Canvas.Draw(0, 0, Btn.PicMouseDown.Graphic);
{$IFNDEF SUIPACK_D9UP}
                if (csDesigning in ComponentState) and InForm() then
                begin
                    if Btn.ButtonType = suiClose then
                        ShowWindow(GetParentForm(self).Handle, SW_HIDE);
                end;
{$ENDIF}
            end
            else
            begin
                if Btn.PicMouseOn <> nil then
                    BtnBuf.Canvas.Draw(0, 0, Btn.PicMouseOn.Graphic);
            end;
        end
        else
            BtnBuf.Canvas.Draw(0, 0, Btn.PicNormal.Graphic);

        Buf.Canvas.Draw(nLeft, m_BtnTop, BtnBuf);
    end;

    nLeft2 := m_LeftBtnXOffset - 4;
    Dec(nLeft2, FormBorderWidth);
    if nLeft2 < 0 then
        nLeft2 := 0;

    if nControlBox <> -1 then
    begin
        Btn := m_Buttons.Items[nControlBox];

        BtnRect := Rect(nLeft2, m_IconTop, nLeft2 + Btn.PicNormal.Width, m_IconTop + Btn.PicNormal.Height);
        
        if not m_DrawAppIcon then
        begin
            BtnBuf.Width := Btn.PicNormal.Width;
            BtnBuf.Height := Btn.PicNormal.Height;
            BtnBuf.Transparent := Btn.Transparent;

            if InRect(MousePoint, BtnRect) then
            begin
                m_InButtons := nControlBox;

                if m_MouseDown then
                    BtnBuf.Canvas.Draw(0, 0, Btn.PicMouseDown.Graphic)
                else
                    BtnBuf.Canvas.Draw(0, 0, Btn.PicMouseOn.Graphic);
            end
            else
                BtnBuf.Canvas.Draw(0, 0, Btn.PicNormal.Graphic);
            Buf.Canvas.Draw(nLeft2, m_IconTop - 1, BtnBuf);
            Inc(nLeft2, Btn.PicNormal.Width + 5);
        end
        else
        begin
            if InRect(MousePoint, BtnRect) then
                m_InButtons := nControlBox;

            BtnBuf.Height := 16;
            BtnBuf.Width := 16;
            Icon := TIcon.Create();
            if Application <> nil then
                Icon.Handle := CopyImage(Application.Icon.Handle, IMAGE_ICON, 16, 16, LR_COPYFROMRESOURCE)
            else
                Icon.Handle := LoadImage(hInstance, 'MAINICON', IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
            Buf.Canvas.Draw(nLeft2 + 3, m_IconTop, Icon);
            Icon.Free();
            Inc(nLeft2, 21);
        end;
    end;

    if nLeft2 = 0 then
        Inc(nLeft2, 6);

    if not m_Active then
        Buf.Canvas.Font.Color := m_InactiveCaptionColor;

    if m_DrawingCap <> '' then
        Cap := m_DrawingCap
    else
    begin
        if Buf.Canvas.TextWidth(Caption) > nLeft - nLeft2 then
        begin
            Cap := Caption;
            while (Buf.Canvas.TextWidth(Cap + '...') > nLeft - nLeft2) and (Cap <> '') do
                SetLength(Cap, Length(Cap) - 1);
            Cap := Cap + '...'
        end
        else
            Cap := Caption;
    end;

    if UsingFileTheme(FileTheme, m_UIStyle, OutUIStyle) then
    begin
        if (BidiMode = bdRightToLeft) and SysLocale.MiddleEast then
            Buf.Canvas.TextOut(nLeft - Buf.Canvas.TextWidth(Cap) - 10,  m_CapTop - 3, Cap)
        else
            Buf.Canvas.TextOut(nLeft2, m_CapTop - 3, Cap);
    end
    else
    begin
        if UIStyle = Protein then
            nLeft := 3
        else
            nLeft := 0;
        if (BidiMode = bdRightToLeft) and SysLocale.MiddleEast then
            Buf.Canvas.TextOut(nLeft - Buf.Canvas.TextWidth(Cap) - 10,  m_BtnTop + nLeft, Cap)
        else
            Buf.Canvas.TextOut(nLeft2, m_BtnTop + nLeft, Cap);
    end;

    BtnBuf.Free();
end;

function TsuiTitleBar.InForm: Boolean;
begin
    if (
        (Parent <> nil) and
        ((Parent is TsuiForm) or (Owner is TsuiMDIForm))
    ) then
        Result := true
    else
        Result := false;
end;

procedure TsuiTitleBar.SetDrawAppIcon(const Value: Boolean);
begin
    m_DrawAppIcon := Value;

    Repaint();
end;

procedure TsuiTitleBar.DrawSectionsTo(Buf: TBitmap);
var
    nLeft, nRight : Integer;
    TopOffset : Integer;
    Offset : Integer;
    i, w : Integer;
    Sec : TsuiTitleBarSection;
    Center : Integer;
    L, L2 : Integer;
    ButtonWidth : Integer;
    FiveSec : Boolean;
    R : TRect;
    Tmp, Tmp2 : TBitmap;
begin
    if GetParentForm(self) = nil then
        Exit;

    if InForm() then
    begin
        Offset := -1 * SendMessage(GetParentForm(self).Handle, SUIM_GETBORDERWIDTH, 0, 0);
        if Offset = 0 then
        begin
            if InMDIForm() then
                Offset := -4
            else
                Offset := -3;
        end;
        TopOffset := -3;
    end
    else
    begin
        Offset := 0;
        TopOffset := 0;
    end;

    ButtonWidth := 0;

    for i := 0 to m_Buttons.Count - 1 do
    begin
        if m_Buttons.Items[i].m_PicNormal <> nil then
            Inc(ButtonWidth, m_Buttons.Items[i].m_PicNormal.Width + m_ButtonInterval);
    end;

    m_Buf.Width := Buf.Width - Offset * 2;
    m_Buf.Height := Buf.Height - TopOffset;

    m_Buf2.Width := m_Buf.Width;
    m_Buf2.Height := m_Buf.Height;
    m_Buf2.Canvas.Brush.Color := m_FormColor;
    R := Rect(0, 0, m_Buf2.Width, m_Buf2.Height);
    m_Buf2.Canvas.FillRect(R);

    nLeft := Offset;
    nRight := Buf.Width - Offset;
    L2 := 0;
    Center := -1;
    FiveSec := false;

    for i := 0 to m_Sections.Count - 1 do
    begin
        Sec := m_Sections.Items[i];
        if Sec.Picture = nil then
            continue;
        if Sec.m_CapSec then
        begin
            if m_Sections.Count > i + 1 then
                w := m_Sections.Items[i + 1].Width
            else
                w := 0;
            m_DrawingCap := Caption;
            m_DrawingCap := FormatStringWithWidth(Buf.Canvas, m_DrawingCap, Buf.Width - m_LeftBtnXOffset - 21 - ButtonWidth - w);
            L := Buf.Canvas.TextWidth(m_DrawingCap) + (m_LeftBtnXOffset + 17 - L2);
            Buf.Canvas.StretchDraw(Rect(nLeft, TopOffset, nLeft + L, TopOffset + Sec.Picture.Height), Sec.Picture.Bitmap);
            m_Buf.Canvas.StretchDraw(Rect(nLeft - Offset, 0, nLeft + L - Offset, Sec.Picture.Height), Sec.Picture.Bitmap);
            nLeft := nLeft + L;
            FiveSec := true;
        end
        else if Sec.Align = suiLeft then
        begin
            Buf.Canvas.Draw(nLeft, TopOffset, Sec.Picture.Bitmap);
            m_Buf.Canvas.Draw(nLeft - Offset, 0, Sec.Picture.Bitmap);
            nLeft := nLeft + Sec.Picture.Width;
            L2 := Sec.Picture.Width;
        end
        else if Sec.Align = suiRight then
        begin
            Tmp := TBitmap.Create();
            Tmp.Assign(Sec.Picture.Bitmap);
            Tmp.PixelFormat := pf24Bit;
            Tmp2 := TBitmap.Create();
            Tmp2.PixelFormat := pf24Bit;
            Tmp2.Width := Tmp.Width;
            Tmp2.Height := Tmp.Height;
            Tmp2.Canvas.Brush.Color := m_FormColor;
            R := Rect(0, 0, Tmp2.Width, Tmp2.Height);
            Tmp2.Canvas.FillRect(R);

            Tmp.TransparentMode := tmFixed;
            Tmp.TransparentColor := m_TransColor;
            Tmp.Transparent := true;
            Tmp2.Canvas.Draw(0, 0, Tmp);
            Buf.Canvas.Draw(nRight - Sec.Picture.Width, TopOffset, Tmp2);

            Tmp.Free();
            Tmp2.Free();

            m_Buf.Canvas.Draw(nRight - Offset - Sec.Picture.Width, 0, Sec.Picture.Bitmap);
            nRight := nRight - Sec.Picture.Width;
        end
        else if Sec.Align = suiClient then
        begin
            Center := i;
        end;
    end;

    if Center > -1 then
    begin
        with m_Sections.Items[Center].Picture do
        begin
            Buf.Canvas.StretchDraw(Rect(nLeft, TopOffset, nRight, TopOffset + Bitmap.Height), Bitmap);
            m_Buf.Canvas.StretchDraw(Rect(nLeft - Offset, 0, nRight - Offset, Bitmap.Height), Bitmap);
        end;
    end;

    m_Buf2.Canvas.Draw(0, 0, m_Buf);

    if not FiveSec then
        m_DrawingCap := ''
    else
    begin
        if (Parent <> nil) and (Parent is TsuiForm) then
            TsuiForm(Parent).PaintFormBorder();
    end;
end;

procedure TsuiTitleBar.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
    Form : TCustomForm;
begin
    inherited;

    if Button = mbLeft then
    begin
        m_MouseDown := true;
        Repaint();
        Form := GetParentForm(self);
        if Form = nil then
            Exit;

    {$IFDEF SUIPACK_D9UP}
        if (csDesigning in ComponentState) and (Form.Parent <> nil) and (Form.Parent.ClassName = 'TFormContainerForm') then
            Exit;
    {$ENDIF}

        if (
            ((m_InButtons = -1) and
            (Form.WindowState <> wsMaximized)) or (csDesigning in ComponentState)
        ) then
        begin
            ReleaseCapture();
            SendMessage(Form.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
            m_MouseDown := false;
        end
    end
end;

procedure TsuiTitleBar.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
    inherited;

    Repaint();
end;

procedure TsuiTitleBar.MouseOut(var Msg: TMessage);
begin
    inherited;

    Repaint();
end;

procedure TsuiTitleBar.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
var
    Point : TPoint;
    InButtons : Integer;
begin
    inherited;

    if Button <> mbLeft then
        Exit;
    InButtons := m_InButtons;
    if InButtons <> -1 then
    begin
        if (
            (m_Buttons.Items[InButtons].ButtonType = suiControlBox) and
            (m_Buttons.Items[InButtons].ControlBoxMenu = nil)
        ) then
        begin
            Point.X := 0;
            Point.Y := Height;
            Point := ClientToScreen(Point);
            m_DefPopupMenu.Popup(Point.X, Point.Y);
        end
        else
        begin
            m_Buttons.Items[InButtons].DoClick();
            if (
                Assigned(m_OnBtnClick) and
                (m_Buttons.Items[InButtons].ButtonType = suiCustom)
            ) then
                m_OnBtnClick(self, InButtons)

            else if(
                Assigned(m_OnHelpBtnClick) and
                (m_Buttons.Items[InButtons].ButtonType = suiHelp)
            ) then
                m_OnHelpBtnClick(self, InButtons);
        end;
    end;
    m_MouseDown := false;
    Repaint();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -