Let's talk about it first. Some people say yes.Source code, SourceCodeIn the blog article "VC ++'s spam cleanup software development 4:ProgramThe interface design and implementation-button control interface is provided at the end for you to download.
In the previous article, we described the Program Overview and requirement analysis. The initialization interface of the program is:
Interface Diagram
The following describes how to implement the Code. The following describes how to scan, display, and clean up junk files.
Function Analysis and Design: the spam cleanup function mainly includes file Traversal Scanning, displaying scanned files, and deleting and cleaning junk files. The user needs to scan files while the other side can clean up the junk files that have been scanned. File scanning usually takes a lot of time. To improve the reliability and efficiency of spam, we should use multi-threaded development technology to place the file scanning tasks in a separate thread.
(1) create a dialog box-based project named "cleartmpfile ".
(2) Add controls such as static text box, button, combo box, list box, and progress bar to the dialog box. The effect is as follows:
Control Layout
(3) Add a common primary data member to the ccleartmpfiledlg dialog box. For the functions of each member, see the comments section:
Clist <cstring, cstring> m_fileextlist; // Records need to find the temporary file extension Bool M_bthreadexit; // Indicates whether the thread exits. Bool M_bfinding; // Searching in progress Handle m_hthread;// Find the file thread handle Handle m_hthread2; // BMP rotating thread handle Cstring m_szcurdisk; // Disk to be searched Handle m_hevent; // Event object. The search will be completed before the dialog box is closed. Bool M_bcontinue; // Pause or continue button operation DWORD getdisksize ( Char * Strpath ); // Obtain disk capacity (used) DWORD m_dwdiskvol; // Total disk capacity, in KB DWORD m_dwscanedvol; // Size of scanned files DWORD m_dwscanedtmpfilevol; // Size of the temporary files scanned DWORD m_dwscanedtmpfilenum; // Size of the temporary files scanned
(4) Add the researchfile Method to the dialog box class to interpret the specified directory and display the specified spam file type in the scan result list.
Void Ccleartmpfiledlg: researchfile ( Char * Pszpath ){ Char Sztmp [max_path] = { 0 }; // Define a temporary character array Strcpy (sztmp, pszpath ); If (Sztmp [strlen (sztmp )- 1 ]! = ' \\ ' ) // End the directory in the form \ *. *. {Strcat (sztmp, " \\*.* " ); // Connection string } Else {Strcat (sztmp, " *.* " ); // Connection string } Win32_find_data finddata; // Define a file query data structure Memset (& finddata, 0 ,Sizeof (Win32_find_data); handle hfind = Findfirstfile (sztmp, & finddata ); // Start searching for files // Because the query is performed in the thread, the user can determine whether to exit the thread. If yes, the thread function if (m_bthreadexit) is terminated in advance) {Findclose (hfind ); // Close the search handle Setevent (m_hevent ); // Set the event to signal Return ;} If (Hfind! = Invalid_handle_value) // File Found { While (Findnextfile (hfind, & finddata) = true) // Find the next file { // Because the query is performed in the thread, the user can determine whether to exit the thread. If yes, the thread function is terminated in advance. If (M_bthreadexit) {findclose (hfind ); // Close the search handle Setevent (m_hevent ); // Set the event to signal Return ;} // If the file is not a directory If (! (Finddata. dwfileattributes & File_attribute_directory) {DWORD dwfilesize = (Finddata. nfilesizehigh * (maxdword + 1 ) + Finddata. nfilesizelow )/( 1024 ); // Obtains the file size, in KB. M_dwscanedvol + = dwfilesize; // Total size of scanned files, in KB // M_dwscanedvol = m_dwscanedvol/1024; // Unit conversion: MB // Set progress bar M_progressctl.setpos (m_dwscanedvol/ 1024 )); Char Szfilename [max_path] = { 0 }; // Define character arrays to store complete file names Strcpy (szfilename, pszpath );// Get the complete file name If (Szfilename [strlen (szfilename )- 1 ]! = ' \\ ' ) {Strcat (szfilename, " \\ " );} Strcat (szfilename ,( Char * ) Finddata. cfilename ); If (Istmpfile (szfilename )) // Determine whether szfilename is a temporary file {M_dwscanedtmpfilevol + = Dwfilesize; // The size of the temporary files scanned in KB. M_dwscanedtmpfilenum ++; // Cumulative number of temporary files scanned M_listboxresults.addstring (lpctstr) szfilename );}} Else // If the file is a directory, recursively traverse the directory { If (Strcmp (( Const Char *) & Finddata. cfilename, " ... " )! = 0 )&& (Strcmp (( Const Char *) & Finddata. cfilename, " .. " )! =0 )&& (Strcmp (( Const Char *) & Finddata. cfilename, " . " )! = 0 )){ Char Szfilename [max_path] = { 0 }; Strcpy (szfilename, pszpath ); // Get the complete file name If (Szfilename [strlen (szfilename )- 1 ]! = ' \\ ' ) {Strcat (szfilename, " \\ " );} Strcat (szfilename ,( Char * ) Finddata. cfilename ); // Because the query is performed in the thread, the user can determine whether to exit the thread. If yes, the thread function is terminated in advance. If (M_bthreadexit) {findclose (hfind ); // Close the search handle Setevent (m_hevent ); // Set the event to signal Return ;} Researchfile (szfilename ); // Recursive call }}} Findclose (hfind ); // Close file search handle }
(5) define a thread function to scan and find spam files separately:
DWORD _ stdcall findtmpfile (lpvoid lpparameter) {ccleartmpfiledlg * Pdlg = (ccleartmpfiledlg *) lpparameter; // Get thread parameters Waitforsingleobject (pdlg-> m_hevent, infinite ); // Wait for the event to signal Cstring dir = pdlg-> m_szcurdisk.getbuffer (); // Disk directory based on the current drive letter directory Char * S = (Lpstr) (lpctstr) dir; pdlg ->Researchfile (s); pdlg -> Restore (); pdlg -> Showresulttext (); // Displays the number and size of temporary files scanned // Restore data to the initial state Pdlg-> m_dwscanedtmpfilevol = 0 ; Pdlg -> M_dwscanedtmpfilenum = 0 ; Pdlg -> M_fileextlist.removeall (); Return 0 ;}
(6) process the Click Event of the "scan now" or "start" button and create a new thread to execute the file scanning task:
// If the search is not complete, you cannot start a new file search. Getdlgitem (idc_begin)-> Showwindow (sw_hide ); If (! M_bfinding & gettmpextname ()) // Get File Extension {Getdlgitem (idc_progress1) -> Showwindow (true ); // Display progress bar Getdlgitem (idc_list1)-> Showwindow (sw_show); m_bthreadexit = False; m_bfinding = True; m_combox.getwindowtext (m_szcurdisk ); // Get current drive letter // Initialize progress bar data M_dwdiskvol = getdisksize (lpstr) (lpctstr) m_szcurdisk ); // Obtain the capacity of the current disk (used) Cstring STR; Str. Format (_ T ( " % D " ), M_dwdiskvol /( 1024 )); Double Isize = Atoi (STR); m_progressctl.setrange32 ( 0 , M_dwdiskvol/ 1024 ); // Initialize the progress bar and set the range of the progress bar. The range is MB. If (M_hevent! = Null) {closehandle (m_hevent ); // Close event object M_hevent = NULL;} m_listboxresults.resetcontent (); // Clear the search result list M_hevent = createevent (null, false, true, _ T ( " Event " )); // Create event object // Create a thread and start executing the thread function. M_hthread = createthread (null, 0 , Findtmpfile, This , 0 , Null); m_hthread2 = Createthread (null, 0 , Rotatingimg,This , 0 , Null); updatedata (false );
(7) clean up the scanned spam file, that is, use the delete file policy, and use the deletefile () method:
Void Ccleartmpfiledlg: onbnclickeddelall (){ // Delete scanned junk files Cstring strdel; cfile file; For ( Int I = 0 ; I <m_listboxresults.getcount (); I ++) {M_listboxresults.gettext (I, strdel); getdlgitem (idc_test) -> Setwindowtext (strdel); deletefile (strdel ); // Delete a file in the specified path } M_listboxresults.resetcontent (); getdlgitem (idc_test) -> Setwindowtext (_ T ( " Cleared! " ); Getdlgitem (idc_list1) -> Showwindow (sw_hide); getdlgitem (idc_begin) -> Showwindow (sw_show); getdlgitem (idc_begin) -> Setwindowtext (_ T ( " Rescan " ));}
Now, almost all the main functions of the software have been developed. As you can see, the function is very simple, that is, scanning, displaying, and cleaning the specified file format. The following describes the design and development of the software interface.