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

📄 jvcalendar.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 3 页
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is: JvCalendar.PAS, released on 2002-05-26.

The Initial Developer of the Original Code is Peter Th鰎nqvist [peter3 at sourceforge dot net]
Portions created by Peter Th鰎nqvist are Copyright (C) 2002 Peter Th鰎nqvist.
All Rights Reserved.

Contributor(s): Oliver Giesen [ogware att gmx dott net]

You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net

Description:
  A wrapper component for the MS MonthCal control available in
    ComCtl32.dll versions 4.70 and above.

Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvCalendar.pas,v 1.31 2005/02/18 14:17:23 ahuser Exp $

unit JvCalendar;

{$I jvcl.inc}
{$I windowsonly.inc}

interface

uses
  {$IFDEF UNITVERSIONING}
  JclUnitVersioning,
  {$ENDIF UNITVERSIONING}
  Windows, Messages, CommCtrl, Classes, Graphics, Controls, Forms,
  JvComponent, JvTypes, JvExControls;

type
  EMonthCalError = class(EJVCLException);
  TJvMonthCalWeekDay = (mcLocale, mcMonday, mcTuesday, mcWednesday, mcThursday, mcFriday, mcSaturday, mcSunday);
  TJvMonthCalSelEvent = procedure(Sender: TObject; StartDate, EndDate: TDateTime) of object;
  TJvMonthCalStateEvent = procedure(Sender: TObject; Date: TDateTime; Count: Integer; var DayStateArray: array of
    TMonthDayState) of object;

  TJvCustomMonthCalendar = class;

  TJvMonthCalColors = class(TPersistent)
  private
    Calendar: TJvCustomMonthCalendar;
    FBackColor: TColor;
    FTextColor: TColor;
    FTitleBackColor: TColor;
    FTitleTextColor: TColor;
    FMonthBackColor: TColor;
    FTrailingTextColor: TColor;
    procedure SetColor(Index: Integer; Value: TColor);
    function GetColor(Index: Integer): TColor;
    procedure SetAllColors;
  public
    constructor Create(AOwner: TJvCustomMonthCalendar);
    procedure Assign(Source: TPersistent); override;
  published
    property BackColor: TColor index 0 read GetColor write SetColor default clWindow;
    property TextColor: TColor index 1 read GetColor write SetColor default clWindowText;
    property TitleBackColor: TColor index 2 read GetColor write SetColor default clActiveCaption;
    property TitleTextColor: TColor index 3 read GetColor write SetColor default clWhite;
    property MonthBackColor: TColor index 4 read GetColor write SetColor default clWhite;
    property TrailingTextColor: TColor index 5 read GetColor write SetColor default clInactiveCaptionText;
  end;

  TJvMonthCalAppearance = class(TPersistent)
  private
    FCircleToday: Boolean;
    FShowToday: Boolean;
    FWeekNumbers: Boolean;
    FFirstDoW: TJvMonthCalWeekDay;
    FColors: TJvMonthCalColors;
    FBoldDays: TStringList;
    procedure SetColors(const AValue: TJvMonthCalColors);
    function GetBoldDays: TStrings;
    procedure SetBoldDays(const AValue: TStrings);
    procedure SetCalendar(const AValue: TJvCustomMonthCalendar);
    function GetCalendar: TJvCustomMonthCalendar;
    procedure SetCircleToday(const AValue: Boolean);
    procedure SetFirstDoW(const AValue: TJvMonthCalWeekDay);
    procedure SetShowToday(const AValue: Boolean);
    procedure SetWeekNumbers(const AValue: Boolean);
  public
    constructor Create;
    destructor Destroy; override;
    property Calendar: TJvCustomMonthCalendar read GetCalendar write SetCalendar;
  published
    property Colors: TJvMonthCalColors read FColors write SetColors;
    property CircleToday: Boolean read FCircleToday write SetCircleToday default True;
    property BoldDays: TStrings read GetBoldDays write SetBoldDays;
    property FirstDayOfWeek: TJvMonthCalWeekDay read FFirstDoW write SetFirstDoW default mcLocale;
    property ShowToday: Boolean read FShowToday write SetShowToday default True;
    property WeekNumbers: Boolean read FWeekNumbers write SetWeekNumbers default False;
  end;

  TMonthDayStateArray = array [0..11] of TMonthDayState;

  TJvCustomMonthCalendar = class(TJvWinControl)
  private
    FAppearance: TJvMonthCalAppearance;
    FOwnsAppearance: Boolean;
    FMultiSelect: Boolean;
    FMaxSelCount: Word;
    FMinDate: TDateTime;
    FMaxDate: TDateTime;
    FFirstSelDate: TDateTime;
    FLastSelDate: TDateTime;
    FMonthDelta: Integer;
    FToday: TDateTime;
    FBorderStyle: TBorderStyle;
    FOnSelect: TJvMonthCalSelEvent;
    FOnSelChange: TJvMonthCalSelEvent;
    FOnGetState: TJvMonthCalStateEvent;
    FOnKillFocus: TJvFocusChangeEvent;
    FOnSetFocus: TJvFocusChangeEvent;
    FLeaving: Boolean;
    FEntering: Boolean;
    procedure DoBoldDays;
    procedure SetColors(Value: TJvMonthCalColors);
    procedure SetBoldDays(Value: TStrings);
    procedure SetMultiSelect(Value: Boolean);
    procedure SetShowToday(Value: Boolean);
    procedure SetCircleToday(Value: Boolean);
    procedure SetWeekNumbers(Value: Boolean);
    procedure SetFirstDayOfWeek(Value: TJvMonthCalWeekDay);
    procedure SetMaxSelCount(Value: Word);
    procedure SetMinDate(Value: TDateTime);
    procedure SetMaxDate(Value: TDateTime);
    procedure SetFirstSelDate(Value: TDateTime);
    function GetFirstSelDate: TDateTime;
    function GetLastSelDate: TDateTime;
    procedure SetLastSelDate(Value: TDateTime);
    procedure SetSelectedDays(dFrom, dTo: TDateTime);
    procedure SetMonthDelta(Value: Integer);
    procedure SetToday(Value: TDateTime);
    procedure SetBorderStyle(Value: TBorderStyle);
    function GetTodayWidth: Integer;
    function GetMinSize: TRect;
    function IsBold(Year, Month, Day: Word): Boolean;
    procedure SetBold(Year, Month, Day: Word; Value: Boolean);

    function GetBoldDays: TStrings;
    function GetCircleToday: Boolean;
    function GetColors: TJvMonthCalColors;
    function GetFirstDayOfWeek: TJvMonthCalWeekDay;
    function GetShowToday: Boolean;
    function GetWeekNumbers: Boolean;

    function GetDays(Year, Month: Word): string;
    procedure SetDays(Year, Month: Word; Value: string);
    procedure CNNotify(var Msg: TWMNotify); message CN_NOTIFY;
    procedure WMLButtonDown(var Msg: TWMLButtonDown); message WM_LBUTTONDOWN;
  protected
    procedure GetDlgCode(var Code: TDlgCodes); override;
    procedure ColorChanged; override;
    procedure FontChanged; override;
    procedure ConstrainedResize(var MinWidth: Integer;
      var MinHeight: Integer; var MaxWidth: Integer;
      var MaxHeight: Integer); override;
    function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
    procedure CheckDayState(Year, Month: Word; var DayState: TMonthDayState); virtual;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure CreateWnd; override;
    procedure Change; virtual;
    procedure DoDateSelect(StartDate, EndDate: TDateTime); virtual;
    procedure DoDateSelChange(StartDate, EndDate: TDateTime); virtual;
    procedure DoGetDayState(var DayState: TNMDayState; var StateArray: TMonthDayStateArray); virtual;
    procedure FocusKilled(NextWnd: HWND); override;
    procedure FocusSet(PrevWnd: HWND); override;

    procedure DoFocusSet(const APreviousControl: TWinControl); virtual;
    procedure DoFocusKilled(const ANextControl: TWinControl); virtual;

    property MinSize: TRect read GetMinSize;
    property Bold[Year, Month, Day: Word]: Boolean read IsBold write SetBold;

    property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsNone;
    property BoldDays: TStrings read GetBoldDays write SetBoldDays;
    property CircleToday: Boolean read GetCircleToday write SetCircleToday default True;
    property Colors: TJvMonthCalColors read GetColors write SetColors;
    property DateFirst: TDateTime read GetFirstSelDate write SetFirstSelDate;
    property DateLast: TDateTime read GetLastSelDate write SetLastSelDate;
    property DateMax: TDateTime read FMaxDate write SetMaxDate;
    property DateMin: TDateTime read FMinDate write SetMinDate;
    property Days[Year, Month: Word]: string read GetDays write SetDays;
    property FirstDayOfWeek: TJvMonthCalWeekDay read GetFirstDayOfWeek write SetFirstDayOfWeek default mcLocale;
    property MaxSelCount: Word read FMaxSelCount write SetMaxSelCount default 7;
    property MonthDelta: Integer read FMonthDelta write SetMonthDelta default 1;
    property MultiSelect: Boolean read FMultiSelect write SetMultiSelect default False;
    property ShowToday: Boolean read GetShowToday write SetShowToday default True;
    property TodayWidth: Integer read GetTodayWidth;
    property WeekNumbers: Boolean read GetWeekNumbers write SetWeekNumbers default False;
    property Today: TDateTime read FToday write SetToday;
    property OnSelect: TJvMonthCalSelEvent read FOnSelect write FOnSelect;
    property OnSelChange: TJvMonthCalSelEvent read FOnSelChange write FOnSelChange;
    property OnGetDayState: TJvMonthCalStateEvent read FOnGetState write FOnGetState;
    property OnSetFocus: TJvFocusChangeEvent read FOnSetFocus write FOnSetFocus;
    property OnKillFocus: TJvFocusChangeEvent read FOnKillFocus write FOnKillFocus;
  public
    constructor Create(AOwner: TComponent); override;
    constructor CreateWithAppearance(AOwner: TComponent; const AAppearance: TJvMonthCalAppearance; const
      AOwnsAppearance: Boolean = False);
    destructor Destroy; override;
    function FirstVisibleDate(Partial: Boolean): TDateTime;
    function LastVisibleDate(Partial: Boolean): TDateTime;
    function VisibleMonths: Integer;
    procedure SetDayStates(MonthCount: Integer; DayStates: array of TMonthDayState);

    property Entering: Boolean read FEntering;
    property Leaving: Boolean read FLeaving;
  end;

  TJvMonthCalendar2 = class(TJvCustomMonthCalendar)
  public
    property MinSize;
    property Bold;
    property Days;
  published
    { inherited properties }
    property Action;
    property Align;
    property Anchors;
    property Constraints;
    property Height default 160;
    property Width default 190;
    property Enabled;
    property Font;
    property ParentColor;
    property ParentFont;
    property ParentShowHint;
    property TabStop;
    property TabOrder;
    property Visible;
    property OnClick;
    property OnDblClick;
    property OnEnter;
    property OnExit;
    property OnMouseDown;
    property OnMouseUp;
    property OnMouseMove;
    property OnKeyDown;
    property OnKeyUp;
    property OnKeyPress;
    property OnStartDrag;
    property OnDragOver;
    property OnDragDrop;
    property OnEndDrag;
    { new properties }
    property AutoSize;
    property BoldDays;
    property BorderStyle;
    property CircleToday;
    property Colors;
    property DateMin;
    property DateMax;
    property DateFirst;
    property DateLast;
    property FirstDayOfWeek;
    property MaxSelCount;
    property MonthDelta;
    property MultiSelect;
    property ShowToday;
    property WeekNumbers;
    property Today;
    property OnKillFocus;
    property OnSelect;
    property OnSetFocus;
    property OnSelChange;
    property OnGetDayState;
  end;

function StringToDayStates(const S: string): TMonthDayState;
function DayStatesToString(Days: TMonthDayState): string;
// function GetDLLVersion(const DLLName: string; var pdwMajor, pdwMinor: Integer): Boolean;

{$IFDEF UNITVERSIONING}
const
  UnitVersioning: TUnitVersionInfo = (
    RCSfile: '$RCSfile: JvCalendar.pas,v $';
    Revision: '$Revision: 1.31 $';
    Date: '$Date: 2005/02/18 14:17:23 $';
    LogPath: 'JVCL\run'
  );
{$ENDIF UNITVERSIONING}

implementation

uses
  SysUtils, ComCtrls,
  JvResources;

const
  MCM_GETMAXTODAYWIDTH = (MCM_FIRST + 21);
  MCS_NOTODAYCIRCLE = $0008;
  MCS_NOTODAY = $0010;
  ColorIndex: array [0..5] of Integer = (MCSC_BACKGROUND, MCSC_TEXT,
    MCSC_TITLEBK, MCSC_TITLETEXT, MCSC_MONTHBK, MCSC_TRAILINGTEXT);

  // IE3 and previous:
  //  MCS_NOTODAY     =    $0008;

function InitCommonControl(CC: Integer): Boolean;
var
  ICC: TInitCommonControlsEx;
begin
  ICC.dwSize := SizeOf(TInitCommonControlsEx);
  ICC.dwICC := CC;
  Result := InitCommonControlsEx(ICC);
  if not Result then
    InitCommonControls;
end;

function IsBlankDate(ST: TSystemTime): Boolean;
begin
  with ST do
    Result := (wMonth = 0) and (wDay = 0);
end;

function StringToDayStates(const S: string): TMonthDayState;
var
  P, L, I, R: Integer;
begin
  Result := 0;
  P := 1;
  L := Length(S);
  if L = 0 then
    Exit;
  while True do
  begin
    while (P <= L) and (S[P] = ',') do
      Inc(P);
    if P > L then
      Break;
    I := P;
    while (P <= L) and (S[P] <> ',') do
      Inc(P);
    R := StrToIntDef(Copy(S, I, P - I), 0);
    if R in [1..31] then
      Result := Result or (1 shl (R - 1));
  end;
end;

type
  // (p3) from ShLwAPI
  TDLLVersionInfo = packed record
    cbSize: DWORD;
    dwMajorVersion: DWORD;
    dwMinorVersion: DWORD;
    dwBuildNumber: DWORD;
    dwPlatformID: DWORD;
  end;

{
function GetDLLVersion(const DLLName: string; var pdwMajor, pdwMinor: Integer): Boolean;
var
  hDLL, hr: THandle;
  pDllGetVersion: function(var dvi: TDLLVersionInfo): Integer; stdcall;
  dvi: TDLLVersionInfo;
begin
  hDLL := LoadLibrary(PChar(DLLName));
  if (hDLL < 32) then
    hDLL := 0;
  if (hDLL <> 0) then
  begin
    Result := True;
    (*  You must get this function explicitly
        because earlier versions of the DLL
        don't implement this function.
        That makes the lack of implementation
        of the function a version marker in itself.   *)
    @pDllGetVersion := GetProcAddress(hDLL, PChar('DllGetVersion'));
    if Assigned(pDllGetVersion) then
    begin
      FillChar(dvi, SizeOf(dvi), #0);
      dvi.cbSize := SizeOf(dvi);
      hr := pDllGetVersion(dvi);
      if (hr = 0) then
      begin
        pdwMajor := dvi.dwMajorVersion;
        pdwMinor := dvi.dwMinorVersion;
      end;
    end
    else (*   If GetProcAddress failed, the DLL is a version previous to the one  shipped with IE 3.x. *)
    begin
      pdwMajor := 4;
      pdwMinor := 0;
    end;
    FreeLibrary(hDLL);
    Exit;
  end;
  Result := False;
end;
}

function DayStatesToString(Days: TMonthDayState): string;
var
  I: Integer;
begin
  Result := '';
  if Days = 0 then
    Exit;
  for I := 0 to 30 do
    if (Days and (1 shl (I))) <> 0 then
      Result := Result + Format('%d,', [I + 1]);
  if AnsiLastChar(Result) = ',' then
    SetLength(Result, Length(Result) - 1);
end;

//=== { TJvMonthCalColors } ==================================================

constructor TJvMonthCalColors.Create(AOwner: TJvCustomMonthCalendar);
begin
  inherited Create;
  Calendar := AOwner;
  FBackColor := clWindow;
  FTextColor := clWindowText;
  FTitleBackColor := clActiveCaption;
  FTitleTextColor := clWhite;
  FMonthBackColor := clWhite;
  FTrailingTextColor := clInactiveCaptionText;
end;

procedure TJvMonthCalColors.Assign(Source: TPersistent);
var
  SourceName: string;
begin
  if Source = nil then
    SourceName := 'nil'
  else
    SourceName := Source.ClassName;
  if Source is TJvMonthCalColors then
  begin
    if Source <> Self then
    begin
      FBackColor := TJvMonthCalColors(Source).BackColor;
      FTextColor := TJvMonthCalColors(Source).TextColor;
      FTitleBackColor := TJvMonthCalColors(Source).TitleBackColor;
      FTitleTextColor := TJvMonthCalColors(Source).TitleTextColor;
      FMonthBackColor := TJvMonthCalColors(Source).MonthBackColor;
      FTrailingTextColor := TJvMonthCalColors(Source).TrailingTextColor;
    end;
  end
  else
    inherited Assign(Source);
end;

procedure TJvMonthCalColors.SetColor(Index: Integer; Value: TColor);
begin
  if (Calendar <> nil) and Calendar.HandleAllocated then
    MonthCal_SetColor(Calendar.Handle, ColorIndex[Index], ColorToRGB(Value));
  case Index of
    0:
      begin
        FBackColor := Value;
        if Calendar <> nil then
          Calendar.Color := FBackColor;
      end;
    1:

⌨️ 快捷键说明

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