VC program design tips 20 cases

  • 2020-04-02 02:24:17
  • OfStack

This paper summarizes the VC program design commonly used in 20 skill examples for your reference. Details are as follows:

1, open the cd-rom


mciSendString("Set cdAudio door open wait",NULL,0,NULL); 

2, turn off CD_ROM


mciSendString("Set cdAudio door closed wait",NULL,0,NULL); 

3. Turn off the computer


OSVERSIONINFO OsVersionInfo; //A data structure that contains OS version information
OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 
GetVersionEx(&OsVersionInfo); //Gets operating system version information
if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) 
{ 
  //Windows98, call the ExitWindowsEx() function to restart the computer
  DWORD dwReserved; 
  ExitWindowsEx(EWX_REBOOT,dwReserved); //The first parameter can be changed to achieve logout of the user,
  //Shutdown, power off and other operations
  //Some of the handlers before exiting
} 

Restart the computer


typedef int (CALLBACK *SHUTDOWNDLG)(int); //Displays a pointer to the shutdown dialog function
HINSTANCE hInst = LoadLibrary("shell32.dll"); //Loading shell32. DLL
SHUTDOWNDLG ShutDownDialog; // Point to the shell32.dll In the library Displays a pointer to the shutdown dialog function
if(hInst != NULL) 
{ 
  //Get the address of the function and call it
  ShutDownDialog = (SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60); 
  (*ShutDownDialog)(0); 
} 

Enumerate all fonts


LOGFONT lf; 
lf.lfCharSet = DEFAULT_CHARSET; // Initialize the LOGFONT structure 
strcpy(lf.lfFaceName,""); 
CClientDC dc (this); 
//Enumerate the font families 
::EnumFontFamiliesEx((HDC) dc,&lf, 
(FONTENUMPROC) EnumFontFamProc,(LPARAM) this,0); 
//The enumeration function
int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf,LPNEWTEXTMETRIC lpntm,DWORD nFontType,long lparam) 
{ 
  // Create a pointer to the dialog window 
  CDay7Dlg* pWnd = (CDay7Dlg*) lparam; 
  // add the font name to the list box 
  pWnd ->m_ctlFontList.AddString(lpelf ->elfLogFont.lfFaceName); 
  // Return 1 to continue font enumeration 
  return 1; 
} 

Where m_ctlFontList is a list control variable

6. Only run one instance of the program at a time


if( FindWindow(NULL," Program title ")) 
exit(0); 

7. Get the current mouse position


CPoint pt; 
GetCursorPos(&pt); //Get the position

8. Context menu event triggers event: OnContextMenu event

Show and hide program menus


CWnd *pWnd=AfxGetMainWnd(); 
if(b_m) //The hidden menu
{ 
  pWnd->SetMenu(NULL); 
  pWnd->DrawMenuBar(); 
  b_m=false; 
} 
else 
{ 
  CMenu menu; 
  menu.LoadMenu(IDR_MAINFRAME); ////Display menu can also change menu items
  pWnd->SetMenu(&menu); 
  pWnd->DrawMenuBar(); 
  b_m=true; 
  menu.Detach(); 
} 

10. Get the icon for the executable


HICON hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0); 
if (hIcon &&hIcon!=(HICON)-1) 
{ 
  pDC->DrawIcon(10,10,hIcon); 
} 
DestroyIcon(hIcon); 

11, automatic window side procedures demonstration


BOOL AdjustPos(CRect* lpRect) 
{ 
  //Automatic pull over
  int iSX=GetSystemMetrics(SM_CXFULLSCREEN); 
  int iSY=GetSystemMetrics(SM_CYFULLSCREEN); 
  RECT rWorkArea; 
  BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkArea, 0); 
  CRect rcWA; 
  if(!bResult) 
  { 
    //If the call is unsuccessful, use GetSystemMetrics to get the screen area
rcWA=CRect(0,0,iSX,iSY); 
  } 
  else 
rcWA=rWorkArea; 
  int iX=lpRect->left; 
  int iY=lpRect->top; 
  if(iX < rcWA.left + DETASTEP && iX!=rcWA.left) 
  { 
    //Adjust the left
//pWnd->SetWindowPos(NULL,rcWA.left,iY,0,0,SWP_NOSIZE); 
lpRect->OffsetRect(rcWA.left-iX,0); 
AdjustPos(lpRect); 
return TRUE; 
  } 
  if(iY < rcWA.top + DETASTEP && iY!=rcWA.top) 
  { 
//On the adjustment
//pWnd->SetWindowPos(NULL ,iX,rcWA.top,0,0,SWP_NOSIZE); 
lpRect->OffsetRect(0,rcWA.top-iY); 
AdjustPos(lpRect); 
return TRUE; 
  } 
  if(iX + lpRect->Width() > rcWA.right - DETASTEP && iX !=rcWA.right-lpRect->Width()) 
  { 
//Adjust the right
//pWnd->SetWindowPos(NULL ,rcWA.right-rcW.Width(),iY,0,0,SWP_NOSIZE); 
lpRect->OffsetRect(rcWA.right-lpRect->right,0); 
AdjustPos(lpRect); 
return TRUE; 
  } 
  if(iY + lpRect->Height() > rcWA.bottom - DETASTEP && iY !=rcWA.bottom-lpRect->Height()) 
  { 
//Adjust the
  //pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE); 
lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom); 
return TRUE; 
  } 
  return FALSE; 
} 
//The following procedure call is then used in the ONMOVEING event
CRect r=*pRect; 
AdjustPos(&r); 
*pRect=(RECT)r; 

12. Add a menu item to the system menu
Adding a menu item to the system menu requires the following three steps:
First, use the Resource Symbols dialog (select Resource Symbols from the View menu... Define the menu item ID, which should be greater than 0x0F and less than 0xF000;
Next, call CWnd::GetSystemMenu to get a pointer to the system menu and call CWnd:: Appendmenu to add a menu item to the menu. The following example adds two new menu items to the system menu.


int CMainFrame:: OnCreate (LPCREATESTRUCT lpCreateStruct) 
{ 
   ...  
  //Make sure system menu item is in the right range. 
  ASSERT(IDM_MYSYSITEM <0xF000); 
  //Get pointer to system menu. 
  CMenu* pSysMenu=GetSystemMenu(FALSE); 
  ASSERT_VALID(pSysMenu); 
  //Add a separator and our menu item to system menu. 
  CString StrMenuItem(_T ("New menu item")); 
  pSysMenu->AppendMenu(MF_SEPARATOR); 
  pSysMenu->AppendMenu(MF_STRING, IDM_MYSYSITEM, StrMenuItem); 
   ...  
} 

13. Run other programs


//Run EMAIL or web address
char szMailAddress[80]; 
strcpy(szMailAddress,"mailto:netvc@163.com"); 
ShellExecute(NULL, "open", szMailAddress, NULL, NULL, SW_SHOWNORMAL); 
//2. Run executable programs
WinExec("notepad.exe",SW_SHOW); //Operating schedule

Dynamically add or remove menus
(1) add menu


//add
CMenu *mainmenu; 
mainmenu=AfxGetMainWnd()->GetMenu(); //Get the main menu
(mainmenu->GetSubMenu (0))->AppendMenu (MF_SEPARATOR);//Add separator
(mainmenu->GetSubMenu (0))->AppendMenu(MF_STRING,ID_APP_ABOUT,_T("Always on &Top")); //Add a new menu item
DrawMenuBar(); //Redraw the menu

(2) delete the menu


//delete
CMenu *mainmenu; 
mainmenu=AfxGetMainWnd()->GetMenu(); //Get the main menu
CString str ; 
for(int i=(mainmenu->GetSubMenu (0))->GetMenuItemCount()-1;i>=0;i--) //Gets the number of items in the menu.
{ 
  (mainmenu->GetSubMenu (0))->GetMenuString(i,str,MF_BYPOSITION); 
  //Copies the label of the specified menu item into the specified buffer. MF_BYPOSITION is explained above.
  if(str=="Always on &Top") //Delete the menu item we just added.
  { 
(mainmenu->GetSubMenu (0))->DeleteMenu(i,MF_BYPOSITION); 
 break; 
  } 
} 

15. Test whether ALT key is pressed:


GetKeyState(VK_MENU); 
GetAlt(); 

16, check whether to press the left mouse button  


if((nFlags&MK_LBUTTON)==MK_LBUTTON) 


17. Check keyboard input
The argument nChar in OnKeyDown is a value that needs to be converted to a character when displayed, using the following command:


char lsChar; 
lsChar=char(nChar); 
if(lsChar=='A'); 
{ 
....... 
} 

18, call another function ::GetKeyState(), with a specific key code to determine whether the key is pressed. If the return value of the ::GetKeyState function is negative, the key is pressed. If the return value is non-negative, the message was not pressed. For example, to determine if the shift key is being pressed, use the following code:


if(::GetKeyState(VK_SHIFT) <O) 
{ 
  AfxMessageBox("shift is pressed"); 
} 

19. How to end an application at any time during programming (general)
1) need to send WM_CLOSE/WM_QUIT message to the window,
Calls the CWnd::OnClose member function and allows you to save modified data to a user prompt.


AfxGetMainWnd()->SendMessage(WM_CLOSE); //Don't forget to get a pointer to the current window first

2) using functions:


void PostQuitMessage( int nExitCode // exit code ); 

3) use of standard functions:


void exit( int status ); //Try not to use it in MFC

20. Get the screen size


HWND hWnd; 
CRect Rect; 
hWnd = ::GetDesktopWindow(); 
::GetClientRect(hWnd, &Rect); 

//--------------------------------------------------------- 

How to query and set system parameters
      The SDK function SystemParametersInfo, introduced in the Windows 3.1 SDK, is called to query and set system parameters, such as keystroke repeat rate Settings, double-click latency, icon fonts, and desktop overwrite bitmaps.  


//Create a font that is used for icon titles. 
LOGFONT stFont; :: SystemParametersInfo (SPIF_GETICONTITLELOGFONT,   sizeof (LOGFONT), &stFont, SPIF_SENDWININICHANGE); 
m_font.CreateFontIndirect (&stFont); //Change the wallpaper to leaves.bmp. 
:: SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, _T("forest.bmp"),SPIF_UPDATEINIFILE); 

How to use a predefined Windows cursor
  Call CWinApp:: LoadStandardCursor and pass the cursor identifier.


BOOL CSampleDialog:: OnSetCursor (CWnd* pWnd, UINT nHitTest, UINT message) {   //Display wait cursor if busy.   
if (m_bBusy)   {     
SetCursor (AfxGetApp () ->LoadStandardCursor (IDC_WAIT));     
return TRUE; 
   }   
return CDialog:: OnSetCursor (pWnd. nHitTest,message); 
 }

Related articles: