Some common methods in VC (20)
// First, open the CD-ROM
McIndstring ("Set Cdaudio Door Open Wait", NULL, 0, NULL;
// Second, turn off CD_ROM
McIndstring ("Set Cdaudio Door Closed Wait", NULL, 0, NULL;
// Third, turn off the computer
OsversionInfo OsversionInfo; / / Contains the data structure of the operating system version information
OsversionInfo.dwosveionsInfosize = SizeOf (OsversionInfo);
GetversionEx (& OSVersionInfo); // Get operating system version information
IF (OsversionInfo.dwplatformID == Ver_Platform_WIN32_WINDOWS)
{
// windows98, call the exitwindowsex () function Restart the computer
DWORD DWRESERVED;
EXITWINDOWSEX (EWX_REBOOT, DWRESERVED); / / You can change the first parameter to realize the user,
// shut down, turn off the power, etc.
// some processing prior to exiting
}
// 4, restart the computer
TypeDef Int (Callback * ShutDowndl) (INT); // Show a pointer to the shutdown dialog function
Hinstance hinst = loadingLibrary ("shell32.dll"); // Load shell32.dll
Shutdowndlg shutdowndialog; // Pointer to display the shutdown dialog function in the shell32.dll library
IF (hinst! = null)
{
// Get the address of the function and call it
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);
// Enumerate 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 (lppelf-> elflogfont.lffacename);
// Return 1 to Continue Font Enumeration
Return 1;
}
// where m_ctlfontlist is a list control variable
// Sixth, only one program instance is run once, if it has been run, exit
IF (FindWindow (NULL, "Program Title")) EXIT (0);
// Seven, get the current mouse location
Cpoint pt;
GetCursorpos (& PT); // Get position
// 8, context menu event trigger event: onContextMenu event // Nine, display and hidden 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 the menu item
PWND-> SetMenu (& menu);
PWND-> DRAWMENUBAR ();
B_m = true;
Menu.detach ();
}
// 10, get the icon for executable
Hicon Hicon = :: Extracticon (AfxGetInstanceHandle (), _ T ("Notepad.exe"), 0)
IF (Hicon && HiCon! = (HICON) -1)
{
PDC-> Drawicon (10, 10, Hicon);
}
DESTROYICI (HICON);
// Eleven, window automatic lean program demonstration
BOOL AdjustPos (CRECT * LPRECT)
{// automatic side
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)
{// 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 {// adjust left //pwnd->etwindowpos (Null, Rcwa.left, iY ,0, 0 ,swp_nosize); LPRECT-> OffsetRect (rcwa.left-ix, 0); AdjustPos (LPRECT); Return True; } IF (IY {// 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 && = 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 ()) {// adjustment // 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 procedure call in the onMoveing event. CRECT R = * prect; AdjustPos (& R); * prect = (RECT) R; // Twelve, add a menu item to the system menu to add 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 ... / / Shows the conversation) Define the menu item ID, which should be greater than 0x0f and less than 0xF000; // Second, call CWnd :: getSystemMenu to get the pointer to the system menu and call CWnd :: appendmenu to put the menu // Add to the menu. The next example adds two new 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_mysysiSISISISISIM, STRMENUITEM); // ... } // 13, run other procedures // 1, run email or URL Char szmailaddress [80]; STRCPY (SzmailAddress, "Mailto: Netvc@21cn.com"); Shellexecute (Null, "Open", Szmailaddress, Null, NULL, SW_SHOWNORMAL); // 2, run the executable Winexec ("NOTEPAD.EXE", SW_SHOW); // Run // 14, dynamic increase or delete menu // 1, add menu //Add to Cmenu * mainmenu; Mainmenu = AFXGETMAINWND () -> getMenu (); // Get 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 (); // Heavy painting 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 -) // Number of items to get menu. { (Mainmenu-> GetSubmenu (0)) -> GetMenustring (i, str, mf_byposition); // Copy the label of the specified menu item to the specified buffer. Explanation of MF_BYPosition. IF (str == "always on & top") // If we have just increased menu items, then delete. { (Mainmenu-> GetSubmenu (0)) -> DeleteMenu (i, mf_byposition); Break; } } // 15, change the icon static changes in the application: / / Modify the icon resource IDR_MAINFRAME. It has two icons, one is 16 * 16, the other is 32 * 32, pay attention to modify together. // Dynamic changes: Send a WM_SETICON message to the main window. The code is as follows: HiCon Hicon = AFXGetApp () -> Loadicon (IDI_ICON); Assert (hic); AFXGETMAINWND () -> SendMessage (WM_SETICON, TRUE, (LPARAM) HICON; //6, another way to change the window title // Use statement cwnd * m_pcWnd = AFXGETMAINWND (), then call the setWindowText () function in the following form: SetWindowText (* m_pcwnd, (lpctstr) m_windowtext); // m_windowText can be a variable of a CString class. // sevente, copy image data by enhancement metafile by enhancement panel to any application, // It can be placed in a function of CVIEW derived class. Cmetafiledc * m_pmetadc = new cmetafiledc (); m_pmetadc-> createenhanced (getDC (), NULL, NULL, "Wherever"); // 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_pmetad; // Eighteen, the transfer of text data on the clipboard puts the text onto the splicing board: CSTRING SOURCE; // Put your text in Source IF (OpenClipboard ()) { Hglobal clipbuffer; Char * buffer; EMPTYCLIPBOARD (); Clipbuffer = GlobalAlloc (gmem_ddeshare, source.getLength () 1); Buffer = (char *) Globalock (Clipbuffer); STRCPY (Buffer, LPCSTR (Source); GlobalUnlock (Clipbuffer); SetClipBoardData (CF_Text, Clipbuffer); CloseClipboard (); } // Get text from the splic panel: 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 cutting version Void cshowbmpindlgdlg :: oncutscreen () { ShowWindow (SW_HIDE); Rect r_bmp = {0, 0, :: getSystemMetrics (SM_CXSCREEN), :: getSystemMetrics (SM_CYSCREEN)}; Hbitmap hbitmap; Hbitmap = COPYSCREENTOBITMAP (& r_BMP); // hwnd is the program window handle IF (OpenClipboard ()) { EMPTYCLIPBOARD (); SetClipboardData (cf_bitmap, hbitmap); CloseClipboard (); } ShowWindow; } Hbitmap cshowbmpindlgdlg :: CopyScreenTOBITMAP (LPRECT LPRECT) { // LPRECT represents the selected area { HDC HSCRDC, HMEMDC; // Screen and Memory Device Description Table Hbitmap hbitmap, holdbitmap; // bit map handle INT NX, NY, NX2, NY2; // Select region coordinates INT NWIDTH, NHEIGHT; // bitmap width and height Int xscrn, yscrn; // Screen Resolution / / Make sure the selected area is not empty rectangle IF (IsRectempty (LPRECT)) Return NULL; / / Create a device description table for the screen HSCRDC = CREATEDC ("Display", NULL, NULL, NULL; / / Create a compatible memory device description for the screen device description table HMEMDC = CREATECOMPATIBLEDC (HSCRDC); / / Get selected area coordinates Nx = lprect-> left; NY = lpRect-> TOP; NX2 = lpRect-> Right; NY2 = LPRECT-> BOTTOM; // Get the screen resolution Xscrn = getDeviceCaps (HSCRDC, Horzres); YSCRN = GetDeviceCaps (HSCRDC, Vertres); / / Ensure 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 screen device description table hbitmap = createcompatibleBitmap (HSCRDC, NWIDTH, NHEIGHT); / / Put the new bitmap in the memory device descriptor table Holdbitmap = (hbitmap) SelectObject (HMEMDC, HBitmap); / / Copy the screen device description table to the memory device descriptor table Bitblt (HMEMDC, 0, 0, NWIDTH, NHEIGHT, HSCRDC, NX, NY, SRCCOPY); / / Get the handle of the screen bitmap Hbitmap = (hbitmap) SelectObject (HMEMDC, Holdbitmap); // Clear Deletedc (HSCRDC); Deletedc (HMEMDC); // Return the bitmap section Return hbitmap; } } // Twenty, how to display bitmap scales in the Static control / / Display bitmap in the STAIC control Void cshowbmpindlgdlg :: showbmpinstaic () { CBITMAP HBMP; Hbitmap hbitmap; // Point PSTATIC to the place to display CSTATIC * PSTAIC; PSTAIC = (cstatic *) getdlgitem (IDC_Image); // Loading the resource mm.bmp is my file name, with your replacement Hbitmap = (Hbitmap) :: loadimage (:: afxgetinstancehandle (), "mm.bmp", Image_bitmap, 0,0, lr_loadfromfile | lr_createdibsection; HBMP.Attach (HBitmap); / / Get 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 (limited.width (), LRECT.WIDTH (), LRECT.HEIGHT (), & DCMEM, 0, 0, BM.BMWIDTH, BM.BMHEIGHT, SRCCOPY; DcMem.selectObject (& Poldbitmap); }