Some common methods in the VC

xiaoxiao2021-03-05  26

Some common methods / one, open CD-ROMMCISENDSTRING ("Set CDAUDIO DOOR Open Wait", NULL, 0, NULL); // Second, close CD_ROMMCISENDSTRING ("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, call the exitWindowSex () function Restart the computer DWORD DWRESERVED; EXITWINDOWSEX (EWX_REBOOT, DWRESERVED); / / You can change the first parameter, implement the user, // shut down, turn off the power, etc. Operation // Some processing programs} // 4, restart the computer typef int (int); // Display the pointer of the shutdown dialog function Hinstance Hinst = LoadLibrary ("shell32.dll"); // Load shell32.dllshdowndlg Shutdowndialog ; // point to the pointer IF (Hinst! = NULL) {// of the shutdown dialog function in the shell32.dll library to get the address of the function and call ShutDowndialog = (ShutDowndlg) GetProcadDress (HinST, (LPSTR) 60); (* Shutdowndialog) (0);} // 5 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 the dialog window CDay7Dlg * pWnd = (CDay7Dlg *) lparam to; // 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, only one program instance is run once, if you have run, exit if (FindWindow (NULL, "Program Title")) EXIT (0); // Seven, get the current The mouse is in the position 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 (AfxGetInstanceHandle (), _ T ("NOTEPAD.EXE"), 0); if (Hicon && Hicon! = (Hicon) - 1) {PDC-> Drawicon (10, 10, hicon);} DESTROYICON (HICON); // Eleven, window automatic lean program Demo BOOL AdjustPos (create * lprect) {// Auto Belt 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 not successful on the screen using the acquired GetSystemMetrics Area RCWA = CRECT (0, 0, ISX, ISY);} el = lprect-> left; int = lprect-> = = lprect-> top; if (ix setWindo WPOS (NULL, RCWA.LEFT, IY, 0, 0, SWP_NOSIZE); LPRECT-> OffsetRect (rcwa.left-ix, 0); AdjustPos (LPRECT); Return True;} IF (IY 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 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 ()) {// Adjust // pwnd-> setWindowPos (NULL, IX, RCWA.BOTTOM-RCW.HEIGHT (), 0, 0, SWP_NOSize); LPRECT-> OffsetRect (0, rcwa.bottom-lprect- > Bottom); Return true;}} // Then use the procedure in the onmoveing ​​event to call CRECT R = * prect; AdjustPos (& R); * prect = (RECT) R; // twelve, to the system Menu Add a menu item to add a menu item to the system menu requires the following three steps: // First, use the Resource Symbols dialog (Select Resource Symbols ... You can display / update this conversation) Define the menu item. ID, the ID should be greater than 0x0f and less than 0xF000; // Second, 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_mysysiSITEM, StrMenuItem); // ...} // thirteen, run other programs // 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 count // fourteen, dynamic Add 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 a new menu item DrawMenuBar (); // Heavy painting menu // 2, delete menu // delete cmenu * mainmenu; mainmenu = AFXGETMAINWND () -> getMenu (); // Get main menu CString Str; for (int i = (mainmenu-> GetSubmenu (0)) -> getMenuItemCount () - 1; i> = 0; i - // obtain 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 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 method // use statement cWnd * m_pcWnd = AFXGETMAINWND (), then call setWindowText () functions in the following form: setWindowText (* m_pcwnd, (lpctstr) m_windowText); // m_windowText can be a variable of a CString class. // 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_pmetc; // Eighteen 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 splic panel: char * buffer; if (OpenClipboard ()) {buffer = (char *) getClipboardData (cf_text); // Do Something with buffer He Re // before it goes out of scope} closeclipboard (); // 19, will capture screen images to clipping 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;} hbitmap cshowbmpindlgdlg :: CopyScreenTOBITMAP (LPRECT LPRECT) {// LPRECT represents the selected area {HDC HSCRDC, HMEMDC;

// Screen and Memory Device Description Table HBitmap Hbitmap, Holdbitmap; // Bitmap Handle INT NX, NY, NX2, NY2; // Selected Area Coordinate INT NWIDTH, NHEIGHT; // Bitmap Width and Height INT XSCRN, YSCRN; // Screen resolution // Make sure the selected area is not a empty rectangular IF (ISRECTEMPTY (LPRECT)) Return Null; / / Create a device description table hscrdc = Createdc ("Display", NULL, NULL, NULL); // To create a compatible memory device description table hmemdc = createcompatibledc (hscrdc); // Get selected area coordinate nx = lpRect-> left; ny = lprect-> top; 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 screen device description table compatible Bitmap hbitmap = createcompatiblebitmap (HSCRDC, NWIDTH, NHEIGHT); // Put the new bitmap in 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; // Get the handle of the screen bitmap HBitmap = (Hbitmap) SelectObject (HbitMDC, Holdbitmap); // Clear Deletedc (HSCRDC); deletedc (HMEMDC) ; // Return the bitmap of Return HbitMap;}} // twenty, how to display bitmap scales in the Static control // Display bitmap void cshowbmpindlgdlg :: showbmpinstaic () {cbitmap hbmp; hbitmap; // Point PSTATIC to the place to display CSTATIC * PSTAIC; PSTAIC = (cstatic *) getdlgitem (IDC_IMAGE);

转载请注明原文地址:https://www.9cbs.com/read-35428.html

New Post(0)