A typical example solves the common advanced Windows programming problem

zhaozj2021-02-08  231

/ / ############################################################################### ####################################################### Reserved.///project name: fileinspector rt // Software version 1.0 //// ------------------------------ ---------------------------- /// File name: fileinspector.h // function: define of classes // created Time: 0000-00-00: DEFINE OF Classes // CREATED TIME: 28th dec, 2002 // Author: hightech young //// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---------------------- //// REVIDION TIME: // Revision By: // Revision Contents: //// ####### ######################################################################################################################################################################################################################################################################################################## #############################################

#include "resource.h" #include "stdafx.h" #include "afxmt.h"

/ / -------------------------------------------------------------------------------------------- --------------------------------------- / /// Comments: Define of Appla Acution CLASS / /// ----------------------------------- ----------------------------------------

Class CMYAPP: PUBLIC CWINAPPPPPPPPPPPPPPP: Virtual Bool InitInstance ();

/ / -------------------------------------------------------------------------------------------- --------------------------------------- /// Comments: Define of Mainwindow Class / /// ----------------------------------- ----------------------------------------

Class CMainWindow: Public CWnd {// // Declare All Objects of Interface // Private: CStatic M_Fitip; CButton M_GroupBoxfi; Cedit M_ObjectPath; CButton M_Browse;

CBUTTON M_GROUPBOXFS; CSTATIC M_FSTIP; CBUTTON M_GB; CBUTTON M_MB; CBUTTON M_KB; CBUTTON M_BYTE;

CButton M_OK; CButton M_Cancel; CButton M_Exit;

PUBLIC: CMAINWINDOW ();

Bool BoolWndhadshow; // WINDOW Display Indicator

Protected: CEVENT M_EVENTSTOPWATCH; // CEVENT OBJECT for Synchonize Thread

Virtual void postncdestroy (); //////////////////ww_msg int oncreate (LPCReatestruct LPCS); AFX_MSG Void OnSysCommand (Uint Nid, LParam Lparam);

AFX_MSG LRESULT ONMYICONNNOTIFY (WPARAM WPARAM, LPARAM LPARAM); AFX_MSG LRESULT OnShowAppiconic (WParam WPARAM, LPARAM LPARAM); AFX_MSG Void OnClose ();

afx_msg void OnBrowseButonClicked (); afx_msg void OnObjectPathChanged (); afx_msg void OnOkButtonClicked (); afx_msg void OnCancelButtonClicked (); afx_msg void OnExitButtonClicked ();

AFX_MSG Long OnfileChanged (WPARAM WPARAM, LPARAM LPARAM); DECLARE_MESSAGE_MAP ()};

// ##################################### End of File ######## ###############################################

// ################################################ ####################################################### Reserved.///project name: fileinspector rt // Software version 1.0 //// ------------------------------ ---------------------------- /// File Name: fileinspector.cpp // function: importation of the class // created Time : 28th Dec, 2002 // Created by: hightech Young //// -------------------------------------------------------------------------------------------------------------------- ------------------------ //// REVIDITTION TIME: // Revision By: // Revision Contents: //// ##### ################################################## ##########################################################

#include "fileinspector.h"

// ------------------------------------------------ --------------------------------------- /// Comments: Globe Defination Area // // ------------------------------------------------ ---------------------------------------

//// user defined message for callback function //////////// User Defined Message for Callback Function //

#define user_wm_notifyicon wm_user 0x001

//// User defined message for display icon on statue bar of windows //

#define user_wm_showappconic WM_USER 0X002 //// User defined message for file change notification //

#define wm_user_thread_file_changed WM_USER 0x003

/// The structure was defined for the input of thread function parameters //

Typedef struct tagthreadparam {hwnd hwnd; cevent * pevent;} threadparam;

/// Worker Thread Function Declare //

Uint ThreadFunction (LPVOID PPARAM);

/ / -------------------------------------------------------------------------------------------- --------------------------------------- /// (Comments: Message Map of CMAINDOW CLASS //// ------------------------------------------------------------------------------------------------------------------------------------------------------------ -----------------------------------------

BEGIN_MESSAGE_MAP (CMainWindow, CWnd) ON_WM_CREATE () ON_MESSAGE (USER_WM_NOTIFYICON, OnMyIconNotify) ON_MESSAGE (USER_WM_SHOWAPPICONIC, OnShowAppIconic) ON_WM_SYSCOMMAND () ON_WM_CLOSE ()

ON_BN_CLICKED (IDC_BROWSE, OnBrowseButonClicked) ON_EN_CHANGE (IDE_OBJ_PATH, OnObjectPathChanged) ON_BN_CLICKED (IDC_OK, OnOkButtonClicked) ON_BN_CLICKED (IDE_OBJ_PATH, OnObjectPathChanged) ON_BN_CLICKED (IDC_EXIT_FI, OnExitButtonClicked)

ON_MESSAGE (WM_USER_THREAD_FILE_CHANGED, ONFILECHANGED) END_MESSAGE_MAP ()

/// / defination of application object //

CMYAPP myApp;

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: BOOL CMYAPP :: InitInstance () // purpose: CMYAPP Member functions // Comments: //// -------------------------------- -------------------------------------------------- ----

Bool CmyApp :: InitInstance () {m_pmainwnd = new cmainwindow;

m_pmainwnd-> showwindow (m_ncmdshow); m_pmainwnd-> UpdateWindow ();

//// send mywm_showappiconic SO That The window can be // hide after program runed and the icon shop be // displayed on the statue area of ​​task bar //

M_PMainWnd -> PostMessage (user_wm_showappiconic); return true;} // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- - //// Function: cmainwindow :: cmainwindow () // purpose: The constructor of the cmainwindow class // Comments: //// ----------------- -------------------------------------------------- ------------------

CMAINWINDOW :: CMainWindow (): m_eventstopWatch (false, true) {//// register a wndclass. //

CString strWndClass = AfxRegisterWndClass (CS_DBLCLKS, // Class style AfxGetApp () -> LoadStandardCursor (IDC_ARROW), // Class cursor (HBRUSH) (COLOR_3DFACE 1), // Background brush AfxGetApp () -> LoadIcon (IDR_FILE_INSPECTOR) // Class icon);

/// Create a window. //

CreateEx (0, strWndClass, _T ( "File / Folder Supervisor"), WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL);

///// size the window. // CRECT RECT (80, 70, 452, 400); CalcWindowRect (& Re);

SetWindowPos (NULL, 0, 0, Rect.width (), Rect.Height (), SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);

}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: int CmainWindow :: oncreate (Lpcreatestruct lpcs) // purpose: Create Gui of Program // Comments: //// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------

INT CMAINWINDOW :: OnCreate (lpcreatestruct lpcs) {if (cWnd :: oncreate (lpcs) == -1) Return -1;

////////Bit selected group box

m_GroupBoxFI.Create (_T ( "Supervised Target:"), WS_CHILD | BS_GROUPBOX | WS_VISIBLE, CRect (10,10,360,115), this, IDC_GROUPBOX_FI); m_FiTip.Create (_T ( "Please select the file or folder which you want to ask the File / Folder Supervisor supervise "), WS_CHILD | WS_VISIBLE, CRect (25,35,350,75), this, IDS_FI_TIP);. m_ObjectPath.Create (WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, CRect (25,80,260,100), this, IDE_OBJ_PATH) M_Browse.create (_T ("Browse .."), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRECT (265, 80, 350, 100), this, IDC_BROWSE); / / / FILE SIZE LIMITATION CONFIG BOX / /M_GROUPBOXFS.CREATE (_t (" File size Limitation: "), WS_CHILD | BS_GROUPBOX | WS_VISIBLE, CRect (10,120,360,280), this, IDC_GROUPBOX_FS); m_FsTip.Create (_T (" The Folder or File size must be less than: "), WS_CHILD | WS_VISIBLE, CRect (25,145,350,165 ), This, IDs_fs_tip); m_objectsize.create (ws_child | ws_visible | ws_border | es_A UTOHSCROLL, CRECT (30, 170, 100, 190), this, IDE_OBJ_SIZE);

m_GB.Create (_T ( "GB"), WS_CHILD | WS_GROUP | WS_VISIBLE | BS_AUTORADIOBUTTON, CRect (110,170,150,190), this, IDC_GB); m_MB.Create (_T ( "MB"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, CRect (155,170,195,190) , this, IDC_MB); m_KB.Create (_T ( "KB"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, CRect (200,170,240,190), this, IDC_GB); m_Byte.Create (_T ( "Byte"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, CRECT (245, 170, 325, 190), this, IDC_BYTE); m_kb.setCheck (bst_checked);

// // Buttons //

m_OK.Create (_T ( "Start"), WS_CHILD | WS_GROUP | BS_DEFPUSHBUTTON | WS_VISIBLE, CRect (50,290,150,315), this, IDC_OK); m_Cancel.Create (_T ( "Cancel"), WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, CRect (155,290,255,315) , This, IDC_cancel); m_cancel.enableWindow (false); m_exit.create (_T ("stop"), ws_child | bs_pushbutton | WS_Visible, CRECT (260, 290, 360, 315), this, IDC_EXIT_FI);

Return 0;}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: void cmainwindow :: Postncdestroy () // purpose: Postncdestroy Will Be Used for delete the window // Comments: //// ---------------------------- -------------------------------------------------- ---------

Void cmainwindow :: postncdestroy () {delete this;}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- /// / Function: BOOL MyTaskbaraddicon (HWND HWnd , Uint Uid, Hicon Hicon, LPSTR LPSZTIP) // Purpose: Add icon in The Statu Area on The Task Bar // Comments: //// ---------------------------------------------------------------------------------------- -------------------------------------------------- -------------------

BOOL MyTaskBarAddIcon (HWND hwnd, UINT uID, HICON hicon, LPSTR lpszTip) {BOOL bResult; NOTIFYICONDATA tnid; tnid.cbSize = sizeof (NOTIFYICONDATA); tnid.hWnd = hwnd; tnid.uID = uID; tnid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE; tnid.uCallbackMessage = USER_WM_NOTIFYICON; // Callback message tnid.hIcon = hicon; // Icon used if (lpszTip) lstrcpyn (tnid.szTip, lpszTip, sizeof (tnid.szTip)); else tnid.szTip [0] = '/ 0';

/// send nim_add to create icon //

BRESULT = shell_notifyicon (NIM_ADD, & TNID); IF (Hicon) Destroyicon (HICON);

Return BRESULT;

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: BOOL MyTaskbardeleteicon (HWND HWND , Uint uid) // purpose: delete icon in The Statu Area on the task bar // Comments: //// ---------------------- -------------------------------------------------- ------------- Bool MyTaskBardeleteicon (HWND HWND, UINT UID) {Bool Bresult; Notifyicondata Tnid; Tnid.cbsize = SizeOf (Notifyicondata); TNID.HWND = Hwnd; TNID.UID = UID;

/// send nim_delete to create icon //

BRESULT = shell_notifyicon (Nim_Delete, & TnID);

Return BRESULT;

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: LRESULT CMAINWINDOW :: OnMyiconnotify (WPARAM WPARAM, LPARAM LPARAM) // Purpose: Call Back Function of Message MyWM_NotifyiCon // Comments: //// ------------------------- -------------------------------------------------- ----------------

LRESULT CMainWindow :: OnMyIconNotify (WPARAM wParam, LPARAM lParam) {UINT nID; UINT uMouseMsg; nID = (UINT) wParam; uMouseMsg = (UINT) lParam; // // Show or hide window // if (uMouseMsg == WM_LBUTTONDOWN) // Click ON ICON {if (BoolWndhadShow) showwindow (sw_hide); else showwindow (sw_shownormal); BoolWndhadShow = ~ BoolWndhadShow;} Return True;}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: // LRESULT CMAINWINDOW: : OnShowAppIconic (wPARAM wParam, lPARAM lParam) // PURPOSE: // Message handler of show icon on statu bar // and hide the window when programe run // COMMENTS: // Call back function of message MYWM_SHOWAPPICONIC //// - -------------------------------------------------- ---------------------------------- Lresult Cmainwindow :: OnShowappPiconic (WPARAM WPARAM, LPARAM LPARAM) {Hicon Theicon = LoadIcon (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDR_FILE_INSPECTOR)); MyTaskBarAddIcon (GetSafeHwnd (), 100, theIcon, _T ( "File / Folder Supervisor")); ShowWindow (SW_HIDE); boolWndHadShow = FALSE; return 1;}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: void cmainwindow :: onClose () // purpose: // WHEN You Want to Close Program, The icon shop // Be delete from statue area on task bar, // We can use wm_close message and message handler to do this // Comments: //// -------------------------------------------------- -------------------------------------

Void cmainwindow :: onclose () {// // add your message hand default // mytaskbardeletelicon (getsafehwnd (), 100); cWnd :: onClose ();

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: // void cmainwindow: : OnsysCommand (Uint Nid, LParam Lparam) // Purpose: // When WERE minimumized, The Window Should Be Hide, // We Use ON_SYSCOMMAND Message and Message Handle To Do this // Comments: //// -------- -------------------------------------------------- --------------------------------- void cmainwindow :: oversyscommand (uint nid, lparam lparam) {if (NID = = SC_MINIMIZE) {ShowWindow (sw_hide); BoolWndhadShow = false;} else cWnd :: oversysCommand (NID, LPARAM);} // -------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- --------------- /// function: void cmainwindow :: onbrowsebutonclicked () // purpose: message handler of ok button // comments: //// ----- -------------------------------------------------- -------------------------------- Void cmainwindow :: onbrowsebutonclicked () {

// // must call imique by this pointer // lpmalloc pmalloc; browseinfo bi; char pszdirname [max_path]; // save the directory name lpitemidlist PIDL;

// // If retrieve a handle to the Shell allocator's IMalloc interface sucessfuly // if (SUCCEEDED (SHGetMalloc (& pMalloc))) {ZeroMemory (& bi, sizeof (bi)); bi.hwndOwner = GetSafeHwnd (); bi.pidlRoot = NULL; bi.pszDisplayName = pszDirName; bi.lpszTitle = _T ( "Select the file or folder which will be Supervised:"); bi.ulFlags = BIF_BROWSEINCLUDEFILES; bi.lpfn = NULL; bi.lParam = 0; if ((pidl ! = :: SHBrowseForFolder (& bi)) = NULL) {// // Get full path user selected // if (:: SHGetPathFromIDList (pidl, pszDirName)) m_ObjectPath.SetWindowText (pszDirName);} pMalloc-> Free (pidl) ; pMalloc-> Release ();} // // Try to detect the inspect object is directory or file // Can also use: // DWORD GetFileAttributes (LPCTSTR lpFileName); // DWORD ObjectType; ObjectType = GetFileAttributes (pszDirName); IF (ObjectType & File_Attribute_directory) {m_groupboxfi.setwindowText ("Folder Supervised:")); m_groupboxfs.setwindowText ("Folder Size Limitation");} else { M_GroupBoxFi.SetWindowText ("File Supervised:"); m_groupboxfs.setwindowText ("File Size Limitation"));} /// Enable The Cancel Button // m_cancel.enableWindow (TRUE);}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: void cmainwindow :: onokbuttonclicked () // PURPOSE: Message handler of OK button // COMMENTS: // This function transfer a structure's pointer // as a LPVOID parameter to worker thread this is the normal way // to handle the worker thread parameters Besides, you.. Can Also // Input A Address of An Object Directly.///---------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- --- void cmainwindow :: onokbuttonclicked () {m_cancel.enablewindow (false); showwindow (sw_hide); BoolWndhadShow = false; //// reset the manual evenet Object Before Start Worker Thread ///////////////////////r

M_EventStopWatch.ReseTevent ();

/// Prepare the parameter for worry thread // sometimes if you can also // input & m_hWnd Directly there //

THREADPARAM * pThreadParam = new THREADPARAM; pThreadParam-> hWnd = m_hWnd; // Message target pThreadParam-> pEvent = & m_eventStopWatch; // Event to stop thread // // Start File Inspector thread // AfxBeginThread (ThreadFunction, pThreadParam);.}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: void cmainwindow :: oncancelbuttonclicked () // purpose: Message Handler of Cancel Button // Comments: //// ------------------------------- -------------------------------------------------- --------

Void cmainwindow :: oncancelbuttonclicked () {showwindow (sw_hide); BoolWndhadShow = false;}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: void cmainwindow :: OnexitButtonClicked () // purpose: Message Handler of exit button // Comments: //// ------------------------------- -------------------------------------------------- ------ Void CMainWindow :: OnexitButtonClicked () {showwindow (sw_hide); BoolWndhadShow = false; m_eventstopWatch.sevent ();

// // close the window // postmessage (wm_close, 0, 0);

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: void cmainwindow :: onObjectPathChanged () // purpose: Message Handler of Object Path Changed // Comments: //// ------------------------------ -------------------------------------------------- -------

Void cmainwindow :: onObjectPathChanged () {m_cancel.enablewindow;}

/ / -------------------------------------------------------------------------------------------- --------------------------------------- // // function: uint threadfunction (lpvoid pParam ) // purpose: worker thread function // Comments: //// ---------------------------------- -------------------------------------------------- ---

Uint threadfunction (lpvoid pParam) {/// unbound the parameters //

Threadparam * pthreadparam = (threadparam *) PParam; hwnd hwnd = pthreadparam -> hw; CEVENT * PEVENT = PthreadParam -> pevent; // delete pthreadparam; delete pthreadparam;

DWORD DWWAITSTATUS; Handle * dwchangehandles = new handle [3];

///////////////////////////////////////////////////////////////////////////////>

DWChangeHandles [2] = pevent-> m_hobject;

// // Watch the C: / directory for file creation and deletion // dwChangeHandles [0] = FindFirstChangeNotification ( "C: //", // directory to watch FALSE, // do not watch the subtree FILE_NOTIFY_CHANGE_FILE_NAME);. / / watch file name changes // // Watch the D: / subtree for directory creation and deletion // dwChangeHandles [1] = FindFirstChangeNotification ( "D: //"., // directory to watch TRUE, // watch the subtree FILE_NOTIFY_CHANGE_DIR_NAME ); // Watch Dir. Name Changes

While (True) {/ * ------------------------------------------- ------------------------------ Examples: Here is The Examples if you want to us.. // Check if user want to Exit File Inspector. // Please make sure that the second parameter of // WaitForSingleObject is 0, and in this case the // first parameter must be manual event. // WaitForSingleObject will check the first parameter, // if it is signal, the loop will break. //iff (WaitForsingleObject (pevent-> m_hobject, 0) == Wait_Object_0) Return (uint) -1;

WaitforsingleObject (dwchangehandles [1], infinite); PostMessage (hwnd, wm_user_thread_file_changed, 0, 0); FindnextChangeNotification (dwchangehandles); ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- - * / // // Wait for notification. // dwwaitstatus = WaitFormultiPleObjects (3, dwchangehandles, false, infinite); if (dwwaitstatus == Wait_Object_0 2) Return (uint) -1;

switch (dwWaitStatus) {case WAIT_OBJECT_0: // // A file was created or deleted in C:. / WINDOWS // Refresh this directory and restart the // change notification RefreshDirectory is an // application-defined function // PostMessage.. (hWnd, WM_USER_THREAD_FILE_CHANGED, 0,0); FindNextChangeNotification (dwChangeHandles [0]); break; case WAIT_OBJECT_0 1:. // // A directory was created or deleted in C: / // Refresh the directory tree and restart the / / CHANGE NOTIFICATION. Refreshtree is an // application-defined function. //

PostMessage (hwnd, wm_user_thread_file_changed, 0, 0); FindnextChangeNotification (dwchangehandles [1]); Break;}}

DELETE [] DWChangeHandles; Return 0;

/ / -------------------------------------------------------------------------------------------- --------------------------------------- //// Function: long cmainwindow :: onfilechanged (WPARAM WPARAM, LPARAM LPARAM) // Purpose: // Comments: // --------------------------------- -------------------------------------------------- ----

Long cmainwindow :: onfilechanged (wparam wparam, lparam lparam) {// // add process senence if file change here. //

Return 0;}

// ######################################################################################################################################################################################################################################################################################################### ###############################################

转载请注明原文地址:https://www.9cbs.com/read-2808.html

New Post(0)