关于Win32 API 函数 ReadDirectoryChangesW(CDirectoryChangeWatcher by Wes Jones)的使用

主题:关于Win32 API 函数ReadDirectoryChangesW的使用

说明:Wes Jones在codeproject上把这个API函数已经打包封装好了,取名为CDirectoryChangeWatcher,我写的内容是如何在VC++下使用Wes Jones已经打包的这个程序

函数所起的作用:如何利用CDirectoryChangeWatcher(或ReadDirectoryChangesW)响应文件夹及其子文件夹下文件变动产生的消息


Wes Jones写的代码链接如下:

http://www.codeproject.com/KB/files/directorychangewatcher.aspx


下面是我自己总结的内容(一直想写中文的,没时间,先把英文的贴过来吧)

我做的程序作用是:监控某个特定目录下图片的增加和减少,并将新添的图片显示出来

These codes are helpful for me. But they still miss events. Next, I'd like to describe it in detail.
 
First,I'd like to explain how I've used these codes in my project and share my experience.
 
Trouble 1:After adding four source files kindly provided by Mr Wes Jones through code project, there should be compile errors.
 
Solution:1.1、Add this code - "#define _WIN32_WINNT 0x0500" in front of "#include..." in "stdAfx.h"
&1.2、Build → Set active configuration... → XX-win32 debug or XX-win32 release → OK
 
Trouble 2:How to use these codes
 
Solution:Rewrite Class CDirectoryChangeHandler_ListBox.
 
2.1、Make the least change to use these codes(Directly use source codes kindly provided by Wes).
 
For example, when a file is added in my watch directory,I want a pop-up message box. Then I will add codes in function "On_FileAdded" in the file "DirWatcherDlg.h" as below.
HWND hWnd=m_listBox.GetSafeHwnd();
MessageBox(hWnd,"OK","Notice",0);

 
As you can see, we just need to use m_listBox.GetDC(),m_listBox.ReleaseDC(),m_listBox.FindWindow() ..., m_listBox.XX(/*FunctionName,it must be able to be called by CListBox members*/)() to get what you want for a simple application.
 
2.2、Simplify functions of these codes.
 
2.2.1 Create a new project based on dialog(eg: Project name is Test).
 
2.2.2 Operation about dialog resource.
Add essential rescources(A button & a listbox) in project TestDlg. Create a message for left click on the button(named test) and a CListBox type variable for the listbox(eg. m_lstChanges).
 
2.3.2 Modify header files: Copy or create class CDirectoryChangeHandler_ListBox and add two variables'
a. Copy codes about class CDirectoryChangeHandler_ListBox from head file "DirectoryChanges.h" and add it in the head file of newly created project dialog(TestDlg.h).
b. Copy this two lines from "DirWatcherDlg.h" as below and add them in project dialog head file(TestDlg.h).
CDirectoryChangeWatcher m_DirWatcher;
CDirectoryChangeHandler_ListBox m_DirChangeHandler;
Remember to include "DirectoryChanges.h".
 
2.3.3 Modify cpp files: Add one function and implement message by clicking on the test button.
a. Copy the body of function GetLastErrorMessageString and add it to implementation file of our dialog(TestDlg.cpp).
b. Copy the following codes and add it into the body of message function produced by clicking on the button(named Test) in our dialog cpp file(TestDlg.cpp). These codes have already delete the filter function(Codes in bold have been modified).
DWORD dwWatch = 0;
const char* pDirectoryToMonitor="E:\\My Documents\\My Pictures";
if( ERROR_SUCCESS != (dwWatch = m_DirWatcher.WatchDirectory(pDirectoryToMonitor,
FILE_NOTIFY_CHANGE_FILE_NAME/*dwChangeFilter*/,
&m_DirChangeHandler,
FALSE/*bWatchSubDir*/,
NULL/*m_strIncludeFilter1*/,
NULL/*m_strExcludeFilter1*/)) )
{
MessageBox(_T("Failed to start watch:\n") + GetLastErrorMessageString(dwWatch) );
}
c. Add the following two lines of codes in the parameter list of constructor of class TestDlg in our dialog cpp file(TestDlg.cpp).
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
,m_DirWatcher( true )
,m_DirChangeHandler( m_lstChanges )

Compile,link and run, then copy sth it the watched directory, what you have copied will appear in the list box.
 
2.3、Add new controls to the project mentioned in 2.2.
In this project, I've added a static picture control to show images added in my watch directory. Then how shall we modify these codes.
 
2.3.1 First,give a unique control ID to this static control,and add a type of CStatic member variable -m_staticShowImg for this ID.
 
2.3.2 Then, in the definition of "CDirectoryChangeHandler_ListBox " just close to this code - "CListBox & m_listBox;",add "CStatic & m_staticImg;".
 
2.3.3 Third, add a new parameter in the parameter list of constructor of class CDirectoryChangeHandler_ListBox as below:
CDirectoryChangeHandler_ListBox(CListBox & list_box,CStatic & staticImg)
: CDirectoryChangeHandler(),
m_listBox( list_box ),
m_staticImg( staticImg ) {}
 
2.3.4 Fourth, add a CStatic parameter in constructor of CDirWatcherDlg(or your own dialog)
CDirWatcherDlg::CDirWatcherDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDirWatcherDlg::IDD, pParent)
,m_DirWatcher( true )
,m_DirChangeHandler( m_lstChanges, m_staticShowImg(a CStatic member variable created in 2.3.1) )
 
2.3.5 Fifth, add your own code in the body of On_FileXX in class CDirectoryChangeHandler_ListBox.
 
HWND hMCImgWnd=m_staticImg.GetSafeHwnd();
CRect rt(0,0,240,240);
CDC* pMCdc=m_staticImg.GetDC();
img.AttachFromFile(strFileName);/*(img is a instance of another Class about image processing)*/
img.Draw(pMCdc,rt);
// MessageBox(hMCImgWnd,"OK","char",0);
m_staticImg.ReleaseDC(pMCdc);
 

Then,here comes my question.
Compile,link,run my project.everything is OK till now.
But after I clicked the test button when I copy image files in my watch directory, it's seldom show all the images I've copied for test. Sometimes,it shows half of the copied images regularly with one showing after another missing.Sometimes, it doesn't show any picture no matter how many times you've copied and how long you're waiting for.
 
Another question worrying me is how I can show two consecutive images at the same time.

我当时给Mr.  Wes Jones 留言的时候,把我写的程序发给了他,我的也考了三张美女图——章子怡、奥黛丽赫本和可爱的仓老师做测试,运行自己的程序就像我上面最后说的那样,同时考2张或3张美女图到监控的文件加下,程序就只能显示最后一个美女图。但是隔段时间,运行又没有问题,难怪Mr. Wes Jones当时回我说没啥问题啊,我以为他不愿意帮忙。至于为啥时对时错我也搞不清楚。。。。。。

程序运行后的界面如下(图片太大尺寸比我程序里的显示空间大,所以不好意思只让章子怡露了个香肩)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值