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

📄 jvqbutton.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 2 页
字号:
    if FForceSameSize then
      SetBounds(Left, Top, Width, Height);
  end;
end;

procedure TJvCustomGraphicButton.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
var
  Form: TCustomForm;
  Msg: TCMForceSize; 
  I: Integer; 
begin
  inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  if ForceSameSize then
  begin
    Form := GetParentForm(Self);
    if Assigned(Form) then
    begin
      Msg.Msg := CM_FORCESIZE;
      Msg.Sender := Self;
      Msg.NewSize.X := AWidth;
      Msg.NewSize.Y := AHeight;  
      for I := 0 to Form.ControlCount - 1 do
        if Form.Controls[I] is TJvCustomGraphicButton then
          TJvCustomGraphicButton(Form.Controls[I]).ForceSize(Self, AWidth, AHeight); 
    end;
  end;
end;

procedure TJvCustomGraphicButton.CMForceSize(var Msg: TCMForceSize);
begin
  with Msg do
    ForceSize(Sender, NewSize.x, NewSize.y);
end;

function TJvCustomGraphicButton.GetPattern: TBitmap;
begin
  Result := CreateBrushPattern;
end;

procedure TJvCustomGraphicButton.SetAllowAllUp(const Value: Boolean);
begin
  if FAllowAllUp <> Value then
  begin
    FAllowAllUp := Value;
    UpdateExclusive;
  end;
end;

procedure TJvCustomGraphicButton.SetGroupIndex(const Value: Integer);
begin
  if FGroupIndex <> Value then
  begin
    FGroupIndex := Value;
    UpdateExclusive;
  end;
end;

procedure TJvCustomGraphicButton.UpdateExclusive;
var
  Msg: TCMButtonPressed; 
  I: Integer; 
begin
  if (GroupIndex <> 0) and (Parent <> nil) then
  begin
    Msg.Msg := CM_BUTTONPRESSED;
    Msg.Index := GroupIndex;
    Msg.Control := Self;
    Msg.Result := 0;  
    for I := 0 to Parent.ControlCount - 1 do
      if Parent.Controls[I] is TJvCustomGraphicButton then
        TJvCustomGraphicButton(Parent.Controls[I]).ButtonPressed(Self, GroupIndex);
  end;
end;

procedure TJvCustomGraphicButton.CMButtonPressed(var Msg: TCMButtonPressed);
begin
  ButtonPressed(TJvCustomGraphicButton(Msg.Control), Msg.Index);
end;

procedure TJvCustomGraphicButton.SetHotFont(const Value: TFont);
begin
  FHotFont.Assign(Value);
end;

procedure TJvCustomGraphicButton.SetHotTrackFontOptions(const Value: TJvTrackFontOptions);
begin
  if FHotTrackFontOptions <> Value then
  begin
    FHotTrackFontOptions := Value;
    UpdateTrackFont(HotTrackFont, Font, HotTrackFontOptions);
  end;
end;

procedure TJvCustomGraphicButton.SetDropArrow(const Value: Boolean);
begin
  if FDropArrow <> Value then
  begin
    FDropArrow := Value;
    Invalidate;
  end;
end;

procedure TJvCustomGraphicButton.SetDropDownMenu(const Value: TPopupMenu);
begin
  if FDropDownMenu <> Value then
  begin
    FDropDownMenu := Value;
    if DropArrow then Invalidate;
  end;
end;

procedure TJvCustomGraphicButton.CMSysColorChange(var Msg: TMessage);
begin
  inherited;
  RepaintBackground;
end;

procedure TJvCustomGraphicButton.FontChanged;
begin
  inherited FontChanged;
  UpdateTrackFont(HotTrackFont, Font, HotTrackFontOptions);
end;

procedure TJvCustomGraphicButton.TextChanged;
begin
  inherited TextChanged;
  RepaintBackground;
end;

procedure TJvCustomGraphicButton.Click;
begin
  inherited Click;
  if GroupIndex <> 0 then
  begin
    if AllowAllUp then
      Down := not Down
    else
      Down := True;
  end;
end;

procedure TJvCustomGraphicButton.ButtonPressed(Sender: TJvCustomGraphicButton;
  AGroupIndex: Integer);
begin
  if AGroupIndex = GroupIndex then
    if Sender <> Self then
    begin
      if Sender.Down and Down then
      begin
        FDown := False;
        Exclude(FStates, bsMouseDown);
        RepaintBackground;
      end;
      FAllowAllUp := Sender.AllowAllUp;
    end;
end;

procedure TJvCustomGraphicButton.ForceSize(Sender: TControl; AWidth, AHeight: Integer);
begin
  if Sender <> Self then
    inherited SetBounds(Left, Top, AWidth, AHeight);
end;

//=== { TJvCustomButton } ====================================================

constructor TJvCustomButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FDropArrow := False;
  FHotTrack := False;
  FHotFont := TFont.Create;
  FFontSave := TFont.Create;
  // ControlStyle := ControlStyle + [csAcceptsControls];
  FWordWrap := True;
  FForceSameSize := False;
  FHotTrackFontOptions := DefaultTrackFontOptions;
end;

destructor TJvCustomButton.Destroy;
begin
  FHotFont.Free;
  FFontSave.Free;
  inherited Destroy;
end;

procedure TJvCustomButton.Click;
var
  Tmp: TPoint;
begin
  inherited Click;
  Tmp := ClientToScreen(Point(0, Height));
  DoDropDownMenu(Tmp.X, Tmp.Y);
end;

procedure TJvCustomButton.DrawDropArrow(Canvas: TCanvas; ArrowRect: TRect);
var
  I: Integer;
begin
  if not Enabled then
    Canvas.Pen.Color := clInactiveCaption
  else
    Canvas.Pen.Color := clWindowText;
  for I := 0 to (ArrowRect.Bottom - ArrowRect.Top) do
  begin
    if ArrowRect.Left + I <= ArrowRect.Right - I then
    begin
      Canvas.MoveTo(ArrowRect.Left + I, ArrowRect.Top + I);
      Canvas.LineTo(ArrowRect.Right - I, ArrowRect.Top + I);
    end;
  end;
end;




procedure TJvCustomButton.SetHotTrackFontOptions(const Value: TJvTrackFontOptions);
begin
  if FHotTrackFontOptions <> Value then
  begin
    FHotTrackFontOptions := Value;
    UpdateTrackFont(HotTrackFont, Font, HotTrackFontOptions);
  end;
end;

procedure TJvCustomButton.SetDropArrow(const Value: Boolean);
begin
  if FDropArrow <> Value then
  begin
    FDropArrow := Value;
    Invalidate;
  end;
end;

procedure TJvCustomButton.SetHotFont(const Value: TFont);
begin
  FHotFont.Assign(Value);
end;

procedure TJvCustomButton.SetDropDownMenu(const Value: TPopupMenu);
begin
  if FDropDownMenu <> Value then
  begin
    FDropDownMenu := Value;
    if DropArrow then
      Invalidate;
  end;
end;

procedure TJvCustomButton.MouseEnter(Control: TControl);
begin
  if not MouseOver then
  begin
    if FHotTrack then
    begin
      FFontSave.Assign(Font);
      Font.Assign(FHotFont);
    end;
    inherited MouseEnter(Control);
  end;
end;

procedure TJvCustomButton.MouseLeave(Control: TControl);
begin
  if MouseOver then
  begin
    if FHotTrack then
      Font.Assign(FFontSave);
    inherited MouseLeave(Control);
  end;
end;

procedure TJvCustomButton.FontChanged;
begin
  inherited FontChanged;
  UpdateTrackFont(HotTrackFont, Font, HotTrackFontOptions);
end;

function TJvCustomButton.GetRealCaption: string;
begin
  if WordWrap then
    Result := StringReplace(Caption, JvBtnLineSeparator, Lf, [rfReplaceAll])
  else
    Result := Caption;
end;

procedure TJvCustomButton.SetWordWrap(const Value: Boolean);
begin
  if FWordWrap <> Value then
  begin
    FWordWrap := Value;
    Invalidate;
  end;
end;

procedure TJvCustomButton.SetForceSameSize(const Value: Boolean);
begin
  if FForceSameSize <> Value then
  begin
    FForceSameSize := Value;
    if FForceSameSize then
      SetBounds(Left, Top, Width, Height);
  end;
end;

procedure TJvCustomButton.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
var
  Form: TCustomForm;
  Msg: TCMForceSize; 
//  I: Integer; 
begin
  inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  if ForceSameSize then
  begin
    Form := GetParentForm(Self);
    if Assigned(Form) then
    begin
      Msg.Msg := CM_FORCESIZE;
      Msg.Sender := Self;
      Msg.NewSize.X := AWidth;
      Msg.NewSize.Y := AHeight;  
      BroadcastMsg(Form, Msg);
//      for I := 0 to Form.ControlCount - 1 do
//        if Form.Controls[I] is TJvCustomButton then
//          TJvCustomButton(Form.Controls[I]).ForceSize(Self, AWidth, AHeight); 
    end;
  end;
end;

procedure TJvCustomButton.CMForceSize(var Msg: TCMForceSize);
begin
  with Msg do
    ForceSize(Sender, NewSize.x, NewSize.y);
end;

procedure TJvCustomButton.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent = FDropDownMenu) then
    DropDownMenu := nil;
end;

procedure TJvCustomGraphicButton.RepaintBackground;
var
  R: TRect;
begin
  if (Parent <> nil) and Parent.HandleAllocated then
  begin
    R := BoundsRect;
    InvalidateRect(Parent.Handle, @R, True);
  end;
  Repaint;
end;

procedure TJvCustomButton.ForceSize(Sender: TControl; AWidth, AHeight: Integer);
begin
  if Sender <> Self then
    inherited SetBounds(Left, Top, AWidth, AHeight);
end;

function TJvCustomButton.DoDropDownMenu(X, Y: Integer): Boolean;
var 
  Handled: Boolean;
begin
  Result := (DropDownMenu <> nil);
  if Result then
  begin
    DropDownMenu.PopupComponent := Self;
    case DropDownMenu.Alignment of
      paRight:
        Inc(X, Width);
      paCenter:
        Inc(X, Width div 2);
    end;
    Handled := False;
    if Assigned(FOnDropDownMenu) then
      FOnDropDownMenu(Self, Point(X, Y), Handled);
    if not Handled then
      DropDownMenu.Popup(X, Y)
    else
      Exit;  
    repeat
      Application.ProcessMessages;
    until not QWidget_isVisible(DropDownMenu.Handle); 
  end;
end;

//=== { TJvDropDownButton } ==================================================

constructor TJvDropDownButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 16;
  Height := 16;
end;

procedure TJvDropDownButton.Paint;
var
  PaintRect: TRect;
  DrawFlags: Integer;
  DC: HDC;
  Bmp: TBitmap;
begin
  // adjust FState and FDragging
  DC := Canvas.Handle;
  Bmp := TBitmap.Create;
  try
    Bmp.Width := 1;
    Bmp.Height := 1;
    Canvas.Handle := Bmp.Canvas.Handle;
    try
      inherited Paint;
    finally
      Canvas.Handle := DC;
    end;
  finally
    Bmp.Free;
  end;

  PaintRect := Rect(0, 0, Width, Height);
  DrawFlags := DFCS_SCROLLCOMBOBOX or DFCS_ADJUSTRECT;
  if FState in [bsDown, bsExclusive] then
    DrawFlags := DrawFlags or DFCS_PUSHED;
 
  begin 
    Canvas.Start;
    RequiredState(Canvas, [csHandleValid, csPenValid, csBrushValid]); 
    DrawFrameControl(Canvas.Handle, PaintRect, DFC_SCROLL, DrawFlags);
 
    Canvas.Stop; 
  end;
end;

procedure TJvCustomGraphicButton.DropDownClose;
begin
  if Assigned(FOnDropDownClose) then
    FOnDropDownClose(Self);
end;

{$IFDEF UNITVERSIONING}
const
  UnitVersioning: TUnitVersionInfo = (
    RCSfile: '$RCSfile: JvQButton.pas,v $';
    Revision: '$Revision: 1.30 $';
    Date: '$Date: 2005/02/06 14:06:01 $';
    LogPath: 'JVCL\run'
  );
{$ENDIF UNITVERSIONING}

initialization
  {$IFDEF UNITVERSIONING}
  RegisterUnitVersion(HInstance, UnitVersioning);
  {$ENDIF UNITVERSIONING}

finalization
  FreeAndNil(GlobalPattern);
  {$IFDEF UNITVERSIONING}
  UnregisterUnitVersion(HInstance);
  {$ENDIF UNITVERSIONING}

end.

⌨️ 快捷键说明

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