Visual C ++ 6.0 programming experience

zhaozj2021-02-11  206

Another way to change the window title ---- There is a article called "Visual C 4.0 Programming Experience" on September 1, 1997, which mentioned a change window title. The method, that is, the value of the member variable m_strtitle of the CFrameWnd class is modified in the member function of the application's Frame CMAINFRAME class (Note: it is a derived class of the CFrameWnd class). The lack of this method is that only the contents of the window title can only be modified in the process of running. For example, when you need to display the mouse before the window is displayed on the window title, we have to use the full-program Bool SetWindowText (HWND HWND, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTR LPSTRING) for Visual C . This function is actually a Win32 function. Its first parameter requires a handle of a window frame, and the second parameter requires a 32-bit pointer that points to a constant string, ie the LPCTSTR type variable. Normally, we tend to dynamically change the contents of the window title in the application's document class or view class, so that there is no existing handle of the window framework for us, so we also need to call before using the setWindowText () function. Another Win32 function AFXGETMAINWND () to get a pointer to the framework class of the application, such as the use of statements

---- CWND * m_pcWnd = AfxgetMainWnd ()

---- If you are calling a setWindowText () function in the following form:

---- setWindowText (* m_pcwnd, (lpctstr) m_windowtext); // m_windowtext can be a variable of a CString class

---- How to put more than 256 colors of bitmaps into resources into the application program

---- Peers who have used Visual C for application development know that the bitmap editor comes with Visual C 5.0 cannot browse and edit the bitmap of 256-color, and the resource file is not allowed The bitmap of embedding (IMPORT) 256 color is used as a resource (otherwise, the application is reported when the application is running). This feature allows us to use Visual C to use the program to increase the beauty of the interface to increase the performance of the interface drawing, in the case of the Using Visual C to open the neutral core, use the Visual Basic to open the interface part. This question is improved in Visual C 5.0. First, the bitmap editor can create and edit 256 colors of bitmaps. In addition, Visual C 5.0 allows programmers to embed 256-color bitmaps into resources, although it is still unable to browse in Visual C bitmap editors, and also requires Win32 Release as compiled applications to generate executable applications. . Another other restriction condition is a bitmap on 256 colors of the resource cannot be automatically opened and closed by the application programming kernel. For example, in the article named "Visual C 4.0 Programming Experience", a method of adding a bitmogram button for a dialog is mentioned in the article, that is, the program developer creates four bitmaps for each button. , Respectively, the bounce status (Up), Press State (Down), Input Focus Status (FOCUS), and Disable, and must be one of the above four states The combination is identified by a bitmap so that the application can automatically find the corresponding resource (ie, bitmap) when the application is drawn bitmap button. However, this automatic map is limited to a bitmap that can be opened by the Visual C bitmap editor. Because of this, the bitmap of 256 colors is selected as the source of the bitmap button, and also hope that the above-mentioned phase mutual exchange is reached, the following function and program design reference model is required.

Set up four constants, button_up, button_down, button_focus, button_disable, which is used to identify the forward state of each button. In the corresponding dialog box class of the application, for each bitmap button (which is convenient below, it is possible to assume two) to set a CRECT class object, m_rect1, and m_rect2 to record the coordinates occupied by each button in the dialog box. rectangle. Then set an integer variable, buton1_status, button2_status, record the current state of each button is set in this dialog box class. The objects and integer variables of these CRECT classes are initially initialized in the structural function of the dialog box. In this dialogue class, create a mess-in handling function in the mouse, such as onMousemove (), ONLButton (), ONLBUTTONDOWN (), etc. Press the Ctrl and W key or directly click the ClassWizard button on the toolbar to open the ClassWizard dialog box. Select the dialog class class in the Class Name list box. After selecting the class name of this class in the Object IDS list box, select the WM_PAINT message in the message (Messages) list box and double-click it, then ClassWizard will be A onpaint () function is added to the dialog class. This function will play an important role in our programs. After that, select the identification name of the newly added toolbar button in the Object IDS list box, double-click the Command message in the Message list box, and the ClassWizard will add a corresponding message processing function in the dialog class. Close the ClassWizard dialog box. ---- The sub-program code is given below, for reference only.

Void ctestdlg :: onpaint ()

{

CWND * PWND;

CDC * PDC;

CDC * pdisplaymemdc;

CBITMAP * PBITMAP;

PWnd = getdlgitem (idc_image1); // Get pointers pointing to the first bitmap button

PDC = PWND-> getDC (); // Get a window device for drawing

PWND-> invalidate (); // makes the window to update it

PWND-> UpdateWindow ();

PDISPLAYMEMDC = New CDC;

Pbitmap = new cbitmap;

Pdisplaymemdc-> CreateCompatiPLEDC (PDC);

if (change1) {// Description The status of the first button has changed

Switch (button1_status) {

Case Button_Disable:

Pbitmap-> loadbitmap (image1_disable); // Load bitmap

Pdisplaymemdc-> SelectObject (Pbitmap);

PDC-> Bitblt (0, 0, 140, 30, PDisPlaymemdc, 0,0, srcopy);

// copy the bitmap to the designated area

Break;

Case Button_up:

Pbitmap-> loadbitmap (image1_up); pdisplaymemdc-> selectObject (pbitmap);

PDC-> Bitblt (0, 0, 140, 30, PDisPlaymemdc, 0,0, srcopy);

Break;

Case Button_focus:

Pbitmap-> loadingbitmap (image1_focus);

Pdisplaymemdc-> SelectObject (Pbitmap);

PDC-> Bitblt (0, 0, 140, 30, PDisPlaymemdc, 0,0, srcopy);

Break;

Case Button_Down:

Pbitmap-> loadbitmap (image1_down);

Pdisplaymemdc-> SelectObject (Pbitmap);

PDC-> Bitblt (0, 0, 140, 30, PDisPlaymemdc, 0,0, srcopy);

Break;

}

}

DELETE PDISPLAYMEMDC;

Delete pbitmap;

PWnd = getdlgitem (IDC_IMAGE2);

PDC = PWND-> getdc ();

PWND-> invalidate ();

PWND-> UpdateWindow ();

PDISPLAYMEMDC = New CDC;

Pbitmap = new cbitmap;

Pdisplaymemdc-> CreateCompatiPLEDC (PDC);

if (change2) {// Description The state of the second button changes

Switch (button2_status) {

Case Button_Disable:

Pbitmap-> loadbitmap (image2_disable);

Pdisplaymemdc-> SelectObject (Pbitmap);

PDC-> Bitblt (0, 0, 140, 30, PDisPlaymemdc, 0,0, srcopy);

Break;

Case Button_up:

Pbitmap-> loadbitmap (image2_up);

Pdisplaymemdc-> SelectObject (Pbitmap);

PDC-> Bitblt (0, 0, 140, 30, PDisPlaymemdc, 0,0, srcopy);

Break;

Case Button_focus:

Pbitmap-> loadingbitmap (image2_focus);

Pdisplaymemdc-> SelectObject (Pbitmap);

PDC-> Bitblt (0, 0, 140, 30, PDisPlaymemdc, 0,0, srcopy);

Break;

Case Button_Down:

Pbitmap-> loadbitmap (image2_down);

Pdisplaymemdc-> SelectObject (Pbitmap);

PDC-> Bitblt (0, 0, 140, 30, PDisPlaymemdc, 0,0, srcopy);

Break;

}

}

DELETE PDISPLAYMEMDC;

Delete pbitmap;

CDIALOG :: onpaint ();

}

Void ctestdlg :: onmousemove (uint nflags, cpoint point)

{

CRECT RECT = CRECT (0, 0, 1, 1);

CRGN RGN1, RGN2; // Record the rectangular area occupied by each graph button

Rgn1.createRectrGnIndirect (m_rect1);

// RGN1 Record the rectangular area occupied by the first bitmap button

IF (Rgn1.ptinRegion (POINT))

{// Whether the mouse is currently incorporated into the rectangular area occupied by the first bitmap button

IF (button_status! = button_focus) && (Button1_Status! = Button_Disable)) {

// If the current state of the bitmap button is not the input focus status and is not a prohibition state

Button1_status = button_focus;

Change1 = True;

INVALIDATERECT (Rect, False);

}

IF ((Button2_status! = Button_up) &&

(Button2_status! = Button_disable)) {

Button2_status = button_up;

Change2 = true;

INVALIDATERECT (Rect, False);

}

}

Else {

Rgn2.createRectrGnIndirect (m_rect2);

IF (Rgn2.ptinRegion (Point) {

IF ((Button2_Status! = Button_focus)

&& (Button2_Status! = Button_Disable)) {

Button2_status = button_focus;

Change2 = true;

INVALIDATERECT (Rect, False);

}

IF ((Button1_Status! = Button_up)

&& (Button1_Status! = Button_Disable)) {

Button1_status = button_up;

Change1 = True;

INVALIDATERECT (Rect, False);

}

}

}

CDIALOG :: OnMousemove (NFLAGS, POINT);

}

Void ctestdlg :: ONLBUTTONUP (UINT NFLAGS, CPOINT)

{

CRECT RECT = CRECT (0, 0, 1, 1);

CRGN RGN1, RGN2;

Rgn1.createRectrGnIndirect (m_rect1);

IF (Rgn1.ptinRegion (Point) {

IF ((Button1_Status! = Button_up)

&& (Button1_Status! = Button_Disable)) {

Button1_status = button_up;

Change1 = True;

INVALIDATERECT (Rect, False);

}

}

Else {

Rgn2.createRectrGnIndirect (m_rect2);

IF (Rgn2.ptinRegion (Point) {

IF ((Button2_Status! = Button_up)

&& (Button2_Status! = Button_Disable)) {

Button2_status = button_up;

Change2 = true;

INVALIDATERECT (Rect, False);

}

}

}

CDIALOG :: ONLBUTTONUP (NFLAGS, POINT);

}

Void ctestdlg :: ONLBUTTONDOWN (uint nflags, cpoint point)

{

CRECT RECT = CRECT (0, 0, 1, 1);

CRGN RGN1, RGN2;

Rgn1.createRectrGnIndirect (m_rect1);

IF (Rgn1.ptinRegion (Point) {

IF ((Button1_Status! = Button_Down)

&& (Button1_Status! = Button_Disable)) {Button1_Status = Button_Down;

Change1 = True;

INVALIDATERECT (Rect, False);

}

IF ((Button2_Status! = Button_up)

&& (Button2_Status! = Button_Disable)) {

Button2_status = button_up;

Change2 = true;

INVALIDATERECT (Rect, False);

}

}

Else {

Rgn2.createRectrGnIndirect (m_rect2);

IF (Rgn2.ptinRegion (Point) {

IF ((Button2_status! = Button_Down)

&& (Button2_Status! = Button_Disable)) {

Button2_status = button_down;

Change2 = true;

INVALIDATERECT (Rect, False);

}

IF ((Button1_Status! = Button_up)

&& (Button1_Status! = Button_Disable)) {

Button1_status = button_up;

Change1 = True;

INVALIDATERECT (Rect, False);

}

}

}

CDIALOG :: ONLBUTTONDOWN (NFLAGS, POINT);

}

How to do not load menu, toolbar and status bar in an application ---- In applications (SDI and MDI) generated by AppWizard, the MFC class library has loaded on menu (including one System menu), toolbar and status bar. But there is a special need for special needs, we can hope that in your own application, do not load the menu, tools and shape strips. At this time, we need to manually delete and modify the statement in some classes.

Open the mainfrm.cpp file, find the oncreate () function drop-down list box with the function on the toolbar. The statement of the creating tool strip and the state strip is released according to the following program.

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)

{

IF (cframewnd :: oncreate (lpcreatestruct) == -1)

Return -1;

/ / Start adding symbols here

/ * if (! m_wndtoolbar.create (this) ||

! m_wndtoolbar.loadtoolbar (idR_mainframe))

{

Trace0 ("Failed to Create Toolbar / N);

Return -1; // fail to create

}

IF (! m_wndstatusbar.create (this) ||

M_WndStatusBar.setindicators (Indicators,

SIZEOF (INDICATORS) / SIZEOF (UINT))))

{

Trace0 ("Failed to Create Status Bar / N);

Return -1; // fail to create

}

// Todo: Remove this if you don't want Tips or a resizeable toolbarm_wndtoolbar.setbarstyle (m_wndtoolbar.getbarstyle ()

| CBRS_ToolTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

// Todo: Delete these Three Lines if you don't wantbar to be Dockable

m_wndtoolbar.enabledocking (CBRS_ALIGN_ANY);

Enabledocking (CBRS_ALIGN_ANY);

DockControlbar (& M_WndToolbar);

At the end of the comment * /

Return 0;

}

In the mainfrm.cpp document, use the function drop-down list box on the toolbar to find the PrecreateWindow () function. Referring to the number of steps in the third part, add a specific window frame attribute, and set the cs.style to the following form, that is, does not load the system menu. // Create A Window WITHUT Min / Max Buttons, System Menu, or Sizable Border Cs.Style = WS_OVERLAPPED | WS_BORDER; in the primary application, ie, in a .cpp file with definition theApp full variable, use the function drop-down list on the toolbar Find the initInstance () function. In the PDOCTEMPLATE = New CSINGEDOCTEMPLATE, use NULL to replace IDR_MAINFRAME. As shown in the following steps.

Bool cyourmainapp :: initInstance ()

{

// ..... This is slightly part of the statement.

CSINGLEDOCTEMPLATE * PDOCTEMPLATE;

PDOCTEMPLATE = New CSINGLEDOCTEMPLATE

Null, // idr_mainframe,

// Replace IDR_MAINFRAME with NULL

Runtime_class (cnobardoc),

Runtime_Class (CMAINFRAME),

// main SDI Frame WinDow

Runtime_class (cnobarview));

AddDDOCTemplate (pdoctemplate);

// ..... This is slightly part of the statement.

}

Locate the complained method of the Visual C editor to pull the list box, select Win32 Release, and generate the Release version.

---- At this point, we have to have the application of no menu, tools and shape strips.

A method of implementing the process of implementation of the dialogue ---- When you use a Windows95 to copy the file or remove the operation, you will have a schedule for flying documentation. This feature is not only allowed to take the operation when it can be taken during the operation, and the document is copied or deleted. It is real, and when you use the Visual C to use program order, we can also use the following method to add a self-adaptation process to the dialog box. Create a dialog box class for each operating process. In order to describe the following, we only have to set the application program requires a manager to indicate the dialog box and "cmodel" as a name of the corresponding dialog box class. The resource editor to edit the resource editor with Visual C , if you add some text, please In the header file of the CMODEL class, add two member variables, cwnd * m_pparent; // Point to the framework class (or dialog class) that calls the prompt dialog, that is, its "parent class" INT M_NID ; 记 Record the ID number of the prompt dialog and the following two member functions: cmodel (cwnd * pparent = null; // discard the original constructor, or modify the original function into this structure of this non-mode dialog Function BOOL CREATE (); // This function will call the Create () function creation dialog that creates a base class in the model.cpp file, add the implementation of the corresponding function.

Cmodel :: cmodel (cwnd * pparent / * = null * /)

: Cdialog (cdialog (cmodel :: IDD, pParent)

{

m_pparent = pParent;

m_nid = cmodel :: IDD;

// {{AFX_DATA_INIT (CMODEL)

// Note: The classwizard will add member initialization here

//}} AFX_DATA_INIT

}

Bool cmodel :: Create ()

{

Return CDIALOG :: Create (m_nid, m_pparent);

}

Press the Ctrl and W key or directly click the ClassWizard button on the toolbar to open the ClassWizard dialog box. Select this prompt dialog class in the Class Name list box. After selecting the class name of the class in the Object IDS list box, select PostncDestroy message in the message (Messages) list box and double-click it, at this time ClassWizard A postncdestroy () function will be added to the dialog class. This function will be called by the onncdestroy () function after the dialog window is lost. Therefore, some sweeping work can be added to the function, such as data transfer, release the needle space, and the like. Void cmodel :: postncdestroy ()

{

// Todo: Add Your Specialized Code Here and / or Call The Base Class

DELETE THIS;

CDIALOG :: Postncdestroy ();

}

In the header file of the classes to call the category of the prompt dialog class, first contain the header file of the CMODEL class, then declare a pointer to the object of the CMODEL class, such as m_dlg, and join in the constructor of this class "M_dlg = null;". Then, add the following sequence in the function of the on-the-time and closure of the report to the words:

if (m_dlg == null) {// If there is no prompt dialog in the event, create one

m_dlg = new cmodel (this);

m_dlg-> create ();

Getdlgitem (IDC_EXPORT) -> EnableWindow (FALSE);

}

Else / / Otherwise activate it

m_dlg-> setActiveWindow ();

Also, add the following statement to the place where the prompt dialog is to be closed.

m_dlg-> destroywindow ();

m_dlg = null;

---- At this point, you have already had your own process to make a dialog box. However, it still does not have a function of moving and taking the work. You mission try to add these functions. In addition, the author also tried to use the method of the method described below to implement the scriptures. Two kinds of methods are more than that, it can be described as a thousand autumn. If you hope that the process of the process of the previous design is enough to be shared by multiple applications, it is best to use the indication of the file as an independent process. However, when you also hope that if you issue data between the dialog box and the adjuvant, it seems to be more simple and more effective.

Realization of applications is now calling for its application ---- In our application program, it can be used to use them to complete a certain function. For example, when we compress and decompress many files in order to facilitate data transmission, one method is our own design of such compression / decompression programs, and then in the form of dynamic link library (DLL) or function library Application is called. But more and high-efficiency work is to use the existing Side of Existing Software, such as Arj.exe, and call it in the form of a process, and close it when it is appropriate. The next step will be described as an example, and the latter method is implemented. In the case where you need to call Arj.exe to compress the shrink / decompression, create a member function, you don't want to createbat (), which is generated into a batch process. Arj.exe is called by this batch process and gives a specific compression / decompression parameter. After that, use the MS-DOS DIR command to generate a temporary file to be used as a sign of compression / decompression shrinkage.

Void CMycompress :: Createbat (CString Batpath, Cstring Arjpath,

CString Batname, CString ArjfileName,

CSTRING TEMPPATH, CSTRING EXITFLAG, BOOL OUT

{

LPTSTR LPBUFFER;

Uint usize;

Handle hheap;

USIZE = (GetCurrentDirectory (0, NULL)) * Sizeof (tchar);

HHEAP = getProcessheap ();

LPBuffer = (lpstr) Heapalloc (hheap, heap_zero_memory, usize);

GetCurrentDirectory (USIZE, LPBUFFER);

// I know the current directory information in order to change the directory as needed.

IF (lpbuffer! = batpath) // Diferent Dir

SetCurrentDirectory (BATPATH);

Cstdiofile f;

CfileException E;

IF (! f.open (Batname, CFile :: Modecreate | CFILE :: Modewrite, & E))

/ / Create a batch file with BatName

{

AfxMessageBox ("You can't create files" batname);

Return;

}

Char Density [6];

Sprintf (Density, "% D", mtotalbytes;

---- // MtotalBytes is a variable set by other functions, used to record the maximum available space for the disk for copying or copying the file.

CSTRING DENSITY = DENSITY

CString string;

If (out) // Description is a batch file that generates compression work

String = "arj a -v" denity;

ELSE // Description is a batch file that generates the decompression work

String = "Arj E -V" Density; String = "..//"4arjpath "//"4arjfilename ";

IF (OUT)

String = string "..//" TEMPPATH "//*.* -y -jm / n";

Else

String = string "..//" TEMPPATH "// -y -jm / n";

f.writeString (String);

String = "DIR>" EXITFLAG "/ N";

f.writeString (String);

f.close ();

SetCurrentDirectory (lpbuffer); // Reply to the original directory

}

---- After the function is executed, a batch file will be generated, the content is roughly: ---- ARJ A -V1440 Compressed file path name file name is compressed path name file name -y -jm ---- Dir> Temporary file name ---- or: ---- ARJ E -V1440 The path name of the decompressed file file name decomposers the path name file name - Y-JM - - DIR> Temporary file name

In a class that needs to call Arj.exe for compression / decompression, create a member function, it may be called Runbat (), which is a batch file that creates and executes the process to run the batch file generated above, and undo at the appropriate time process.

Void CMYCompress :: runbat (cstring

BATPATH, CSTRING FILENAME, CSTRING EXITFLAG

{

Cstring lpapplicationname = batPath "//" filename;

// The full path name of the application program is executed

Startupinfo startupinfo; // Creating the information required for the process

GetStartupInfo (& Startupinfo);

STARTUPINFO.LPRESERVED = NULL;

STARTUPINFO.LPDESKTOP = NULL;

Startupinfo.lptitle = NULL;

STARTUPINFO.DWX = 0;

STARTUPINFO.DWY = 0;

Startupinfo.dwxsize = 200;

STARTUPINFO.DWYSIZE = 300;

STARTUPINFO.DWXCOUNTCHARS = 500;

STARTUPINFO.DWYCOUNTCHARS = 500;

STARTUPINFO.DWFLAGS = startf_useshowwindow;

Startupinfo.wshowwindow = sw_hide;

// The process will be implemented in the background with a hidden approach

Startupinfo.cbreserved2 = 0;

STARTUPINFO.LPRESERVED2 = NULL;

STARTUPINFO.HSTDINPUT = stdin;

STARTUPINFO.HSTDOUTPUT = stdout; startupinfo.hstderror = stderr;

LPTSTR LPBUFFER;

Uint usize;

Handle hheap;

USIZE = (GetCurrentDirectory (0, NULL)) * Sizeof (tchar);

HHEAP = getProcessheap ();

LPBuffer = (lpstr) Heapalloc (hheap, heap_zero_memory, usize);

GetCurrentDirectory (USIZE, LPBUFFER);

// Receive the current directory information, so that you need to change your directimate

IF (lpbuffer! = batpath) // Diferent Dir

SetCurrentDirectory (BATPATH);

// Creating a process

IF (CreateProcess (LPApplicationName, Null, NULL,

NULL, FALSE, CREATE_DEFAULT_ERROR_MODE,

NULL, NULL, & Startupinfo, & Pro_INFO))

{

MSG Message;

Deletefile (EXITFLAG);

Settimer (1,100, null); // Setting timer

Search = True;

WHILE (SEARCH) {

IF (: PeekMessage (& Message, NULL, 0, 0, PM_REMOVE)) {

:: TranslateMessage (& Message);

:: DispatchMessage (& Message);

}

}

// Processing work before the end of the process

DWORDEXITCODE;

IF (! getExitcodeProcess (Pro_info.hprocess, & exitcode))

AfxMessageBox ("GetExitcodeProcess IS Failed!");

if (! TerminateProcess (Pro_info.hprocess, (uint) EXITCODE))

// Final process

AFXMESSAGEBOX ("TerminateProcess IS Failed!");

IF (! CLOSEHANDLE (Pro_info.hprocess))

/ / Release the handle of the ended process

AfxMessageBox ("Closehandle Is Failed!");

KillTimer (1); // Removal

}

Else AfxMessageBox ("Process is not create!";

SetCurrentDirectory (lpbuffer); // Reupfaction to the original directory

}

Press the Ctrl and W key or directly click the ClassWizard button on the toolbar to open the ClassWizard dialog box. In the Class Name list box, select the class that you want to call Arj.exe for compression / decompression / decompression. After selecting the class name of the class in the Object IDS list box, select the WM_TIMER message in the message (Messages) list box and Double-click it, then ClassWizard adds an ONTIMER () function in this class. The function will check the compression / decompression zone in a set time interval whether the compression / decompression zoom has been completed, ie the time to check if the document is displayed, and then modified "Search", so that the Runbat () function end is notified. Void CMYCompress :: ONTIMER (Uint Nidevent)

{

// Todo: add your message handler code here and / or call default

CFILE FILE;

CfileException Error;

IF (file.open (exitflag, cfile :: ModeRead, & error) {

Search = false;

File.Close ();

}

}

Self-editing Directors and Subjects Functions ---- High -rely MS-DOS and Windows 95 provide a command that can delete one or more directories and its subordinate files and directory, the deletetree command. However, there is no corresponding function with the MFC class library or in the Win32 library. Thus, when we need to use the DELETETREE function in its own design, the naturally thoughtful way is to call MD-DOS or Windows 95 by process call or system call (as described above). DELETREE command. However, the Win32 function library has been provided for us to use a function of files and directory operations, which makes it difficult to set up the deletetree () function.

---- Reader read this, and I will feel some doubts. For the sixth part of the strong transfer process is superior to the functions of our design, and this part has changed again? Yes, in the case of normal conditions, the function of calling the application program is more flexible than the use of the process or the external function and can improve the efficiency of the execution rate. So, like deletetree (), using existing functions, it is not difficult to realize, and it is best to complete through the square function. However, the workload of the function is designed to be compressed / unzipped is not more cost-effective than the cost of using the current product. Because it requires at least we understand the compressed / decompressed complex algorithm, and debug and maintain it. Need a certain price. So, this time, or use "Take the Large" as well. ---- Next, give the deletetree () function of myself, for reference only.

Bool deleTree (CString Dirname)

{// Success: Return true; otherwise, return false

Bool result;

Result = preremovedirectory (DIRNAME)

&& removedirectory (DIRNAME);

Return Result;

}

Bool preremovedirectory (CString Dirname)

{// Success: Return true; otherwise, return false

LPTSTR LPBUFFER;

Uint usize;

CSTRING FILENAME;

Handle hheap;

Bool result;

Handle hfindfile;

Win32_find_data findfiledata;

USIZE = (GetCurrentDirectory (0, NULL)) * Sizeof (tchar);

HHEAP = getProcessheap ();

LPBuffer = (lpstr) Heapalloc (hheap, heap_zero_memory, usize);

GetCurrentDirectory (USIZE, LPBUFFER);

IF (lpbuffer! = DIRNAME) {// Adjust the current directory

SetCurrentDirectory (DIRNAME);

}

Hfindfile = findfirstfile ("*. *", & findfiledata);

CSTRING TFILE;

IF (hfindfile! = invalid_handle_value) {

Do {

Tfile = findfiledata.cfilename;

IF ((tfile == ") || (tfile =="))) Continue;

IF (FindFileData.dwFileAttributes ==

File_attribute_directory) {

IF (dirname [dirname.getlength () - 1]! = '//')

PreremoveDirectory (DIRNAME '//' TFile); ELSE

PreremoveDirectory; DirName Tfile;

IF (! RemoveDirectory (TFile))

Result = false;

Else

Result = TRUE;

}

Else

IF (! deletefile (tfile) Result = false;

Else Result = true;

}

While (FindNextFile (Hfindfile, & FindFileData);

FindClose (HFindFile);

}

Else {

SetCurrentDirectory (lpbuffer);

Return False;

}

SetCurrentDirectory (lpbuffer); // Reply to the original directory

Return Result;

}

How to get and modify the information of each drive ---- When the application related to the design and file input / output, we are likely to understand the information of the source driver or the target drive before entering / output files, such as whether There is a disk in the floppy drive, it has been opened to write protection, and the capacity of the existing disk. Regretty, there is no class in the MFC library to support these functions, and we can only finish our requirements in the functions of Win32. Below, I am practicable according to my own programming, through several steps, I will use the function of the function of Win32 to realize the operation of the driver. The reader can insert the function of the introduction of the program to be inserted by self-denial, and then inserted into the application.

The function of the lower program is that there is a driven in the search meter, select the driver number of the soft disk drive, and add it into a drop-down list box.

Void FindriverInfo ()

{

CCOMBOBOX * DRIVER = (ccomboBox *) getdlgitem (IDC_DRIVER);

DWORD DWNUMBYTESFORDRIVESTRINGS;

Handle hheap;

LPSTR LP;

Cstring strlogdrive;

INT nnumdrives = 0, NDrivenum;

DwnumBytesfordriveStrings = getLogicalDriveStrings (0, null)

* sizeof (tchar); // actually store the string length of the drive letter

IF (DwnumBytesfordriveStrings! = 0) {

HHEAP = getProcessheap ();

LP = (lpstr) Heapalloc (hheap, heap_zero_memory,

DwnumBytesfordriveStrings); //

GetLogicalDriveStrings (Heapsize (HHEAP, 0, LP), LP);

StringBox.setsize (dwnumbytesfordriveStrings / sizeof (tchar) 1);

While (* lp! = 0) {

IF (GetDriveType (LP) == Drive_removable) {

Driver-> addstring (lp); stringbox [nnumdrives] = lp;

NNUMDRIVES ;

}

LP = _TCSCHR (LP, 0) 1;

}

}

Else AfxMessageBox ("Can't Use The Function GetLogicalDriveStrings!");

}

The EMPTYDISKSPACE () function of the next introduction is responsible for clearing the magnetic disk in the empty specifier, and it is also responsible for recording the capacity of the magnetic disk in the specified drive, and obtain the serial number of the disk. In this function, the preremovedirectory () function mentioned in Seventh part is also called to complete the empty work.

BOOL EmptyDiskSpace (CSTRING DRIVER)

{

BOOL RESULT = True;

DWordsectorsperCluster; // address of researchors per cluster

DWordBytespersector; // address of bytes per seler

DWordNumberoffreeClusters; // address of number of free clusters

DWORDTOTAALNUMBEROFCLUSTERS;

DWORDTOTALBYTES;

DWORDFREEBYTES;

Int bcontinue = 1;

Char DiskvolumeSerialNumber [30];

// Store the serial number of the current disk in the drive

LPCTSTRLPROOTPATHNAME;

// Address of root directory of the file system

LPTSTRLPVOLUMENAMEBUFFER = New Char [12];

// address of name of the Volume

DWordnvolumenameSize = 12;

// Length of LPVoluMenameBuffer

DWORD VOLUMESERIALNUMBER;

// Address of Volume Serial Number

DWORD MAXIMUMComponentLength;

// Address of System's Maximum FileName Length

DWORD FILESYSTEMFLAGS;

// Address of File System Flags

LPTSTRLPFILESYSTEMNAMEBUFFER = New Char [10];

// Address of Name of File System

DWordnFileSystemNameSize = 10;

// length of lpfilesystemnamebuffer

LPROOTPATHNAME = Driver;

While (1) {

IF (GetDiskFreespace (Driver, & SectorsperCluster,

& Bytespersector, & NumberoffreeClusters,

& Totalnumberofclusters)))

{// with disk in the drive

Totalbytes = SECTORSPERCLUSTER * BYTESPERSECTOR

* TotalNumberOfClusters; // Disk total capacity

FreeBytes = SectorsperCluster * bytespector

* NumberoffreeClusters; // Disk free space capacity

GetVolumeInformation (LPRootpathname, LPVoluMenameBuffer, NVolumeNameSize,

& VolumeSerialnumber,

& Maximumcomponentlength,

& FileSystemFlags,

LPFileSystemNameBuffer, NFileSystemNameSize;

Sprintf (DiskVolumeSerialNumber, "% X", VoluMserialNumber;

// Get the serial number of the current disk in the drive

SetMtotalBytes (TotalBytes / 1024); // Stores the capacity of the disk in the specified drive

If (totalbytes! = freebytes) {// When the total disk capacity is not equal to the idle space capacity,

Clear operation should be performed

While (bcontinue) {

IF ((bcontinue == 2) || (MessageBox

("The disk in the drive" m_driver "has data.

/ n Do you want to let the system delete them? "

"Question", MB_YESNO | MB_ICONQUESTION == iDyes))

If (! preremovedirectory (driver)) // Unable to perform empty operation

IF ("" due to some reason system unable to delete

Data on the disk in the drive " m_driver ".

/ n Check if the disk does not turn off write protection.

/ n You are willing to try again? "

"Question", MB_YESNO | MB_ICONERROR) == iDYES) {

BCONTINUE = 2;

CONTINUE;

}

Else {

BCONTINUE = 0;

Result = false;

}

Else {

MessageBox ("successfully deletes data on disk!",

"Tips", MB_ok | MB_ICONInformation);

BCONTINUE = 0;

Result = TRUE;

}

Else {// the first if's else

BCONTINUE = 0;

Result = false;

}

}

}

Else Result = true;

Break;

}

Else {

IF ("No disk in the drive" m_driver ".

/ N Do you want to insert a disk again? "

"Question", MB_YESNO | MB_ICONASTERISK == iDYES) Continue;

Else Break;

}

} // end of while

Return Result;

}

In MS-DOS and Windows 95, the magnetic disk scrubber is constructed by 11 characters, and the large lowercase of the letter does not add to the division. When you need to set a volume of the magnetic disk in the specified drive, you only want to call the Win32's setvolumelabel () function, and in the first parameter, the driven number in the magnetic disk is indicated in the second parameter, refers to the new volume mark. For example, SetVolumelabel (Drivernum, NewvolumeLabel).

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

New Post(0)