文件监视_能用_但似乎不太完善

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <ShellAPI.h>
#include <ShlObj.h>
#include <ShTypes.h>

#define WM_USERDEF_FILECHANGED 0x1000

LRESULT CALLBACK WindowProc(HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam)
{
 switch(uMsg)
 {
 case WM_KEYDOWN:
  if(wParam == VK_ESCAPE)
  {
   PostQuitMessage(0);
  }
  break;
 case WM_USERDEF_FILECHANGED:
  switch(lParam)
  {
  case SHCNE_RENAMEITEM: MessageBoxA(0, "重命名文件", "提示", MB_OK); break;
  case SHCNE_CREATE: MessageBoxA(0, "建立文件", "提示", MB_OK); break;
  case SHCNE_DELETE: MessageBoxA(0, "删除文件", "提示", MB_OK); break;
  case SHCNE_MKDIR: MessageBoxA(0, "新建目录", "提示", MB_OK); break;
  case SHCNE_RMDIR: MessageBoxA(0, "删除目录", "提示", MB_OK); break;
  case SHCNE_MEDIAINSERTED: MessageBoxA(0, "插入可移动存储介质", "提示", MB_OK); break;
  case SHCNE_MEDIAREMOVED: MessageBoxA(0, "移去可移动存储介质", "提示", MB_OK); break;
  case SHCNE_DRIVEREMOVED: MessageBoxA(0, "移去驱动器", "提示", MB_OK); break;
  case SHCNE_DRIVEADD: MessageBoxA(0, "添加驱动器", "提示", MB_OK); break;
  case SHCNE_NETSHARE: MessageBoxA(0, "改变目录", "提示", MB_OK); break;
  case SHCNE_ATTRIBUTES: MessageBoxA(0, "改变文件目录属性", "提示", MB_OK); break;
  case SHCNE_UPDATEDIR: MessageBoxA(0, "更新目录", "提示", MB_OK); break;
  case SHCNE_UPDATEITEM: MessageBoxA(0, "更新文件", "提示", MB_OK); break;
  case SHCNE_SERVERDISCONNECT: MessageBoxA(0, "断开与服务器的连接", "提示", MB_OK); break;
  case SHCNE_UPDATEIMAGE: MessageBoxA(0, "SHCNE_UPDATEIMAGE", "提示", MB_OK); break;
  case SHCNE_DRIVEADDGUI: MessageBoxA(0, "SHCNE_DRIVEADDGUI", "提示", MB_OK); break;
  case SHCNE_RENAMEFOLDER: MessageBoxA(0, "重命名文件", "提示", MB_OK); break;
  case SHCNE_FREESPACE: MessageBoxA(0, "磁盘空间大小改变", "提示", MB_OK); break;
  case SHCNE_ASSOCCHANGED: MessageBoxA(0, "改变文件关联", "提示", MB_OK); break;
  }
  ShowWindow(hWnd, SW_HIDE);
  UpdateWindow(hWnd);
  break;
 }
 return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

int main()
{
 //Register And Create Window
 WNDCLASS wc;
 ZeroMemory(&wc,sizeof(wc));
 wc.cbClsExtra = 0;
 wc.cbWndExtra = 0;
 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 wc.hCursor = LoadCursor(0, IDC_ARROW);
 wc.hIcon = LoadIcon(0, IDI_APPLICATION);
 wc.hInstance = 0;
 wc.lpfnWndProc = (WNDPROC)WindowProc;
 wc.lpszClassName = L"Music";
 wc.lpszMenuName = NULL;
 wc.style = CS_HREDRAW | CS_VREDRAW;
 if(RegisterClassW(&wc) == 0) return 0;
 HWND hWnd = CreateWindowW(wc.lpszClassName, L"文件监视", WS_OVERLAPPED, 633, 334, 100, 50, 0, 0, 0, 0);
 if(hWnd == INVALID_HANDLE_VALUE) return 0;
 ShowWindow(hWnd, SW_HIDE);
 UpdateWindow(hWnd);

 //监视文件
 SHChangeNotifyEntry shEntry = {0};
 shEntry.fRecursive = TRUE;
 shEntry.pidl = 0;
 ULONG g_ulNotifyId = SHChangeNotifyRegister( hWnd, SHCNRF_InterruptLevel | SHCNRF_ShellLevel, SHCNE_ALLEVENTS, WM_USERDEF_FILECHANGED, 1, &shEntry );
 if(g_ulNotifyId == 0)
 {
  MessageBoxA(0, "Register failed!","ERROR",MB_OK|MB_ICONERROR);
  return -1;
 }

 //Msg Looping
 MSG msg;
 ZeroMemory(&msg, sizeof(MSG));
 while(msg.message != WM_QUIT)
 {
  if(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
  {
   TranslateMessage(&msg);
   DispatchMessageA(&msg);
  }
  else Sleep(200);
 }
 
 //解除监视
 if(g_ulNotifyId != 0)
 {
  SHChangeNotifyDeregister(g_ulNotifyId);
 }

 return 0;
}

 

//对于消息中wParam还不明白什么意思

//SHCNRF_InterruptLevel和SHCNRF_ShellLevel也不太理解

posted on 2012-04-25 20:37  紫澜  阅读(497)  评论(0编辑  收藏  举报