2017年9月7日 星期四

游標移到物件上顯示屬性資料

procedure TfrmNormalForm.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
var pd:TPoint;
  WinCon : TWinControl;
  WND : HWND;
  cmpComponent:TComponent;
  sName, sDataSource, sDataField:String;
  objDataSource, objDataField:TObject;
  PropInfo: PPropInfo;
  procedure pr_AddColumn(AListView:TListView; ACaption:String);
  var lvColumn:TListColumn;
    iWidth:Integer;
  begin
    lvColumn := AListView.Columns.Add;
    lvColumn.Alignment := taCenter;
    lvColumn.Caption := ACaption;
    lvColumn.Width := Self.Canvas.TextWidth(ACaption);
  end;
  procedure pr_AddItem(AListView:TListView; ACaption:String; ASubCaption:String);
  var lvItem:TListItem;
    iWidth, iHeight:Integer;
  begin
    lvItem := AListView.Items.Add;
    lvItem.Caption := ACaption;
    lvItem.SubItems.Add(ASubCaption);
    iWidth := Self.Canvas.TextWidth(ACaption+'XXX');
    AListView.Column[0].Width := Max(AListView.Column[0].Width, iWidth);
    iWidth := Self.Canvas.TextWidth(ASubCaption+'XXX');
    AListView.Column[1].Width := Max(AListView.Column[1].Width, iWidth);
    AListView.Width := AListView.Column[0].Width + AListView.Column[1].Width+4;
    iHeight := Self.Canvas.TextHeight('A');
    AListView.Height := (AListView.Items.Count + 2)*iHeight;
  end;
begin
  if not bDesignMode then
    Exit;
  //
  if Msg.message=WM_MOUSEMOVE then
  begin
    GetCursorPos(pd);
    WND := Self.Handle;
    repeat
      WinCon := FindControl(WND);
      WND := ChildWindowFromPoint(WinCon.Handle,WinCon.ScreenToClient(pd));
      if (WND = 0) or (not WinCon.Showing) or (not WinCon.CanFocus) then
        Exit;
    until (WND = WinCon.Handle) or (WinCon.ControlCount <= 0);
    cmpComponent := FindComponent(wincon.Name);
    if Assigned(cmpComponent) then
    begin
      PropInfo := GetPropInfo(cmpComponent.ClassInfo, 'DataSource');
      if Assigned(PropInfo) then
      begin
        //物件名稱
        sName := cmpComponent.Name;
        //物件DataSource屬性
        objDataSource := GetObjectProp(cmpComponent, 'DataSource');
        if objDataSource is TDataSource then
          sDataSource := TDataSource(objDataSource).Name;
        //物件DataField屬性
        PropInfo := GetPropInfo(cmpComponent.ClassInfo, 'DataField');
        sDataField := '*';
        if Assigned(PropInfo) then
          sDataField := GetStrProp(cmpComponent, 'DataField');
        //
        if not Assigned(F_HintList) then
        begin
          F_HintList := TListView.Create(Self);
          F_HintList.Parent := Self;
          F_HintList.Readonly := True;
          F_HintList.ViewStyle := vsReport;
          F_HintList.GridLines := True;
          F_HintList.Visible := False;
          pr_AddColumn(F_HintList, 'Property');
          pr_AddColumn(F_HintList, 'Value');
        end;
        pd := ScreenToClient(pd);
        F_HintList.BringToFront;
        F_HintList.Left := Min(pd.X+2, Self.Width-F_HintList.Width-20);
        F_HintList.Top := Min(pd.Y+2, Self.Height-F_HintList.Height-80);
        if not F_HintList.Showing then
        begin
          F_HintList.Items.Clear;
          pr_AddItem(F_HintList, 'Name', sName);
          pr_AddItem(F_HintList, 'DataSource', sDataSource);
          if sDataField<>'*' then
            pr_AddItem(F_HintList, 'DataField', sDataField);
          F_HintList.Show;
        end;
      end
      else
      begin
        if Assigned(F_HintList) then
        begin
          if F_HintList.Showing then
            F_HintList.Hide;
        end;
      end;
    end;
  end;
end;

沒有留言:

張貼留言