20 common methods in VC
1. Open CD-ROM
Mcisendstring ("set cdaudio door open wait", null, 0, null );
Ii. Disable cd_rom
Mcisendstring ("set cdaudio door closed wait", null, 0, null );
3. Disable the computer
Osversioninfo; // data structure containing the operating system version information
Osversioninfo. dwosversioninfosize = sizeof (osversioninfo );
Getversionex (& osversioninfo); // get the operating system version
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 unregister a user,
// Shutdown, power off, and other operations
// Some processing programs before exiting
}
4. restart the computer
Typedef int (callback * shutdowndlg) (INT); // display the pointer of the function in the shutdown dialog box.
Hinstance hinst = loadlibrary ("shell32.dll"); // load shell32.dll
Shutdowndlg shutdowndialog; // pointer to the function in the shutdown dialog box displayed in the shell32.dll Library
If (hinst! = NULL)
{
// Obtain and call the function address
Shutdowndialog = (shutdowndlg) getprocaddress (hinst, (lpstr) 60 );
(* 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 );
// 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;
}
// M_ctlfontlist is a list control variable.
6. Run only one program instance at a time. Exit if it is already running.
If (findwindow (null, "program title") Exit (0 );
7. Get the current mouse position
Cpoint pt;
Getcursorpos (& pt); // obtain the location
8. Context Menu event triggering event
Oncontextmenu event
9. Show and hide program menus
Cwnd * pwnd = afxgetmainwnd ();
If (B _m) // hide the menu
{
Pwnd-> setmenu (null );
Pwnd-> drawmenubar ();
B _m = false;
}
Else
{
Cmenu menu;
Menu. loadmenu (idr_mainframe); // you can change the menu items when the menu is displayed.
Pwnd-> setmenu (& menu );
Pwnd-> drawmenubar ();
B _m = true;
Menu. Detach ();
}
10. Obtain the executable file icon
Hicon =: extracticon (AfxGetInstanceHandle (), _ T ("notepad.exe"), 0 );
If (hicon & hicon! = (Hicon)-1)
{
PDC-> drawicon (10, 10, hicon );
}
Destroyicon (hicon );
11. Demonstration of window auto-bypass Program
Bool adjustpos (crect * lprect)
{// Auto backend
Int ISX = getsystemmetrics (sm_cxfullscreen );
Int ISY = getsystemmetrics (sm_cyfullscreen );
Rect rworkarea;
Bool bresult = systemparametersinfo (spi_getworkarea, sizeof (rect), & rworkare
A, 0 );
Crect rcwa;
If (! Bresult)
{// Use getsystemmetrics to obtain the screen area if the call fails.
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, swp_nosize );
Lprect-> offsetrect (rcwa. Left-IX, 0 );
Adjustpos (lprect );
Return true;
}
If (Iy <rcwa. Top + detastep & Iy! = Rcwa. Top)
{// Adjust
// Pwnd-> setwindowpos (null, IX, rcwa. Top, 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 right
// Pwnd-> setwindowpos (null, rcwa. right-rcW.Width (), Iy, 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
// Pwnd-> setwindowpos (null, IX, rcwa. bottom-rcW.Height (), swp_nosize );
Lprect-> offsetrect (0, rcwa. Bottom-lprect-> bottom );
Return true;
}
Return false;
}
// Then use the called process in the onmoveing event
Crect r = * prect;
Adjustpos (& R );
* Prect = (rect) R;
12. Add a menu item to the System Menu
To add a menu item to the system menu, perform the following three steps:
First, use the resource symbols Dialog (select resource symbols... in the View menu to display the dialog) to define the menu item ID, which should be greater than 0x0f and less than 0xf000;
Next, call cwnd: getsystemmenu to obtain the pointer of the System menu and call cwnd: appendmenu to add the menu item to the menu. In the following example, add two new
Int cmainframe: oncreate (maid)
{
//...
// 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
1. Run email or website
Char szmailaddress [80];
Strcpy (szmailaddress, "mailto: netvc@21cn.com ");
ShellExecute (null, "open", szmailaddress, null, null, sw_shownormal );
2. Run the executable program
Winexec ("notepad.exe", sw_show); // run the event plan
14. add or delete menus dynamically
1. Add a menu
Add
Cmenu * mainmenu;
Mainmenu = afxgetmainwnd ()-> getmenu (); // obtain the main menu
(Mainmenu-> getsubmenu (0)-> appendmenu (mf_separator); // Add a 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 a menu
Delete
Cmenu * mainmenu;
Mainmenu = afxgetmainwnd ()-> getmenu (); // obtain the main menu
Cstring STR;
For (INT I = (mainmenu-> getsubmenu (0)-> getmenuitemcount ()-1; I> = 0; I --) // obtain the number of menu items.
{
(Mainmenu-> getsubmenu (0)-> getmenustring (I, STR, mf_byposition );
// Copy the label of the specified menu item to the specified buffer. The description of mf_byposition can be found in.
If (STR = "Always On & Top") // if it is the menu item we just added, delete it.
{
(Mainmenu-> getsubmenu (0)-> deletemenu (I, mf_byposition );
Break;
}
}
15. Change the static changes of the application icon:
Modify the icon resource idr_mainframe. It has two icons, one of which is 16*16 and the other is 32*32. Be sure to modify them together.
Dynamic Change: Send the wm_seticon message to the main window. The Code is as follows:
Hicon = afxgetapp ()-> loadicon (idi_icon );
Assert (hicon );
Afxgetmainwnd ()-> sendmessage (wm_seticon, true, (lparam) hicon );
16. Another method for changing the window title
Use the cwnd * m_pcwnd = afxgetmainwnd () statement, and then call the setwindowtext () function in the following form:
Setwindowtext (* m_pcwnd, (lpctstr) m_windowtext); // m_windowtext can be a cstring variable.
17. Copy image data through Enhanced Meta files on the clipboard
The following code copies image data to any application through a Metafile,
// It can be placed in the function of the cview derived class.
Cmetafiledc * m_pmetadc = new cmetafiledc ();
M_pmetadc-> createenhanced (getdc (), null, null, "whatever ");
// Draw Meta File
// Do what ever you want to do: bitmaps, lines, text...
// Close Meta File DC and prepare for clipboard;
Henhmetafile hmf = m_pmetadc-> closeenhanced ();
// Copy to clipboard
Openclipboard ();
Emptyclipboard ();
: Setclipboarddata (cf_enhmetafile, hmf );
Closeclipboard ();
// Deletemetafile (hmf );
Delete m_pmetadc;
18. Text data on the clipboard is transmitted to the clipboard:
Cstring source;
// Put your text in Source
If (openclipboard ())
{
Hglobal clipbuffer;
Char * buffer;
Emptyclipboard ();
Clipbuffer = globalalloc (gmem_ddeshare, source. getlength () + 1 );
Buffer = (char *) globallock (clipbuffer );
Strcpy (buffer, lpcstr (source ));
Globalunlock (clipbuffer );
Setclipboarddata (cf_text, clipbuffer );
Closeclipboard ();
}
// Obtain text from the Splicing board:
Char * buffer;
If (openclipboard ())
{
Buffer = (char *) getclipboarddata (cf_text );
// Do something with buffer here
// Before it goes out of scope
}
Closeclipboard ();
19. Capture the screen image to the cut version
Void cshowbmp indlgdlg: oncutscreen ()
{
Showwindow (sw_hide );
Rect r_bmp = {0, 0,: getsystemmetrics (sm_cxscreen ),
: Getsystemmetrics (sm_cyscreen )};
Hbitmap;
Hbitmap = copyscreentobitmap (& r_bmp );
// Hwnd is the program window handle
If (openclipboard ())
{
Emptyclipboard ();
Setclipboarddata (cf_bitmap, hbitmap );
Closeclipboard ();
}
Showwindow (sw_show );
}
Hbitmap cshowbmp indlgdlg: copyscreentobitmap (lprect)
{
// Lprect indicates the selected area
{
HDC hscrdc, hmemdc;
// Screen and memory device description table
Hbitmap, holdbitmap;
// Bitmap handle
Int NX, NY, nx2, ny2;
// Coordinates of the selected region
Int nwidth, nheight;
// Bitmap width and height
Int xscrn, yscrn;
// Screen resolution
// Make sure that the selected area is not empty.
If (isrectempty (lprect ))
Return NULL;
// Create a device description table for the screen
Hscrdc = createdc ("display", null );
// Create a compatible memory device description table for the screen device description table
Hmemdc = createcompatibledc (hscrdc );
// Obtain the coordinates of the selected region
Nx = lprect-> left;
NY = lprect-> top;
Nx2 = lprect-> right;
Ny2 = lprect-> bottom;
// Obtain the screen resolution
Xscrn = getdevicecaps (hscrdc, horzres );
Yscrn = getdevicecaps (hscrdc, vertres );
// Make sure that the selected area is visible
If (nx <0)
Nx = 0;
If (NY <0)
NY = 0;
If (nx2> xscrn)
Nx2 = xscrn;
If (ny2> yscrn)
Ny2 = yscrn;
Nwidth = nx2-NX;
Nheight = ny2-NY;
// Create a bitmap compatible with the on-screen device description table
Hbitmap = createcompatiblebitmap
(Hscrdc, nwidth, nheight );
// Select the new bitmap to the memory device description table.
Holdbitmap = (hbitmap) SelectObject (hmemdc, hbitmap );
// Copy the screen device description table to the memory device description table.
Bitblt (hmemdc, 0, 0, nwidth, nheight, hscrdc, NX, NY, srccopy );
// Obtain the handle of the screen bitmap.
Hbitmap = (hbitmap) SelectObject (hmemdc, holdbitmap );
// Clear
Deletedc (hscrdc );
Deletedc (hmemdc );
// Returns the bitmap handle.
Return hbitmap;
}
}
20. How to scale a bitmap to a static control
// Display the bitmap in the Staic Control
Void cshowbmp indlgdlg: showbmp instaic ()
{
Cbitmap hbmp;
Hbitmap;
// Point pstatic to the position to be displayed
Cstatic * pstaic;
Pstaic = (cstatic *) getdlgitem (idc_image );
// Load the resource mm.bmp is a file name of mine. Replace it with yours.
Hbitmap = (hbitmap): LoadImage (: AfxGetInstanceHandle (), "mm.bmp ",
Image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection );
Hbmp. Attach (hbitmap );
// Obtain the image format
Bitmap bm;
Hbmp. getbitmap (& BM );
CDC dcmem;
Dcmem. createcompatibledc (getdc ());
Cbitmap * poldbitmap = (cbitmap *) dcmem. SelectObject (hbmp );
Crect lrect;
Pstaic-> getclientrect (& lrect );
// Display bitmap
Pstaic-> getdc ()-> stretchblt (lrect. Left, lrect. Top, lrect. Width (), lrect. Height (),
& Dcmem, 0, 0, BM. bmwidth, BM. bmheight, srccopy );
Dcmem. SelectObject (& poldbitmap );
}