SHChangeNotifyRegister in Windows CE

来源:互联网 发布:淘宝照片后期处理 编辑:程序博客网 时间:2024/05/16 15:31

BOOL WINAPI SHChangeNotifyRegister(
  HWNDhwnd,
  SHCHANGENOTIFYENTRY * pshcne
);

第一个参数需要一个窗口句柄,用CreateWindow()方法可以创建一个窗口(创建窗口前需要调用RegisterClass()方法,在RegisterClass这个参数中需要传入实现我们自己的消息处理函数WindowProc())。
第二个参数是注册我们需要获取的通知消息。
上面这些处理完成过后,我们就可以用GetMessage()来获取系统给我们的通知消息了。
窗口消息中有很多其他我们不想要的消息,在此处我们关心的是 WM_FILECHANGEINFO(33025) 消息。对此消息做处理:
FILECHANGENOTIFY *lpfcn = NULL;
FILECHANGEINFO *lpfci = NULL;
lpfcn = (FILECHANGENOTIFY *)lParam;//lParam是这个消息处理函数传入的参数。
lpfci = &(lpfcn->fci);
switch(lpfci->wEventId)
{
    case SHCNE_CREATE://消息通知事件
    break;
    .....
}
不在做说明,可以看 FILECHANGENOTIFY 介绍。
在处理结束后需要调用SHChangeNotifyFree(lpfcn)释放资源。
相关代码:
  1. BOOL LogSHCNEEvent(FILECHANGEINFO *lpfci)
  2. {
  3.     WCHAR *path1 = NULL;
  4.     WCHAR *path2 = NULL;
  5.     switch(lpfci->wEventId)
  6.     {
  7.     case SHCNE_RENAMEITEM:
  8.         break;
  9.     case SHCNE_CREATE:
  10.         break;
  11.         //.......//other message
  12.         }
  13.         //....
  14. }
  15. LRESULT CALLBACK WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  16. {
  17. // Structures for handling the File Change Information
  18.     FILECHANGENOTIFY *lpfcn = NULL;
  19.     FILECHANGEINFO *lpfci = NULL;
  20.     switch(Msg)
  21.     {
  22. case WM_FILECHANGEINFO:
  23.         // see if the pointer to the file change notify struct is valid
  24.         lpfcn = (FILECHANGENOTIFY *)lParam;
  25.         if (NULL == lpfcn)
  26.         {
  27.             break;
  28.         }
  29.         // see if the pointer to the file change info structure
  30.         lpfci = &(lpfcn->fci);
  31.         if (NULL == lpfci)
  32.         {
  33.             break;
  34.         }
  35.         else
  36.         {
  37.             if (FALSE == LogSHCNEEvent(lpfci))
  38.             {
  39.                 MessageBox(hWnd, TEXT("SCHNE Event was not logged"), TEXT("Program Error"), MB_OK);
  40.             }                    
  41.         }
  42.         SHChangeNotifyFree(lpfcn);
  43.         break;
  44.         }
  45. }
  46. ATOM MyRegisterClass(LPTSTR szWindowClass)
  47. {
  48.     WNDCLASS wc;
  49.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  50.     wc.lpfnWndProc   = WindowProc;
  51.     wc.cbClsExtra    = 0;
  52.     wc.cbWndExtra    = 0;
  53.     wc.hInstance     = NULL;
  54.     wc.hIcon         = NULL;
  55.     wc.hCursor       = 0;
  56.     wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  57.     wc.lpszMenuName  = 0;
  58.     wc.lpszClassName = szWindowClass;
  59.     return RegisterClass(&wc);
  60. }
  61. void ParseMessage()
  62. {
  63.     MSG msg;
  64.     while (TRUE)
  65.     {
  66.         GetMessage(&msg, NULL, 0, 0);
  67.         TranslateMessage(&msg);
  68.         DispatchMessage(&msg);
  69.     }
  70. }
  71. DWORD SHNCInit()
  72. {
  73.     BOOL bl_ret = FALSE;
  74.     WCHAR *winClass = _T("console");
  75.     WCHAR *title = _T("notify");
  76.     DWORD dw_error = 0;
  77.     SHCHANGENOTIFYENTRY pshcne;
  78.     if(0 == MyRegisterClass(winClass))
  79.     {
  80.         dw_error = GetLastError();
  81.         return dw_error;
  82.     }
  83.     g_hWnd = CreateWindow(winClass, title, WS_VISIBLE, 
  84.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
  85.         NULL, NULL, (HINSTANCE)GetCurrentProcess(), NULL);
  86.     if (NULL == g_hWnd)
  87.     {
  88.         dw_error = GetLastError();
  89.         return dw_error;
  90.     }
  91.     pshcne.dwEventMask = SHCNE_ALLEVENTS;
  92.     pshcne.pszWatchDir = NULL;
  93.     pshcne.fRecursive = TRUE;
  94.     bl_ret = SHChangeNotifyRegister(g_hWnd, &pshcne);
  95.     if (FALSE == bl_ret)
  96.     {
  97.         dw_error = GetLastError();
  98.         return dw_error;
  99.     }
  100.     return 0;
  101. }
  102. void SHNCListener()
  103. {
  104.     DWORD dw_error = 0;
  105.     dw_error = SHNCInit();
  106.     ParseMessage();
  107. }
原创粉丝点击