首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 調用 WinAPI:控制輸入光標閃爍速度(二)

     編輯:Delphi

SetCaretBlinkTime 實戰:動態調整與系統還原

本範例專注於 SetCaretBlinkTime 與 GetCaretBlinkTime 的應用。透過 RadioGroup 讓使用者選擇三種閃爍間隔 (1000、500、200 毫秒),即時改變光標閃爍速度。特別強調:此 API 為全系統生效,因此在 FormDestroy 中務必還原預設值 (530 ms),避免影響其他程式。

程式碼重點

  • 滑鼠點擊建立標準光標 (寬 0、高 20)。
  • 選項切換時呼叫 SetCaretBlinkTime(ms) 即時生效。
  • 表單關閉時自動還原系統預設值,展現良好的程式公民素養。

完整程式碼

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    RadioGroup1: TRadioGroup;
    procedure FormCreate(Sender: TObject);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure RadioGroup1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  RadioGroup1.Caption := '光標閃爍速度(毫秒)';
  RadioGroup1.Items.CommaText := '1000,500,200';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SetCaretBlinkTime(530); // 還原系統預設值
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  DestroyCaret;
  CreateCaret(Handle, 0, 0, 20);
  SetCaretPos(X, Y);
  ShowCaret(Handle);
end;

procedure TForm1.RadioGroup1Click(Sender: TObject);
var
  ms: Cardinal;
begin
  ms := StrToIntDef(RadioGroup1.Items[RadioGroup1.ItemIndex], 530);
  SetCaretBlinkTime(ms);
end;

end.

表單定義 (DFM)

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 137
  ClientWidth = 298
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  OnMouseUp = FormMouseUp
  PixelsPerInch = 96
  TextHeight = 13
  object RadioGroup1: TRadioGroup
    Left = 160
    Top = 8
    Width = 130
    Height = 113
    Caption = 'RadioGroup1'
    TabOrder = 0
    OnClick = RadioGroup1Click
  end
end

執行效果圖

簡單幾行代碼即可掌握全系統光標閃爍節奏,開發無障礙介面時可適度調整以提升可見度。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved