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

📄 editors.cpp

📁 delphi做的类似与属性编辑器一样的控件
💻 CPP
字号:
//---------------------------------------------------------------------------


#pragma hdrstop

#include "Editors.h"
#include<Windows.hpp>
#include<Messages.hpp>
#include<SysUtils.hpp>
#include<Classes.hpp>
#include<Graphics.hpp>
#include<Controls.hpp>
#include<Forms.hpp>
#include<Dialogs.hpp>
#include<StdCtrls.hpp>
#include<VirtualTrees.hpp>
#include<ImgList.hpp>
#include<ExtCtrls.hpp>
#include<ComCtrls.hpp>
#include<Mask.hpp>
//----------------- TPropertyEditLink ----------------------------------------------------------------------------------

// This implementation is used in VST3 to make a connection beween the tree
// and the actual edit window which might be a simple edit, a combobox
// or a memo etc.

using  namespace WZZ_Editors_namespace;
__fastcall TPropertyEditLink::TPropertyEditLink()
{
       FEdit=NULL;
}
__fastcall TPropertyEditLink::~TPropertyEditLink(void)
{
    delete FEdit;
    FEdit = NULL;
    //  Release() will delete myself if counter == 0 !!!!!
}
//----------------------------------------------------------------------------------------------------------------------
void __fastcall TPropertyEditLink::EditKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
    //  als Key hat man hier: VK_HOME VK_END VK_UP VK_DOWN VK_PRIOR VK_NEXT VK_ESCAPE VK_RETURN
   bool CanAdvance = true;
   TShiftState Empty;
   //bool Handled = false;
   TVirtualNode*  NewFocusedNode;       // The node being edited.

   switch (Key)
   {
   case VK_ESCAPE:
        if(CanAdvance)
        {
                FTree->CancelEditNode();
                Key = 0;
        }
        break;


   case VK_RETURN:
        if(CanAdvance)
        {
                FTree->EndEditNode();
                Key = 0;
        }
        break;


   case VK_UP:
   case VK_DOWN:
        // Consider special cases before finishing edit mode.
        Empty.Clear();
        CanAdvance = (Shift == Empty);
        if (FEdit->ClassNameIs("TComboBox"))
        {
                CanAdvance =  CanAdvance && (!((TComboBox*)FEdit)->DroppedDown);
        }
         else if (FEdit->ClassNameIs("TDateTimePicker"))
        {
                CanAdvance =  CanAdvance &&  (!((TDateTimePicker*)FEdit)->DroppedDown);
        }

        if(CanAdvance)
        {
                // Forward the keypress to the tree. It will asynchronously change the focused node.
                PostMessage(FTree->Handle, WM_KEYDOWN, Key, 0);
                Key = 0;
        }
   }
}
//----------------------------------------------------------------------------------------------------------------------
bool __stdcall TPropertyEditLink::BeginEdit(void)
{
    bool Result = true;

    FEdit->Show();
    FEdit->SetFocus();
    return(Result);
}

//----------------------------------------------------------------------------------------------------------------------

bool __stdcall TPropertyEditLink::CancelEdit(void)
{
    FEdit->Hide();
    return true;
}

//----------------------------------------------------------------------------------------------------------------------

bool __stdcall TPropertyEditLink::EndEdit(void)
{
  PPropertyData Data;
  bool Result;
  char Buffer[1024];
  WideString S;

  Result = true;

  Data = (TPropertyData *)FTree->GetNodeData(FNode);
  if (FEdit->ClassNameIs("TComboBox"))
  {
        S = ((TComboBox*)FEdit)->Text;
  }
  else
  {
        GetWindowText(FEdit->Handle, Buffer, 1024);
        S = Buffer;
  }

  if( S != Data->Value )
  {
        Data->Value = S;
        Data->Changed = true;
        FTree->InvalidateNode(FNode);
  }
  FEdit->Hide();
  FTree->SetFocus();
  return Result;
}

//----------------------------------------------------------------------------------------------------------------------

Types::TRect __stdcall TPropertyEditLink::GetBounds(void)
{
    TRect R;
    if (FEdit != NULL)
        R = FEdit->BoundsRect;
    else
        memset(&R, 0, sizeof(TRect));

    return(R);
}
//----------------------------------------------------------------------------------------------------------------------

bool __stdcall TPropertyEditLink::PrepareEdit(TBaseVirtualTree* Tree, PVirtualNode Node, TColumnIndex Column)
{

    PPropertyData Data;

    bool Result = True;

    FTree = (TVirtualStringTree*)Tree ;
    FNode = Node;
    FColumn = Column;

    // determine what edit type actually is needed
    delete FEdit;
    FEdit = NULL;
    Data =(TPropertyData*) FTree->GetNodeData(Node);
    switch( Data->ValueType)
    {
    case vtString:
        FEdit = new TEdit((TComponent*)NULL);
        if(FEdit)
        {
             ((TEdit*)FEdit)->Visible = False;
             ((TEdit*)FEdit)->Parent = Tree;
             ((TEdit*)FEdit)->Text = Data->Value;
             ((TEdit*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtPickString:
        FEdit = new TComboBox((TComponent*)NULL);
        if(FEdit)
        {
          ((TComboBox*)FEdit)->Visible = false;
          ((TComboBox*)FEdit)->Parent = Tree;
          ((TComboBox*)FEdit)->Text = Data->Value;
          ((TComboBox*)FEdit)->Items->Add(((TComboBox*)FEdit)->Text);
          ((TComboBox*)FEdit)->Items->Add("Standard");
          ((TComboBox*)FEdit)->Items->Add("Additional");
          ((TComboBox*)FEdit)->Items->Add("Win32");
          ((TComboBox*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtNumber:
        FEdit = new TMaskEdit((TComponent*)NULL);
        if(FEdit)
        {
          ((TMaskEdit*)FEdit)->Visible = false;
          ((TMaskEdit*)FEdit)->Parent = Tree;
          ((TMaskEdit*)FEdit)->EditMask = "9999";
          ((TMaskEdit*)FEdit)->Text = Data->Value;
          ((TMaskEdit*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtPickNumber:
        FEdit = new TComboBox((TComponent*)NULL);
        if(FEdit)
        {
          ((TComboBox*)FEdit)->Visible = False;
          ((TComboBox*)FEdit)->Parent = Tree;
          ((TComboBox*)FEdit)->Text = Data->Value;
          ((TComboBox*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtMemo:
        FEdit = new TComboBox((TComponent*)NULL);
        // In reality this should be a drop down memo but this requires
        // a special control.
        if(FEdit)
        {
          ((TComboBox*)FEdit)->Visible = False;
          ((TComboBox*)FEdit)->Parent = Tree;
          ((TComboBox*)FEdit)->Text = Data->Value;
          ((TComboBox*)FEdit)->Items->Add(Data->Value);
          ((TComboBox*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtDate:
        FEdit = new TDateTimePicker((TComponent*)NULL);
        if(FEdit)
        {
          ((TDateTimePicker*)FEdit)->Visible = False;
          ((TDateTimePicker*)FEdit)->Parent = Tree;
          ((TDateTimePicker*)FEdit)->CalColors->MonthBackColor = clWindow;
          ((TDateTimePicker*)FEdit)->CalColors->TextColor = clBlack;
          ((TDateTimePicker*)FEdit)->CalColors->TitleBackColor = clBtnShadow;
          ((TDateTimePicker*)FEdit)->CalColors->TitleTextColor = clBlack;
          ((TDateTimePicker*)FEdit)->CalColors->TrailingTextColor = clBtnFace;
          ((TDateTimePicker*)FEdit)->Date = StrToDate(Data->Value);
          ((TDateTimePicker*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    default:
        Result = false;
    }
    return Result;
}

//----------------------------------------------------------------------------------------------------------------------

void __stdcall TPropertyEditLink::ProcessMessage(Messages::TMessage &Message)
{
    FEdit->WindowProc(Message);
}
//----------------------------------------------------------------------------------------------------------------------

void __stdcall TPropertyEditLink::SetBounds(const Types::TRect R)
{
    int Dummy;

    // Since we don't want to activate grid extensions in the tree (this would influence how the selection is drawn)
    // we have to set the edit's width explicitly to the width of the column.
    FTree->Header->Columns->GetColumnBounds(FColumn, Dummy, R.Right);
    FEdit->BoundsRect = R;
}
//---------------------------------------------------------------------------

ULONG __stdcall TPropertyEditLink::AddRef() {                  //  Implement IUnknown interface
	return InterlockedIncrement((LPLONG)&FRefCount);        //  thread safe increment
}
//---------------------------------------------------------------------------

ULONG __stdcall TPropertyEditLink::Release() {                 //  Implement IUnknown interface
	InterlockedDecrement((LPLONG)&FRefCount);               //  thread safe decrement
	if (FRefCount == 0) {                                   //  if last user then
		delete this;                                        //    delete myself
		return 0;                                           //    => 0 !
	}
	return FRefCount;                                       //  return internal counter
}
//---------------------------------------------------------------------------

HResult  __stdcall TPropertyEditLink::QueryInterface(const GUID& IID, void**ppv) {
	if (IID == __uuidof(IUnknown))                          //  IUnknown as basic interface
   	*ppv = (IUnknown*)this;
	else if (IID == __uuidof(IVTEditLink))                  //  i am self
		*ppv = (IVTEditLink*)this;
	else {
		*ppv = NULL;                                        //  unknowm interface
		return E_NOINTERFACE;
	}
	((IUnknown*)(*ppv))->AddRef();                          //  increment if used
	return S_OK;                                            //  Ok !
}
//---------------- TGridEditLink ---------------------------------------------------------------------------------------
/*
bool __stdcall TGridEditLink::EndEdit(void)
{
      PGridData Data;
      char Buffer[1024];
      WideString S;
      int I;

      bool Result = true;

      Data = FTree->GetNodeData(FNode);
      if (FEdit->ClassNameIs("TComboBox"))
      {
        S = ((TComboBox*)FEdit)->Text;
        if(S != Data->Value[FColumn - 1])
        {
            Data->Value[FColumn - 1] = S;
            Data->Changed = true;
        }
      }
      else
      {
        if (FEdit->ClassNameIs("TMaskEdit"))
        {
          I = StrToInt(Trim(((TMaskEdit*)FEdit)->EditText));
          if(I !=Data.Value[FColumn - 1])
          {
            Data->Value[FColumn - 1] = I;
            Data->Changed = true;
          }
        }
        else
        {
          GetWindowText(FEdit->Handle, Buffer, 1024);
          S = Buffer;
          if(S != Data.Value[FColumn - 1])
          {
            Data->Value[FColumn - 1] = S;
            Data->Changed = True;
          }
        }
      }
      if(Data->Changed)
        FTree->InvalidateNode(FNode);
      FEdit->Hide();
}

//----------------------------------------------------------------------------------------------------------------------

bool __fastcall TGridEditLink::PrepareEdit(TBaseVirtualTree* Tree, PVirtualNode Node, TColumnIndex Column)
{
  PGridData Data;
  bool Result = true;

  FTree = (TVirtualStringTree*)Tree;
  FNode = Node;
  FColumn = Column;

  // Determine what edit type actually is needed.
  delete FEdit;
  FEdit = NULL;
  Data = FTree->GetNodeData(Node);
  switch(Data->ValueType[FColumn - 1])
  {
    case vtString:
        FEdit = new TEdit(NULL);
        if(FEdit)
        {
          ((TEdit*)FEdit)->Visible = false;
          ((TEdit*)FEdit)->Parent = Tree;
          ((TEdit*)FEdit)->Text = Data->Value[FColumn - 1];
          ((TEdit*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtPickString:
        FEdit = new TComboBox(NULL);
        if(FEdit)
        {
          ((TComboBox*)FEdit)->Visible = false;
          ((TComboBox*)FEdit)->Parent = Tree;
          ((TComboBox*)FEdit)->Text = Data->Value[FColumn - 1];
          // Here you would usually do a lookup somewhere to get
          // values for the combobox. We only add some dummy values.
          switch(FColumn)
          {
          case  2:
                ((TComboBox*)FEdit)->Items->Add("John");
                ((TComboBox*)FEdit)->Items->Add("Mike");
                ((TComboBox*)FEdit)->Items->Add("Barney");
                ((TComboBox*)FEdit)->Items->Add("Tim");
                break;
          case  3:
                ((TComboBox*)FEdit)->Items->Add("Doe");
                ((TComboBox*)FEdit)->Items->Add("Lischke");
                ((TComboBox*)FEdit)->Items->Add("Miller");
                ((TComboBox*)FEdit)->Items->Add("Smith");
                break;
          }
          ((TComboBox*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtNumber:
        FEdit = new TMaskEdit(NULL);
        if(FEdit)
        {
          ((TMaskEdit*)FEdit)->Visible = false;
          ((TMaskEdit*)FEdit)->Parent = Tree;
          ((TMaskEdit*)FEdit)->EditMask = "9999;0; ";
          ((TMaskEdit*)FEdit)->Text = Data->Value[FColumn - 1];
          ((TMaskEdit*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtPickNumber:
        FEdit = new TComboBox(NULL);
        if(FEdit)
        {
          ((TComboBox*)FEdit)->Visible = false;
          ((TComboBox*)FEdit)->Parent = Tree;
          ((TComboBox*)FEdit)->Text = Data->Value[FColumn - 1];
          ((TComboBox*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtMemo:
        FEdit = new TComboBox(NULL);
        // In reality this should be a drop down memo but this requires
        // a special control.
        if(FEdit)
        {
          ((TComboBox*)FEdit)->Visible = false;
          ((TComboBox*)FEdit)->Parent = Tree;
          ((TComboBox*)FEdit)->Text = Data.Value[FColumn - 1];
          ((TComboBox*)FEdit)->Items->Add(Data->Value[FColumn - 1]);
          ((TComboBox*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    case vtDate:
        FEdit = new TDateTimePicker(NULL);
        if(FEdit)
        {
          ((TDateTimePicker*)FEdit)->Visible = False;
          ((TDateTimePicker*)FEdit)->Parent = Tree;
          ((TDateTimePicker*)FEdit)->CalColors->MonthBackColor = clWindow;
          ((TDateTimePicker*)FEdit)->CalColors->TextColor = clBlack;
          ((TDateTimePicker*)FEdit)->CalColors->TitleBackColor = clBtnShadow;
          ((TDateTimePicker*)FEdit)->CalColors->TitleTextColor = clBlack;
          ((TDateTimePicker*)FEdit)->CalColors->TrailingTextColor = clBtnFace;
          ((TDateTimePicker*)FEdit)->Date = StrToDate(Data->Value[FColumn - 1]);
          ((TDateTimePicker*)FEdit)->OnKeyDown = EditKeyDown;
        }
        break;
    default:
        Result = false;
  }
  return Result;
}
*/

#pragma package(smart_init)

⌨️ 快捷键说明

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