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

📄 inspctrl.pas

📁 类似Delphi Ide的对象查看器 可以在RUNTIME时使用
💻 PAS
📖 第 1 页 / 共 4 页
字号:
//      {$IFDEF GOITRIAL}
//      TrialMessage;
//      {$ELSE}
      FEdit.FEditing:=True;
      SetValue(FIndex,GetEnumName(TypeInfo(Boolean),Integer(Checked)));
//      {$ENDIF}
      Update;
    end;
end;

procedure TInspectorButton.SetDown(const Value: Boolean);
begin
  if Value<>FDown then
  begin
    FDown:=Value;
    Invalidate;
  end;
end;

procedure TInspectorButton.SetButtonType(const Value: TButtonType);
begin
  FButtonType:=Value;
  Visible:=(FButtonType<>btNone) and not (csDesigning in ComponentState);
  Invalidate;
end;

function TInspectorButton.Inspector: TCustomInspector;
begin
  if Assigned(Owner) then Result:=TCustomInspector(Owner)
  else Result:=nil;
end;

procedure TInspectorButton.WndProc(var Message: TMessage);
var
  R: TRect;
  Glyph: TBitmap;
begin
  with Message do
    case Msg of
      WM_LBUTTONDOWN:
        if not (csDesigning in ComponentState) then
          with Inspector do
          begin
            if not FPopup.Showing then
            begin
              Down:=True;
              SetCapture(Self.Handle);
            end
            else
            begin
              HidePopup;
              if IsFocused and CanFocus then
              begin
                SetFocus;
                FocusControl;
              end;
            end;
            if not IsFocused and CanFocus then
            begin
              SetFocus;
              FocusControl;
            end;
            if FButtonType=btUpDown then
            begin
              Self.Cursor:=crUpDown;
              Screen.Cursor:=crUpDown;
            end;
          end
        else inherited;
      WM_MOUSEMOVE:
        if GetCapture=Handle then
        begin
          Down:=(FButtonType=btUpDown) or PtInRect(ClientRect,Point(LoWord(lParam),HiWord(lParam)));
          if FButtonType=btUpDown then
          begin
            GetCursorPos(R.TopLeft);
            R.BottomRight:=ClientToScreen(Point(0,ClientHeight div 2));
            with Inspector do DragValue(ItemIndex,R.Bottom-R.Top);
          end;
        end;
      WM_LBUTTONUP:
        if GetCapture=Handle then
        begin
          ReleaseCapture;
          Cursor:=crDefault;
          Screen.Cursor:=crDefault;
          if Down then
          begin
            Down:=False;
            with Inspector do
              case FButtonType of
                btDropDown: if Action(iaButtonClick) then Update;
                btDialog: if Action(iaDoubleClick) then Update;
              else
                ApplyChanges;
                Update;
              end;
          end;
        end;
      WM_LBUTTONDBLCLK:
      begin
        Down:=False;
        ReleaseCapture;
        if FButtonType=btDropDown then Inspector.HidePopup;
        if FButtonType<>btUpDown then
          with Inspector do
            if IsFocused and CanFocus then
            begin
              SetFocus;
              FocusControl;
            end;
      end;
      WM_PAINT:
      begin
        inherited;
        R:=ClientRect;
        with R,Canvas do
        begin
          Dec(Right);
          Dec(Bottom);
          if FDown then
          begin
            Pen.Color:=clBtnShadow;
            MoveTo(Left,Bottom);
            LineTo(Left,Top);
            LineTo(Right,Top);
            LineTo(Right,Bottom);
            LineTo(Left,Bottom);
          end
          else
          begin
            Pen.Color:=cl3DLight;
            MoveTo(Left,Bottom);
            LineTo(Left,Top);
            LineTo(Right,Top);
            Pen.Color:=clBlack;
            LineTo(Right,Bottom);
            LineTo(Pred(Left),Bottom);
            InflateRect(R,-1,-1);
            Pen.Color:=clBtnHighlight;
            MoveTo(Left,Bottom);
            LineTo(Left,Top);
            LineTo(Right,Top);
            Pen.Color:=clBtnShadow;
            LineTo(Right,Bottom);
            LineTo(Pred(Left),Bottom);
          end;
          case FButtonType of
            btDropDown: Glyph:=FDropDown;
            btUpDown: Glyph:=FUpDown;
            btDialog: Glyph:=FDialog;
          else Glyph:=nil;
          end;
          if Assigned(Glyph) then
          begin
            Left:=(Right+Left) div 2-(Glyph.Width) div 2;
            Top:=(Bottom+Top) div 2-(Glyph.Height) div 2;
            CopyMode:=cmSrcAnd;
            R:=Rect(Left,Top,Left+Glyph.Width,Top+Glyph.Height);
            if FDown then OffsetRect(R,1,1);
            CopyRect(R,Glyph.Canvas,Rect(0,0,Glyph.Width,Glyph.Height));
          end;
        end;
      end;
    else inherited;
    end;
end;

constructor TInspectorButton.Create(AOwner: TComponent);
begin
  inherited;
  Color:=clBtnFace;
  FDropDown:=TBitmap.Create;
  FDropDown.Handle:=LoadBitmap(HInstance,'DROPDOWN');
  FUpDown:=TBitmap.Create;
  FUpDown.Handle:=LoadBitmap(HInstance,'UPDOWN');
  FDialog:=TBitmap.Create;
  FDialog.Handle:=LoadBitmap(HInstance,'DIALOG');
  ControlStyle:=ControlStyle-[csDoubleClicks,csOpaque];
end;

destructor TInspectorButton.Destroy;
begin
  FDropDown.Free;
  FUpDown.Free;
  FDialog.Free;
  inherited Destroy;
end;

procedure TCustomInspector.UpdateControls;
var
  R: TRect;
  WasFocused: Boolean;
begin
  if not (csDesigning in ComponentState) and (ItemCount>0) then
  begin
    WasFocused:=IsFocused;
    R:=ItemRect(FIndex);
    Dec(R.Left);
    Dec(R.Bottom);
    with R do Left:=Right-(Bottom-Top);
    with FButton do
    begin
      BoundsRect:=R;
      ButtonType:=Inspector.GetButtonType(FIndex);
      Visible:=Visible and not (csDesigning in ComponentState) and (ItemCount>0);
    end;
    R:=ItemRect(FIndex);
    InflateRect(R,-2,-2);
    R.Left:=FSplitter+3;
    Dec(R.Top);
    Dec(R.Bottom);
    if FButton.Visible then Dec(R.Right,FButton.Width);
    OffsetRect(R,0,1);
    Dec(R.Bottom);
    with FEdit do
    begin
      ParentFont:=True;
      if not (csLoading in Self.ComponentState) then GetValueFont(FIndex,Font);
      InflateRect(R,1,1);
      BoundsRect:=R;
      Text:=GetValue(FIndex);
      ReadOnly:=GetReadOnly(FIndex);
      MaxLength:=GetMaxLength(FIndex);
      EditMask:=GetEditMask(FIndex);
      Visible:=(GetInplaceEditorType(FIndex)=ieEdit) and not (csDesigning in ComponentState) and (ItemCount>0);
      SelectAll;
      if WasFocused and CanFocus then SetFocus;
      Invalidate;
    end;
    with FCheckBox do
    begin
      BoundsRect:=R;
      if GetInplaceEditorType(FIndex)=ieCheckBox then
        Checked:=StringToBoolean(GetValue(FIndex));
      Enabled:=not GetReadOnly(FIndex);
      Visible:=(GetInplaceEditorType(FIndex)=ieCheckBox) and CheckBoxes and
        not (csDesigning in ComponentState) and (ItemCount>0);
      if WasFocused and CanFocus then SetFocus;
      Invalidate;
    end;
    if WasFocused and not IsFocused and CanFocus then SetFocus;
  end
  else
  begin
    FEdit.Hide;
    FButton.Hide;
    FCheckBox.Hide;
  end;
end;

procedure TCustomInspector.SetIndex(const Value: Integer);
var
  R: TRect;
begin
  if Value<>FIndex then
  begin
    if GetAutoApply(FIndex) then ApplyChanges;
    R:=ItemRect(FIndex);
    Dec(R.Top,ItemHeight+2);
    Inc(R.Bottom,ItemHeight);
    FIndex:=Value;
    ItemIndex:=FIndex;
    UpdateControls;
    Dec(R.Top,ItemHeight+2);
    Inc(R.Bottom,ItemHeight);
    InvalidateRect(Handle,@R,True);
  end;
end;

procedure TCustomInspector.FocusControl;
begin
  if Showing and CanFocus and not FPopup.Visible then
  begin
    case GetInplaceEditorType(FIndex) of
      ieEdit:
        with FEdit do
          if CanFocus and Enabled then
          begin
            SelectAll;
            SetFocus;
            Invalidate;
          end;
      ieCheckBox:
        with FCheckBox do
          if CanFocus and Enabled then
          begin
            SetFocus;
            Invalidate;
          end;
    end;
    with FButton do if Visible then Invalidate;
  end;
end;

//{$IFDEF GOITRIAL}
//procedure TCustomInspector.TrialMessage;
//begin
//  PostMessage(Self.Handle,WM_TRIALMESSAGE,0,0);
//end;
//{$ENDIF}

function TCustomInspector.GetLocked: Boolean;
begin
  Result:=FLockCounter>0;
end;

function TCustomInspector.GetItemCount: Integer;
begin
  Result:=Items.Count;
end;

procedure TCustomInspector.SetItemCount(const Value: Integer);
var
  i: Integer;
  Focused: HWND;
begin
  if HandleAllocated and (ItemCount<>Value) then
  begin
    with Items do
    begin
      BeginUpdate;
      try
        Clear;
        for i:=0 to Pred(Value) do AddObject(GetName(i),GetData(i));
      finally
        EndUpdate;
      end;
    end;
    Focused:=GetFocus;
    if Items.Count>0 then ItemIndex:=GetDefaultIndex;
    Windows.SetFocus(Focused);
  end;
  FItemCount:=Value;
end;

procedure TCustomInspector.SetSplitter(const Value: Integer);
var
  FS: Integer;
begin
  FS:=Value;
  if FS<32 then FS:=32;
  if FS>ClientWidth-48 then FS:=ClientWidth-48;
  if FSplitter<>FS then
  begin
    FSplitter:=FS;
    if Value<>FSplitter then FSplitter:=Value;
    UpdateControls;
    Invalidate;
  end;
end;

procedure TCustomInspector.SetCheckBoxes(const Value: Boolean);
begin
  if FCheckBoxes<>Value then
  begin
    FCheckBoxes:=Value;
    Update;
    Invalidate;
  end;
end;

procedure TCustomInspector.SetPaintStyle(const Value: TPaintStyle);
begin
  if FPaintStyle<>Value then
  begin
    FPaintStyle:=Value;
    Invalidate;
  end;
end;

procedure TCustomInspector.DrawDragSplitter;
var
  OldPen: TPen;
  R: TRect;
begin
  with TCanvas.Create do
  try
    Handle:=GetWindowDC(0);
    OldPen:=TPen.Create;
    try
      OldPen.Assign(Pen);
      R:=ClientRect;
      MapWindowPoints(Self.Handle,HWND_DESKTOP,R,2);
      with Pen,R do
      begin
        Color:=clSilver;
        Mode:=pmXor;
        MoveTo(FDragSplitter+ClientOrigin.X,Top);
        LineTo(FDragSplitter+ClientOrigin.X,Bottom);
        Assign(OldPen);
      end;
    finally
      OldPen.Free;
    end;
  finally
    ReleaseDC(0,Handle);
    Free;
  end;
end;

procedure TCustomInspector.SetMouseItem(Pos: LParam);
var
  I: Integer;
begin
  with Items do
    if Count>0 then
    begin
      I:=ItemAtPos(Point(0,HiWord(Pos)),False);
      if I<0 then
        if SmallInt(HiWord(Pos))>ItemHeight*(ClientHeight div ItemHeight) then I:=Succ(ItemIndex)
        else I:=Pred(ItemIndex);
      if I<0 then I:=0;
      if I>Pred(Count) then I:=Pred(Count);
      ItemIndex:=I;
    end;
end;

function TCustomInspector.Action(A: TItemAction): Boolean;
var
  W: Integer;
  P: TPoint;
  EnableDefault: Boolean;
begin
  Result:=False;
  if (ItemIndex>-1) and (ItemIndex<Items.Count) and Assigned(GetParentForm(Self)) then
  begin
    case A of
      iaDoubleClick:
      begin
        EnableDefault:=True;
        if Assigned(FOnValueDoubleClick) then FOnValueDoubleClick(Self,ItemIndex,EnableDefault);
        if EnableDefault then
        begin
          if GetEnableExternalEditor(ItemIndex) then Result:=CallEditor(ItemIndex)
          else
            if not GetReadOnly(ItemIndex) then
            begin
              Result:=True;
//              {$IFDEF GOITRIAL}
//              TrialMessage;
//              {$ELSE}
              SetValue(ItemIndex,GetNextValue(ItemIndex));
//              {$ENDIF}
            end;
          FocusControl;
        end;
        if Result then Update;
      end;
      iaButtonClick:
      begin
        with FPopup do
        begin
          Visible:=False;
          Parent:=Self;
          Canvas.Font.Assign(Font);
          Items.Clear;
          Sorted:=GetSortValuesList(Self.ItemIndex);
          GetValuesList(Self.ItemIndex,Items);
          W:=Items.Count;
          if W>8 then W:=8;
          if W<1 then W:=1;
          Height:=ItemHeight*W+2;
          Width:=0;
          W:=GetPopupItemWidth(FPopup,Self.ItemIndex)+6;
          if Height div ItemHeight < Items.Count then
            Inc(W,GetSystemMetrics(SM_CXVSCROLL));
          if W<Self.ClientWidth-FSplitter then W:=Self.ClientWidth-FSplitter;
          ClientWidth:=W;
          P.X:=FSplitter;
          if Width+FSplitter>Self.ClientWidth then P.X:=Self.ClientWidth-Width;
          with Self do
          begin
            P.Y:=ItemRect(ItemIndex).Bottom-2;
            P:=ClientToScreen(P);
            if P.Y+FPopup.Height>Screen.Height then
              Dec(P.Y,ItemHeight+FPopup.Height-2);
          end;
          Left:=P.X;
          Top:=P.Y;
          ItemIndex:=Items.IndexOf(GetSelectedValue(Self.ItemIndex));
          Windows.SetParent(Handle,GetDesktopWindow);
          SetWindowLong(Handle,GWL_HWNDPARENT,GetDesktopWindow);
          SetWindowPos(Handle,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE or SWP_NOSIZE);
          Visible:=True;
          SendMessage(GetFocus,WM_KILLFOCUS,0,0);
          SendMessage(Handle,WM_SETFOCUS,GetFocus,0);
          SetFocus;
        end;
      end;
    end;
  end;
end;

procedure TCustomInspector.HidePopup;

⌨️ 快捷键说明

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