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; // data structure contains the operating system version information OsVersionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); GetVersionEx (& OsVersionInfo); // Get OS version information if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {// Windows98, calls the ExitWindowsEx () Function restarts the computer DWORD DWRESERVED; EXITWINDOWSEX (EWX_REBOOT, DWRESERVED); / / You can change the first parameter, implement some processing programs before using the user, // shut down, turn off the power, etc. //
// 4, restart the computer
TypeDef Int (Callback * Shutdowndl) (INT); // Displays the pointer hinstance hinst = loading dialog function ("shell32.dll"); // Load shell32.dllshdowndlg shutdowndialog; // Point to the shell32.dll library display The pointer IF (hinst! = Null) {// get the address of the function and calls ShutDowndialog = (Shutdowndialog) (* shutdowndialog) (* shutdowndialog) (* shutdowndialog) (* shutdowndialog) (* shutdowndialog) (* shutdowndialog) (* shutdowndialog) (0);}
// 5, enumerate all fonts
LOGFONT lf; lf.lfCharSet = DEFAULT_CHARSET; // Initialize the LOGFONT structurestrcpy (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 (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 // eight, context menu event trigger event: onContextMenu event
// Nine, display and hide the program 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);} DESTROYICON (hic);
// Eleven, window automatic lean program demonstration
BOOL AdjustPos (CRect * lpRect) {// automatically aside 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, ISX, ISY);} el = lpRect-> left; int = = LPRECT-> TOP; IF (ix
// Twelve, add a menu item to the system menu to add a menu item to the system menu to add three steps: // First, use the Resource Symbols dialog (Select Resource Symbols in the View menu ... can be displayed //// This dialog is defined in the menu item ID, which should be greater than 0x0f and less than 0xF000; // Secondly, call the CWnd :: GetSystemMenu to get the pointer to the system menu and call CWnd :: Appendmenu to add the menu // item to the menu. The following example is added to the system menu two new 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_mysysIstem, strmenuite); // ...}
// 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 the bill
// 14, dynamic increase or delete menu
// 1, add menu // Add cmenu * mainmenu; mainmenu = AFXGETMAINWND () -> getMenu (); // Get the main menu (MainMenu-> GetSubMenu (0)) -> appendmenu (mf_separator); // Add Division. (mainmenu-> getSubmenu (0)) -> appendmenu (mf_string, id_app_about, _t ("always on & top")); // Add 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;}} // fifteenth, changing the application icon static changes:
/ / 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 WM_SETICON messages to the main window. The code is as follows: hicon hicon = AFXGetApp () -> loading (idi_icon); Assert (HICON); AFXGETMAINWND () -> SendMesetage (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 CString class variable.
// sevente, copying image data on the clipboard to copy image data below the code data to any application, // can be placed in a function of CVIEW derived class.
Cmetafiledc * m_pmetadc = new cmetafiled (); m_pmetadc-> createenhacesd (getdc (), null, null, "whatver"); // 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 clipboardOpenClipboard (); 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 sourceif (OpenClipboard ()) {HGLOBAL clipbuffer; char * buffer; EmptyClipboard (); clipbuffer = GlobalAlloc (GMEM_DDESHARE, source.GetLength () 1); buffer = (char *) GlobalLock ( Clipbuffer; STRCPY (Buffer, LPCSTR (Source)); GlobalUnlock (Clipbuff (CF_Text, Clipbuffer; CloseClipboard ();} // Get text from the splic panel:
Char * buffer; if (OpenClipboard ()) {buffer = (char *) getclipboardData (cf_text); // do sometying 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 window handles if (OpenClipboard ()) {EmptyClipboard (); SetClipboardData (CF_BITMAP, hBitmap); CloseClipboard ();} ShowWindow (SW_SHOW);} HBITMAP CShowBmpInDlgDlg :: CopyScreenToBitmap (LPRECT lpRect) {// lpRect representative of the selected region { HDC HSCRDC, HMEMDC; / / Screen and Memory Device Description Table HBitmap Hbitmap, Holdbitmap; // Bit 图 INT NX, NY, NX2, NY2; // Selection Area Coordinate INT NWIDTH, NHEIGHT; // Bitmap Width and Height INT XSCRN, YSCRN; // Screen resolution // Make sure the selected area is not empty rectangular IF (ISRECTEMPTY (LPRECT)) Return Null; / / Creating a device description table hscrdc = createdc ("Display", NULL, NULL, NULL); / / Creating a compatible memory device description table for the screen device description table HMEMDC = CR EateCompatiPLEDC (HSCRDC); // Get selected area coordinate nx = lprect-> left; Nx2 = lpRect-> Right; NY2 = LPRECT-> Right; NY2 = LPRECT-> BOTTOM; // Get screen resolution xscrn = getDevicecaps HSCRDC, Horzres); YSCRN = getDeviceCaps (HSCRDC, Vertres); // Make sure 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 Hbitmap (HSCRDC, NWIDTH, NHEIGHT), which is compatible with the screen device descriptor table.
// Put the new bitmap to the memory device descriptor 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; // Get the handle of the screen bitmap HBitmap = (HbitMap) SelectObject (HbitMDC, Holdbitmap); / / Clear DELETEDC (HSCRDC); deletedc (hmemdc); // Return bitmail Return Return HbitMap;}} // twenty, how to display bitmap scales in the Static control
// Display bitmap void cshowbmpindlgdlg :: showbmpinstaic () {cbitmap hbmpinstaic () {cbitmap hbmpinstaic () {cbitmap hbmpinstaic () {cbitmap hbitmap; // Point PSTATIC * Pstain; pstaic = (cstatic *) getdlgitem (IDC_IMAGE); // Load 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 the picture 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.wid, limited.top, limited.width (), LRECT.HEIGHT (), & DCMEM, 0, 0, BM. BMWIDTH, BM.BMHEIGHT, SRCCOPY; DCMem.selectObject (& Poldbitmap);