一个开关(灯)组件

博主因工作开发了开关灯程序并分享代码。代码中定义了开关灯类 TSwitch,包含开关状态、颜色、半径等属性,实现了创建、绘制、点击等方法,还对开关的选中状态、颜色、半径设置进行了处理,最后完成组件注册。

由于工作的关系,特开发了一个开关灯,现在拿出来给大家共享一下

unit SwitchLight;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TSwitch= class(TCustomControl)
  private
    PFState: TCheckBoxState;
    FOnColor,
    FOffColor  : TColor;
    FLightRadius : integer;
    FEnabled   : Boolean;
    function GetChecked: Boolean;
    procedure SetChecked(Value: Boolean);
    procedure SetOnColor(Value: TColor);
    procedure SetOffColor(Value: TColor);
    procedure SetLightRadius(Value : integer);
  protected
    procedure Paint; override;
    procedure Click; override;
  public
    constructor Create(aOwner: TComponent); override;
    procedure CreateParams(var Params: TCreateParams); override;
    property State: TCheckBoxState read PFState;                                   //保存开关状态
  published
    property Checked: Boolean read GetChecked write SetChecked;                    //开关选择
    property Enabled: Boolean read FEnabled write FEnabled default True;           //是否启用开关
    property OnColor: TColor read FOnColor write SetOnColor default clLime;        //开关灯开启颜色
    property OffColor: TColor read FOffColor write SetOffColor default clRed;      //开关灯关闭颜色
    property LightRadius : integer read FLightRadius write SetLightRadius;         //开关灯半径
    property Hint;
    property OnClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnEnter;
    property OnExit;
    property OnKeyPress;
    property OnKeyDown;
    property OnKeyUp;
  end;

procedure Register;

implementation

{$R *.DCR}

constructor TSwitch.Create;
begin
  inherited Create(aOwner);
  ControlStyle := [csCaptureMouse, csClickEvents, csDesignInteractive];
  FEnabled := True;
  FOnColor := clLime;
  FOffColor := clRed;
  FLightRadius := 5;
  Width := 20;
  Height := 20;
end;

procedure TSwitch.CreateParams(var Params: TCreateParams);
begin
  { call the create of the params }
  inherited CreateParams(Params);
  { and then add our twist, transparency }
  Params.ExStyle := Params.ExStyle + WS_EX_Transparent;
end;

procedure TSwitch.Paint;
var
   X,
   Y        : Integer;
   TheColor : TColor;
   Rect : TRect;
begin
  X := (Width div 2) - FLightRadius;
  Y := (Height div 2) - FLightRadius;

  if Checked then
    TheColor := FOnColor
  else
    TheColor := FOffColor;

  with Canvas do
  begin
    Rect := ClientRect;
    Brush.Color := Self.Color;
    Brush.Style := bsSolid;
    FillRect(Rect);
   
    //画外面的阴影圆
    Pen.Color := clBtnHighLight;
    Arc(X - 1,
        Y - 1,
        X + 2 * FLightRadius,
        Y + 2 * FLightRadius,
        X + FLightRadius div 2,
        Y + FLightRadius * 4 div 3,
        X + FLightRadius * 4 div 3,
        Y + FLightRadius div 2);

    //画外面的高亮圆
    Pen.Color := clBtnShadow;
    Arc(X,
        Y,
        X + 1 + 2 * FLightRadius,
        Y + 1 + 2 * FLightRadius,
        X + integer(FLightRadius * 4 div 3),
        Y + FLightRadius div 2,
        X + FLightRadius div 2,
        Y + integer(FLightRadius * 4 div 3));
    //画中间的圆
    Brush.Color := TheColor;
    Ellipse(X, Y, X - 2 + 2 * FLightRadius, Y - 1 + 2 * FLightRadius);
    Pixels[X + 2, Y - 1 + FLightRadius] := clBtnHighLight;
    Pixels[X + 2, Y - 2 + FLightRadius] := clBtnHighLight;
    Pixels[X + 3, Y - 1 + FLightRadius] := clBtnHighLight;
  end;
end;

function TSwitch.GetChecked;
begin
  Result := not(State = cbUnChecked);
end;

procedure TSwitch.SetChecked;
begin
  if Value then
    PFState := cbChecked
  else
    PFState := cbUnChecked;

  Paint;
end;

procedure TSwitch.Click;
begin
  if FEnabled then Checked := not Checked;
end;

procedure TSwitch.SetOnColor;
begin
  FOnColor := Value;
  if Checked then
    Paint;
end;

procedure TSwitch.SetOffColor;
begin
  FOffColor := Value;
  if not Checked then
    Paint;
end;

procedure TSwitch.SetLightRadius(Value : integer);
begin
  if FLightRadius <> Value then
  begin
    FLightRadius := Value;
    Paint;
  end;
end;

procedure Register;
begin
  RegisterComponents('Standard', [TSwitch]);
end;

end.

### 单开关电路设计 在 Multisim 中实现受两个开关控制的功能,可以通过逻辑门的设计来完成。这种设计通常基于基本的布尔代数原理以及与、或、非等逻辑运算。 #### 设计思路 为了使一盏能够被两个独立的开关所控制,可以采用异或(XOR)逻辑门或者组合多个与/或/非逻辑门的方式构建电路。具体来说: - 当任意一个开关闭合时,亮;当两个开关都闭合或都不闭合时,灭。 - 这种功能可以用 XOR 门直接实现,因为 XOR 的特性是输入不同时输出为高电平[^1]。 #### 组件需求 以下是所需的主要组件列表: - **LED 或其他形式的指示** - **SPST 开关 ×2** (用于模拟实际场景中的物理开关) - **7486 芯片或其他支持 XOR 功能的集成电路** #### 步骤说明 通过以下方法可以在 Multisim 上搭建该电路模型并验证其行为: ```plaintext +---SW1----+ | | GND ---- XOR Gate ---- Lamp (D1) | | +---SW2----+ ``` 上述图表示的是连接方式,在软件环境中需按照此布局放置元件,并调整参数使其正常工作。 对于初学者而言,建议先熟悉各个基础元器件的操作界面及其属性设置窗口的位置和作用。 #### 示例代码片段 如果需要进一步扩展到程序化仿真环境,则可利用 SPICE netlist 定义如下网络结构: ```spice * Single Light Double Switch Circuit Example V1 N001 0 DC 5 R1 N001 OUT 1K LAMP OUT 0 D1 XOR_IN1 IN1 XNOR_XOUT U1 XOR_IN2 IN2 XNOR_XOUT U1 SWITCH1 GND IN1 SW_INIT=OPEN RON=1 ROFF=1E9 VON=0 VOFF=5 SWITCH2 GND IN2 SW_INIT=CLOSED RON=1 ROFF=1E9 VON=0 VOFF=5 .model D1 D(Is=1e-12 Rs=.1 Cjo=2pFc=0.5m Bv=5 Ibv=10u Tt=1n) .subckt XNOR_XOUT A B Y XU1 A B INT1 XOR XU2 INT1 B Y XOR .ends .lib 'standard.dio' .inc 'xnor_xout.sub' ! Include subcircuit definition file. .tran 0.1ms 1s ; Run transient analysis from t=0 to t=1 second with step size of .1 milliseconds. .end ``` 以上是一个简单的 SPICE 文件例子,它定义了一个带双控开关的 LED 照明系统的电气特性和时间响应情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值