Some common methods in the VC (20) 2003-11-3 Add to www.9cbs.net Author Name 0 reviews Click 5383 Times, open CD-ROM MCISENDSTRING ("Set CDAudio Door Open Wait", NULL, 0, NULL); Second, close CD_ROM McISendString ("Set CDAUDIO DOOR CLOSED WAIT", NULL, 0, NULL); three, turn off computer osversioninfo osversioninfo; // contains data structure of operating system version information OsversionInfo.dwosveionFoSize = SizeOf (OsversionInfo); GetversionEx (& OSVersionInfo); // Get operating system version information if (osversioninfo.dwplatformid == ver_platform_win32_windows) {// windows98, call the exitWindowSex () function Restart your 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 * Shutdowndlg (int); // Show the pointer of the shutdown dialog function Hinstance Hinst = loadLibrary ("shell32.dll"); // Load shell32.dll shutdowndlg shutdowndialog; // Point to the Short32.dll library to display the shutdown dialog function Pointer IF (Hinst! = NULL) {// Gets the address of the function and calls shutdowndialog = (shutdowndlg) getProcaddress (HinST, (LPSTR) 60);
(* ShutDowndialog) (0);} 5 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;} where m_ctlfontlist is a list control variable six, only run a program instance at a time, if you have run, exit if (FindWindow (NULL, "Program Title")) EXIT (0); Seven, get the current mouse The location cpoint pt; getCursorpos (& PT); // Get position eight, context menu event trigger event: OnContextMenu event Nine, display and hidden program menus 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 (AFXGETITISTANCEHANDE (), _ T ("Notepad.exe"), 0); if (Hicon && Hicon! = (Hic) -1) {PDC-> Drawicon (10, 10, hic);
} DestroyIcon (hIcon); XI, automatic window aside program demonstrates 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);} else rcwa = RWORKAREA; int ix = lprect-> left; int = lpRect-> TOP;
IF (IX
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 requires the following three steps: First, use the Resource Symbols conversation (Select Resource Symbols ... You can display the dialog in the View menu) Define the menu item ID, which should be greater than 0x0f and less than 0xF000; secondly, call CWnd :: getSystemMenu to get the pointer to the system menu and call CWnd :: appendmenu to put the menu Item Add to the menu. The following example adds two new int CMAINFRAME :: oncreate (lpcreatestruct lpcreatestruct) {... // make su consisting to add two new int CMAINFRAME :: on ... // make suret 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); ...} thirteen, 1 // run other programs, run, or URL EMAIL 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 count, Dynamic Add or Remove 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 -) // Take the number of items in the 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;} fifteen, changing the icon static changes to the application: Modify the icon resource IDR_MAINFRAME. It has two icons, one is 16 * 16, the other is 3
2 * 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 () -> SendMessage (wm_seticon, true, (lparam) Hicon; 16 Another method of changing the window title uses statement CWnd * m_pcWnd = AFXGETMAINWND (), then call the setWindowTe XT () function in the following form: setWindowText (* m_pcwnd, (lpctstr) m_windowtext); // m_windowtext can be a CST Ring class variable. 17. Copying image data by enhancement metafile by enhancement panel to any application by enhanced meta file copying image data to any application, which 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 clipboard OpenClipboard (); EmptyClipboard (); :: SetClipboardData (CF_ENHMETAFILE, hMF); CloseClipboard ();
// deletemetafile (HMF); DELETE M_PMETADC; Eighteen, the transfer of text data on the clipboard places text on 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 *) GlobalLock (clipbuffer); strcpy (buffer, LPCSTR (source)); GlobalUnlock (clipbuffer); SetClipboardData ( CF_Text, Clipbuffer; CloseClipboard ();} Get text from the clipboard:
Char * buffer; if (OpenClipboard ()) {buffer = (char *) getClipboardData (cf_text); // do something with buffer here // before it goes out of scope} closeclipboard (); 19, will capture screen images to cut 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 window handle if (OpenClipboard ()) {EmptyClipboard (); SetClipboardData (CF_BITMAP, hBitmap); CloseClipboard ();} ShowWindow (SW_SHOW);} HBITMAP CShowBmpInDlgDlg :: CopyScreenToBitmap (LPRECT lpRect) {// lpRect representatives Selected Area {HDC HSCRDC, HMEMDC; // Screen and Memory Device Description Table Hbitmap Hbitmap, HoldbitMap; // Bit INT NX, NY, NX2, NY2; // Select area coordinate int nwidth, nHEight; // Figure width and height int XSCRN, YSCRN; / / screen resolution
/ / Make sure the selected area is not empty rectangular IF (ISRECTEMPTY (LPRECT)) Return Null; / / Create a device description table hscrdc = createdc ("Display", NULL, NULL, NULL); / / for screen device description table Create a compatible memory device description table hmemdc = createcompatibleDC (HSCRDC); // Get selected area coordinate nx = lpRect-> left; Nx2 = 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 = createcompatibleBitmap (HSCRDC, NWIDTH, NHEIGHT); // select the new bitmap = (hbitmap) SelectObject (HMEMDC, HBitMap) in the memory device descriptor table; // Put the screen device Description Table Copy to Memory Device Description Table Bitblt (HMEMDC, 0, 0, NWIDTH, NHEIGHT, HSCRDC, NX, NY, SRCCOPY); // Gets the handle of the screen bitmap HBitmap = (HbitMap) SelectObject (HMEMDC, Holdbitmap); / / Clear deletedc (HSCRDC); deletedc (hmemdc); // Return bitmaphe Return HbitMap;}} 20. How to display bitmap scales in the Static control // Display bitmap void cshowbmpindlgdlg from STAIC control :: Showbmpinstaic () {cbitmap hbmp; hbitmap hbitmap; // Point PSTATIC to the place to display CSTATIC * PSTAIC; PSTAIC = (cstatic *) getdlgitem (IDC_IMAGE); // Loading resource mm.bmp is my file name, with you Alternative hbitmap = (hbitmap) :: loadimage (:: AfxGetInstanceHandle (), "mm.bmp", image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection;
HBMP.Attach (hbitmap); // Get picture format Bitmap BM; HBmp.GetBitmap (& B); CDC DCMEM; DCMem.createCompatibleDC (GETDC ()); cbitmap * PoldbitMap = (cbitmap *) DcMem.selectObject (HBMP); CRECT LRECT; PSTAIC-> getClientRect (& LRECT); // Display bitmap pstaic-> getdc () -> strets, limited.width (), LRECT.HEIGH T (), LRECT.HEIGH T (), & DCMEM, 0, 0, BM.BMWIDTH, BM.BMHEIGHT, SRCCOPY; DCMem.selectObject (& Poldbitmap);