Some common methods in VC (20)

xiaoxiao2021-03-06  67

Some common methods in VC (20) 2003-11-3 Add to www.9cbs.net Author Name 0 reviews Click 5383 times

First, 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;

Third, close the computer OSVERSIONINFO OsVersionInfo; // data structure containing OsVersionInfo.dwOSVersionInfoSize = sizeof OS version information (OSVERSIONINFO); GetVersionEx (& OsVersionInfo); // Get OS version information if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {// Windows98, call the exitWindowSex () function to 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. //

Fourth, restart the computer typef int (int); // Show the pointer of the shutdown dialog function Hinstance Hinst = LoadLibrary ("shell32.dll"); // Load shell32.dll shutdowndlg shutdowndialog; // point to shell32 The Pointer IF (Hinst! = NULL) {// Get the address of the function and calls Shutdowndialog = (ShutDowndlg) GetProcaddress (Hinst, (LPSTR) 60);

(* Shutdowndialog) (0);

Fifth, to 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 (LPNUMLOGFONT 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

Sixth, only one program instance is run, if you have run, exit if (FindWindow (NULL, "Program Title")) EXIT (0); 7. Get the current mouse in 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 ();}

Ten, get the icon of executable Hicon Hicon = :: Extracticon (AFXGetInstanceHandle (), _ T ("NOTEPAD.EXE"), 0); if (Hicon && Hicon! = (HICON) -1) {PDC-> Drawicon (10 , 10, Hicon;

DESTROYICON (HICON);

Eleven, program presentation window aside automatically 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 = LPRECT-> LEFT; int = lpRect-> TOP;

IF (IX 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-i); AdjustPos (LPRECT); return true;}} (iv lprect-> width ()> rcwa.right - detastep && ix! = rcwa.right-lpRect-> W

Idth ()) {// Adjust right // pwnd-> setWindowPos (NULL, RCWA.right-rcw.width (), IY, 0, 0, SWP_NOSIZE); LPRECT-> Offset (rcwa.right-lpRect-> Right , 0); AdjustPos (LPRECT); Return True;} f (= lprect-> height ()> rcwa.bottom - detastep && = 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;} return false ;} // Then use the procedure in the onmoveing ​​event to call 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 dialog (select Resource Symbols ... You can display the dialog) Define the menu item. ID, the ID 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 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, 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 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 a separator (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 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 app: 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 () -> Loadicon; ASSERT (HICON); AFXGETMAINWND () -> SendMessage (wm_seticon, true, (lparam) Hicon;

XVI, another way to change the window title use statement cwnd * m_pcWnd = AFXGETMAINWND (), then call SETWINDOWTE XT () functions in the following form: setWindowText (* m_pcwnd, (lpctstr) m_windowtext); // m_windowtext can be A variable of a CST Ring class.

17. Copying image data by reinforcing meta files on the clipboard to copy image data to any application through the meta file, which can be placed in the CView derived class.

Number. 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_pmetad;

Eighteen, the transfer of text data on the clipboard puts the text onto the splic panel: cstring source; // put your text in Source if (OpenClipBoard ()) {Hglobal Clipbuffer; Char * Buffer; EmptyClipboard (); CLIPBUFFER = GlobalLococ (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 plate:

Char * buffer; if (OpenClipboard ()) {buffer = (char *) getclipboardData (cf_text); // do sometying with buffer here // before it goes out of scope} closeclipboard ();

Nineteen, the captured screen images to shear 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 high 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 for the screen 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 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;}}

Twenty, how to display bitmap scales in the Static control // Display bitmap void cshowbmpindlgdlg :: showbmpinstaic () {cbitmap hbmp; hbitMap; // Point PSTATIC * Pstaic; PSTAIC = (Cstatic *) getdlgitem (IDC_IMAGE); // Loading resource mm.bmp is my file name, with your replacement hbitmap = (hbitmap) :: loadImage (: AFXGetInstanceHandle (), "mm.bmp", image_bitmap , 0, 0, Lr_LoadFromFile | LR_CREATEDIBSECTION;

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

New Post(0)