VC Programming Tips 20 Cases _c language

Source: Internet
Author: User

This article summarizes the VC programming commonly used 20 skill examples, for everybody's reference. Details are as follows:

1. Open CD-ROM

mciSendString ("Set cdaudio door open Wait", null,0,null); 

2. Close Cd_rom

mciSendString ("Set cdaudio door closed Wait", null,0,null); 

3, shut down the computer

osVersionInfo osversioninfo; The data structure containing the operating system version information 
osversioninfo.dwosversioninfosize = sizeof (OSVERSIONINFO); 
GetVersionEx (&osversioninfo); Get 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); You can change the first parameter to achieve the cancellation of users,// 
  shutdown, power off, and so on 
  //exit some of the handlers before 
} 

4, restart the computer

typedef int (CALLBACK *SHUTDOWNDLG) (int); Displays a pointer to the shutdown dialog function 
hinstance hinst = LoadLibrary ("Shell32.dll");//Mount Shell32.dll shutdowndlg shutdowndialog 
; Point to the Shell32.dll library to display the shutdown dialog function pointer 
if (hinst!= NULL) 
{ 
  //Get the address of the function and invoke the 
  Shutdowndialog = (SHUTDOWNDLG) GetProcAddress (Hinst, (LPSTR)); 
  (*shutdowndialog) (0); 
} 

5. 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); 
enum function 
int CALLBACK enumfontfamproc (lpenumlogfont lpelf,lpnewtextmetric lpntm,dword nfonttype,long) 
{ 
  //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. Run only one instance of the program at a time and exit if it is already running

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

7, get the current mouse location

CPoint pt; 
GetCursorPos (&PT); Get a position 

8. Context Menu Event Trigger event: OnContextMenu Event

9. Show and Hide Programs menu

CWnd *pwnd=afxgetmainwnd (); 
if (b_m)//Hide menu 
{ 
  pwnd->setmenu (NULL); 
  Pwnd->drawmenubar (); 
  B_m=false; 
} 
else 
{ 
  CMenu menu; 
  Menu. LoadMenu (IDR_MAINFRAME); Display menu can also change menu item 
  pwnd->setmenu (&menu); 
  Pwnd->drawmenubar (); 
  B_m=true; 
  Menu. Detach (); 
} 

10, get the icon of the executable file

Hicon Hicon=::extracticon (AfxGetInstanceHandle (), _t ("NotePad.exe"), 0); 
if (Hicon &&hicon!= (HICON)-1) 
{ 
  Pdc->drawicon (10,10,hicon); 
} 
DestroyIcon (HICON); 

11, the window automatically pull over program demo

BOOL Adjustpos (crect* lprect) {//automatically 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 obtain 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 left//pwnd->setwindowpos (null,rcwa.left,iy,0,0,s 
Wp_nosize); 
Lprect->offsetrect (rcwa.left-ix,0); 
Adjustpos (lprect); 
  return TRUE; } if (IY < Rcwa.top + detastep && iy!=rcwa.top) {//Adjusted on//pwnd->setwindowpos (NULL, IX,RCWA.TOP,0,0,SW 
P_nosize); 
Lprect->offsetrect (0,rcwa.top-iy); 
Adjustpos (lprect); 
  return TRUE; The IF (IX + lprect->width () > Rcwa.right-detastep && iX!=rcwa.right-lprect->width ()) {//Adjust right//p Wnd->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 ()) 
Pwnd->setwindowpos (NULL, Ix,rcwa.bottom-rcw.height (), 0,0,swp_nosize); 
Lprect->offsetrect (0,rcwa.bottom-lprect->bottom); 
  return TRUE; 
return FALSE; 
//Then use the following procedure in the Onmoveing event to invoke the CRect r=*prect; 
Adjustpos (&AMP;R); 

 *prect= (RECT) R;

12. Add a menu item to the System menus
Adding a menu item to the System menu requires the following three steps:
First, use the Resource Symbols dialog (select Resource Symbols in the View menu ...). The dialog can be displayed to define a menu item ID that is greater than 0x0f and less than 0xf000;
Second, call Cwnd::getsystemmenu to get a pointer to the system menu and invoke CWnd:: AppendMenu to add menu items to the menus. 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 URL 
char szmailaddress[80]; 
strcpy (szmailaddress, "mailto:netvc@163.com"); 
ShellExecute (NULL, "open", szmailaddress, NULL, NULL, SW_SHOWNORMAL); 
2, the operation of the executable program 
winexec ("notepad.exe", sw_show); 

14. Add or remove menus dynamically
(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 ();//Repaint menu 

(2), delete 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 to the specified buffer. Mf_byposition's explanation is on. 
  if (str== "Always on &top")//if it is the menu item we added just now, delete it. 
  { 
(mainmenu->getsubmenu (0))->deletemenu (i,mf_byposition); 
 break; 
  } 
} 

15, test the 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 parameter nchar in onkeydown is a numeric value that, when displayed, needs to be converted into characters, 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, indicating that the key was pressed. If the return value is non-negative, it indicates that the stay has not been pressed. For example, if you want to determine whether the SHIFT key is pressed, you can use the following code:

if (:: Getkeystate (Vk_shift) <o) 
{ 
  AfxMessageBox ("SHIFT is pressed"); 
} 

19. How to end the application at any time in the programming process (general)
1 need to send Wm_close/wm_quit message to the window,
Call the Cwnd::onclose member function and allow the user to be prompted whether to save the modified data.

AfxGetMainWnd ()->sendmessage (WM_CLOSE); Don't forget to get the pointer to the current window first. 

2) Using functions:

void PostQuitMessage (int nexitcode//exit code); 

3 Use standard functions:

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

20. Get the size of the screen

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

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

How to query and set system parameters
The SDK function SystemParametersInfo is described in the Windows 3.1 SDK, which can be invoked to query and set system parameters such as repeat rate settings for keystrokes, mouse double-click latency, icon fonts, desktop overlay bitmaps, and so on.

Create a font is used for icon titles. 
LogFont Stfont; :: SystemParametersInfo (Spif_geticontitlelogfont,   sizeof (LOGFONT), &stfont, spif_sendwininichange); 
M_font. CreateFontIndirect (&stfont); Change of 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 transfer 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); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.