VC tips summary of 5 practical tips

  • 2020-04-02 02:31:00
  • OfStack

This paper collects and summarizes VC 5 tips, very practical, for VC programming has a good reference value, the details are as follows:

1. How to get the path of the program

That is, get the path where the program itself is located.
Define a variable CString m_exePath in the header file of the application class CxxApp. Add the following statement to the InitInstance() function of the application class CxxApp:


TCHAR m_Path[MAX_PATH];
GetModuleFileName( NULL, m_Path, MAX_PATH ); //Get program path (including program name)
int i = 0, j;
while( m_Path[i]!=0 )
{
  if( m_Path[i]=='' )
    j = i;
  i++;
}
m_Path[j+1] = '';
m_exePath.Format( "%s", m_Path );  //Separate path name (remove program name)

After the program is executed, the path of the program is placed in the string variable m_exePath, which does not include the program name.
What's the point of getting the location of the program?

(1) open the data file placed with the application:
If you have used the open file dialog box to open files under other paths in the process of running the program, then the default path of the system has been changed, may cause you to open the original data file, if the following methods can be no problem:


CFile file;
file.Open( m_exePath+" Data file name ", CFile::modeRead );

(2) temporary files in the running of the placement program:
Similarly, when the default path of the system is changed, the temporary files generated in the program will be everywhere and become a garbage file. The following methods can be used to make the temporary files only in the path of the program:


CFile file;
file.Open( m_exePath+" Temporary file name ", CFile::modeCreate | CFile::modeWrite );
 ... 

At the end of the program, delete the temporary file by:


CFile::Remove( m_exePath+" Temporary file name " );

2. How to execute other programs in your program

There are several ways to call other programs in your own program. Here are two I've used:
(1) WinExec() function:
General usage:


WinExec(m_PathName,SW_SHOWNORMAL);

M_PathName is the path name of the executable and must be an executable file.
Such as:


WinExec("C:Program FilesInternet Exploreriexplore.exe",SW_SHOWNORMAL);//To open Internet explorer

(2) ShellExecute() function:
General usage:


ShellExecute(NULL,NULL,m_PathName,NULL,_T("c:temp"),SW_SHOWNORMAL);

M_PathName is the pathname of the open program;
_T("c:\temp") is the working directory;
Unlike WinExec(), the ShellExecute() function also opens a non-executable file, such as.txt, which opens notepad and loads it. I call my own help file (.chm) very well in this way.

3. How do I save a file at the end of a program without serialization?

In the document-view structure, serializing auto-save files is covered in various VC books. The problem is that instead of serializing, I save it myself, and when I click the close button of the window, how do I prompt and save the document?
Use the ClassWizard to add the function CanCloseFrame() to the document class (CxxDoc) and add a statement to save the file.
Ex. :


//Exit the program
BOOL CEditDoc::CanCloseFrame(CFrameWnd* pFrame) 
{
  CFile file;
  if(b_Flag)  //B_Flag is the document modification flag, which is set to True when the document is modified
  {
    int t;
    t=::MessageBox(NULL," The text has changed. Do you want to save it? "," warning ",
      MB_YESNOCANCEL | MB_ICONWARNING);  //Prompt dialog box pops up
    if(t==0 || t==IDCANCEL)
      return false;
    if(t==IDYES)
    {
      CString sFilter="Text File(*.txt)|*.txt||";
      CFileDialog m_Dlg(FALSE,"txt",NULL,OFN_HIDEREADONLY |         OFN_OVERWRITEPROMPT,(LPCTSTR)sFilter,NULL); //Custom file dialog box
      int k=m_Dlg.DoModal(); //The file dialog box pops up
      if(k==IDCANCEL || k==0)
        return false;
      m_PathName=m_Dlg.GetPathName(); //Gets the selected file path name

      file.Open(m_PathName,CFile::modeCreate | CFile::modeWrite);
      file.Write(m_Text,m_TextLen); //Data write file
      file.Close();
    }
  }
  return CDocument::CanCloseFrame(pFrame);
}

So when you click the close button on the window, if the data has been modified, a dialog box will pop up that prompts you to save the data.
The b_Flag in the program is the data modification flag, which should be set when the data is modified, and m_Text is the data to be saved and placed in the document.

4. How do I use POSITION?

The POSITION type data, used to represent the positions of elements in various lists, is similar to, but different from, the index of an array. The main differences are:
We cannot access the values of the POSITION data, nor can we add, subtract, compare, and so on the POSITION data.
When using POSITION data to access the list, the iterative method is adopted. The general format is:


POSITION pos;    //Define pos type variables
pos = GetHeadPosition();  //Gets the starting element position of the list
while( pos )
{
  x = GetNext(pos);  //Gets the list value at the pos and modifies the pos to the next element position
}

GetNext() is an iteration in the form:


TYPE GetNext(POSITION& rPosition);

First, it returns the element at the current pos position; Another is to change the pos value to the next element position. In this loop, the values of each element in the list can be obtained in turn. When the end of the list is reached, the pos is NULL and the loop ends.
So when using POSITION data, instead of trying to modify it with operations such as addition and subtraction, you can only modify its value by iterating over and over with GetNext() (iterating backwards) or GetPrev() (iterating forwards).
If you want to go directly to the specified value, you can also use the Find() function or the FindIndex() function to get the POSITION value of the specified value.


POSITION Find(TYPE Value);//To find the POSITION Value of the element with Value in the list;
POSITION FindIndex(int nIndex);//To get the POSITION value of the nIndex element in the list, starting at 0.

Such as:


pos = FindIndex(5);  //Find the position of the fifth element in the list
x = GetNext(pos);   //Reads the value of the element

In a word, The POSITION type is provided in a variety of classes that involve lists, and different classes provide different functions, but the usage is similar.

5. How do I separate filename and pathname from the full file path?

Separate file names from paths:


CString GetFileName(CString pathname) 
{ 
  for( int i=pathname.GetLength()-1; i>=0; i-- ) 
  { 
    if( pathname[i]=='' ) 
      break; 
  } 
  return pathname.Mid( i+1 ); 
}

Separate path name from path (remove file name) :


CString GetPath(CString pathname) 
{ 
  int i = 0, j; 
  while( i<pathname.GetLength() ) 
  { 
    if( pathname[i]=='' ) 
      j = i; 
    i++; 
  } 
  return pathname.Left( j+1 ); 
}

Related articles: