← 都来码 / Dolaima← 例程索引pascal/app/calendar.pas▶ 在浏览器里试玩 原文

pascal/app/calendar.pas · 4671 字节 · 下载原文

▶ 在浏览器里试玩

(* 日历 —— 用 **Pascal** 写的手机小应用(日期 / 月历)
   Calendar -- a small phone app written in **Pascal** (dates / month view)

   功能:一屏一个月历(2026 年,每月天数与 1 号星期几用常量表);← →
   切月;点左右半屏也能切;ESC 退出。

   本前端的写法要点见 app/stopwatch.pas 文件头。

   跑法 —— 手机:vml run examples/pascal/calendar.pas
   跑法 —— 桌面:vmlcli Examples/pascal/calendar.pas --screen 480x640 --frames /tmp/fr
*)

program Calendar;

uses vml_ui;

var
  w, h, cell, ox, oy, month, year, headH, footH, fontDay: integer;
  i, row, col, px, py, days, startd, cellw: integer;
  msg, ax, ay, key, tid, frames: integer;
  mdays: array[1..12] of integer;
  mstart: array[1..12] of integer;
  s: string;

begin
  mdays[1] := 31;  mstart[1] := 3;
  mdays[2] := 28;  mstart[2] := 6;
  mdays[3] := 31;  mstart[3] := 6;
  mdays[4] := 30;  mstart[4] := 2;
  mdays[5] := 31;  mstart[5] := 4;
  mdays[6] := 30;  mstart[6] := 0;
  mdays[7] := 31;  mstart[7] := 2;
  mdays[8] := 31;  mstart[8] := 5;
  mdays[9] := 30;  mstart[9] := 1;
  mdays[10] := 31; mstart[10] := 3;
  mdays[11] := 30; mstart[11] := 6;
  mdays[12] := 31; mstart[12] := 1;

  ui_win_open('日历', 0, 0);
  ui_scr_set_keep_on(1);
  w := ui_scr_get_w();
  h := ui_scr_get_h();
  if w <= 0 then
    w := 360;
  if h <= 0 then
    h := 620;

  month := 11;
  year := 2026;
  { 版面全部由 scr_w / scr_h 现算:标题区 + 星期行 + 6 行网格 + 底部提示 }
  headH := h div 6;
  if headH < 84 then
    headH := 84;
  if headH > 120 then
    headH := 120;
  footH := 30;
  cell := (w - 24) div 7;
  if (h - headH - footH) div 6 < cell then
    cell := (h - headH - footH) div 6;
  if cell < 10 then
    cell := 10;
  if cell > 140 then
    cell := 140;
  cellw := cell * 7;
  ox := (w - cellw) div 2;
  oy := headH;
  fontDay := cell div 3;
  if fontDay < 10 then
    fontDay := 10;
  if fontDay > 22 then
    fontDay := 22;
  frames := 0;
  tid := ui_timer_set(200, 0);

  while frames < 20000 do
  begin
    days := mdays[month];
    startd := mstart[month];
    ui_2d_clear($FF101018);

    Str(year, s);
    ui_2d_text(ox, 46, s, $FFFFFFFF, 26, 0);
    ui_2d_text(ox + 66, 46, '年', $FF8A9AA8, 14, 0);
    Str(month, s);
    ui_2d_text(ox + 110, 46, s, $FF66BB6A, 36, 0);
    ui_2d_text(ox + 160, 46, '月', $FF8A9AA8, 14, 0);

    ui_2d_text(ox + cell div 2, headH - 10, '一', $FF8A9AA8, 12, 1);
    ui_2d_text(ox + cell + cell div 2, headH - 10, '二', $FF8A9AA8, 12, 1);
    ui_2d_text(ox + cell * 2 + cell div 2, headH - 10, '三', $FF8A9AA8, 12, 1);
    ui_2d_text(ox + cell * 3 + cell div 2, headH - 10, '四', $FF8A9AA8, 12, 1);
    ui_2d_text(ox + cell * 4 + cell div 2, headH - 10, '五', $FF8A9AA8, 12, 1);
    ui_2d_text(ox + cell * 5 + cell div 2, headH - 10, '六', $FF8A9AA8, 12, 1);
    ui_2d_text(ox + cell * 6 + cell div 2, headH - 10, '日', $FF8A9AA8, 12, 1);

    i := 0;
    while i < days do
    begin
      row := (i + startd) div 7;
      col := (i + startd) - row * 7;
      px := ox + col * cell;
      py := oy + row * cell;
      if (month = 11) and (i + 1 = 14) then
        ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, $FF66BB6A, 1, 0, 4)
      else
        ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, $FF24242C, 1, 0, 4);
      Str(i + 1, s);
      if (month = 11) and (i + 1 = 14) then
        ui_2d_text(px + cell div 2, py + 3 + (cell - 6) div 2 + fontDay div 3,
                   s, $FF101018, fontDay, 1)
      else
        ui_2d_text(px + cell div 2, py + 3 + (cell - 6) div 2 + fontDay div 3,
                   s, $FFFFFFFF, fontDay, 1);
      i := i + 1;
    end;

    ui_2d_text(w div 2, h - 8, '← → 切月 · ESC 退出', $FF8A9AA8, 11, 1);
    ui_2d_present();

    msg := ui_msg_fetch_wait(0);
    if msg = 10 then
      frames := 20000;

    if msg = 1 then
    begin
      key := ui_msg_get_a();
      if key = 27 then
        frames := 20000;
      if key = 37 then
      begin
        month := month - 1;
        if month < 1 then
          month := 12;
      end;
      if key = 39 then
      begin
        month := month + 1;
        if month > 12 then
          month := 1;
      end;
    end;

    if msg = 6 then
    begin
      ax := ui_msg_get_a();
      if ax < w div 2 then
      begin
        month := month - 1;
        if month < 1 then
          month := 12;
      end
      else
      begin
        month := month + 1;
        if month > 12 then
          month := 1;
      end;
    end;

    if msg = 9 then
      frames := frames + 1;
  end;

  ui_timer_kill(tid);
  ui_scr_set_keep_on(0);
  ui_win_close();
end.