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

📄 dirnotify.cpp

📁 Dir Notify Mointor
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "DirNotify.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TDirNotify *)
{
  new TDirNotify(NULL);
}
//---------------------------------------------------------------------------

__fastcall TDirNotify::TDirNotify(TComponent* Owner)
        : TComponent(Owner)
{
  FEnabled = true;
  FFilter = TNotifyFilters::nfFileName;
}
//---------------------------------------------------------------------------

namespace Dirnotify
{
  void __fastcall PACKAGE Register()
  {
    TComponentClass classes[1] = {__classid(TDirNotify)};
    RegisterComponents("MySelf", classes, 0);
  }
}
//---------------------------------------------------------------------------

void __fastcall TDirNotify::SetEnabled(bool Value)
{
  if (Value != FEnabled)
  {
    FEnabled = Value;
    RecreateThread(this);
  }
}
//---------------------------------------------------------------------------

void __fastcall TDirNotify::SetFilter(TNotifyFilter Value)
{
  if (Value != FFilter)
  {
    FFilter = Value;
    RecreateThread(this);
  }
}
//---------------------------------------------------------------------------

void __fastcall TDirNotify::SetOnChange(TNotifyEvent Value)
{
  FOnChange = Value;
}
//---------------------------------------------------------------------------

void __fastcall TDirNotify::SetPath(AnsiString Value)
{
  if (Value != FPath)
  {
    FPath = Value;
    RecreateThread(this);
  }
}
//---------------------------------------------------------------------------

void _fastcall TDirNotify::SetWatchSubTree(bool Value)
{
  if (Value != FWatchSubTree)
  {
    FWatchSubTree = Value;
    RecreateThread(this);
  }
}
//---------------------------------------------------------------------------

void __fastcall TDirNotify::RecreateThread(TObject *Sender)
{
  if (FEnabled == false)
  {
    FNotificationThread->Terminate();
    FNotificationThread = NULL;
  }

  if ((FEnabled == true) && (FPath != ""))
  {
    FNotificationThread = new TNotificationThread(false, this);
    FNotificationThread->Resume();
  }
}
//---------------------------------------------------------------------------

void __fastcall TDirNotify::Change(void)
{
  if (FOnChange != NULL)
    FOnChange(this);
}
//---------------------------------------------------------------------------

__fastcall TNotificationThread::TNotificationThread(bool CreateSuspended, TObject *Sender)
        : TThread(CreateSuspended)
{
  DirNotifyOwner = (TDirNotify *)Sender;
  FreeOnTerminate = true;
}
//----------------------------------------------------------------------------

void __fastcall TNotificationThread::Execute()
{
  HANDLE h;
  BOOL wst;
  int nf = FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE ;

  if (DirNotifyOwner->Filter == TNotifyFilters::nfFileName)
    nf |= FILE_NOTIFY_CHANGE_FILE_NAME;

  if (DirNotifyOwner->Filter == TNotifyFilters::nfDirName)
    nf |= FILE_NOTIFY_CHANGE_DIR_NAME;

  if (DirNotifyOwner->Filter == TNotifyFilters::nfAttributes)
    nf |= FILE_NOTIFY_CHANGE_ATTRIBUTES;

  if (DirNotifyOwner->Filter == TNotifyFilters::nfSize)
    nf |= FILE_NOTIFY_CHANGE_SIZE;

  if (DirNotifyOwner->Filter == TNotifyFilters::nfLastWrite)
    nf |= FILE_NOTIFY_CHANGE_LAST_WRITE;

  if (DirNotifyOwner->Filter == TNotifyFilters::nfSecurity)
    nf |= FILE_NOTIFY_CHANGE_SECURITY;

  if (DirNotifyOwner->WatchSubTree)
    wst = BOOL(1);
  else
    wst = BOOL(0);

  h = FindFirstChangeNotification((DirNotifyOwner->Path).c_str(), wst, nf);

  if (h == INVALID_HANDLE_VALUE)
  {
    throw ("Error! Invalid Handle Value! Notify to Author!");
  }

  while (!Terminated)
  {
    if (WaitForSingleObject(h, 1000) == WAIT_OBJECT_0)
    {
      Synchronize(DoChange);
      if (!FindNextChangeNotification(h))
        throw ("Error! Notify to Author!");
    }
  }
}
//---------------------------------------------------------------------------

void __fastcall TNotificationThread::DoChange(void)
{
  DirNotifyOwner->Change();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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