20 tips for VC development

Source: Internet
Author: User
Tips for VC development: 20 I, Open the CD-ROM mcisendstring ("set cdaudio door open wait", null, 0, null );

II,Disable cd_rom mcisendstring ("set cdaudio door closed wait", null, 0, null );

III,Close the computer osversioninfo; // data structure containing the operating system version information
Osversioninfo. dwosversioninfosize = sizeof (osversioninfo );
Getversionex (& osversioninfo); // get the operating system version
If (osversioninfo. dwplatformid = ver_platform_win32_windows)
{
// Windows98, call the exitwindowsex () function to restart the computer
DWORD dwreserved;
Exitwindowsex (ewx_reboot, dwreserved); // you can change the first parameter to unregister a user,
// Shutdown, power off, and other operations
// Some processing programs before exiting}

4. restart the computer typedef int (callback * shutdowndlg) (INT); // display the function pointer in the shutdown dialog box.
Hinstance hinst = loadlibrary ("shell32.dll"); // load shell32.dll
Shutdowndlg shutdowndialog; // pointer to the function in the shutdown dialog box displayed in the shell32.dll Library
If (hinst! = NULL)
{
// Obtain and call the function address
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: encrypt (HDC) DC, & lf, (fontenumproc) enumfontfamproc, (lparam) This, 0 );
// Int callback (effeclpelf, 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 ;}
M_ctlfontlist is a list control variable.6. Run only one program instance at a time. Exit if it is already running.If (findwindow (null, "program title") Exit (0 );7. Get the current mouse positionCpoint pt; getcursorpos (& pt); // obtain the location

8. Context Menu event triggering event: Oncontextmenu event

9. Show and hide program menusCwnd * pwnd = afxgetmainwnd ();
If (B _m) // hide the menu
{
Pwnd-> setmenu (null );
Pwnd-> drawmenubar ();
B _m = false;
}
Else
{
Cmenu menu;
Menu. loadmenu (idr_mainframe); // you can change the menu items when the menu is displayed.
Pwnd-> setmenu (& menu );
Pwnd-> drawmenubar ();
B _m = true;
Menu. Detach ();
}

10. Obtain the executable file iconHicon =: extracticon (AfxGetInstanceHandle (), _ T ("notepad.exe"), 0); If (hicon & hicon! = (Hicon)-1) {PDC-> drawicon (10, 10, hicon);} destroyicon (hicon ); 11. Demonstration of window auto-bypass ProgramBool adjustpos (crect * lprect) {// automatic edge int ISX = getsystemmetrics (TRIM); int ISY = getsystemmetrics (TRIM); rect rworkarea; bool bresult = systemparametersinfo (spi_getworkarea, sizeof (rect), & rworkarea, 0); crect rcwa; If (! Bresult) {// if the call fails, use getsystemmetrics to obtain the screen area rcwa = crect (, ISX, ISY);} else rcwa = rworkarea; int IX = lprect-> left; int Iy = lprect-> top; If (IX <rcwa. left + detastep & Ix! = Rcwa. left) {// Adjust left // pwnd-> setwindowpos (null, rcwa. left, Iy, 0, 0, swp_nosize); lprect-> offsetrect (rcwa. left-IX, 0); adjustpos (lprect); Return true;} If (Iy <rcwa. top + detastep & Iy! = Rcwa. top) {// adjust // 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 & Ix! = Rcwa. right-lprect-> width () {// Adjust right // pwnd-> setwindowpos (null, rcwa. right-rcW.Width (), Iy, 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 the following // pwnd-> setwindowpos (null, IX, rcwa. bottom-rcW.Height (), swp_nosize); lprect-> offsetrect (0, rcwa. bottom-lprect-> bottom); Return true;} return false;} // call the crect r = * prect in the onmoveing event using the following procedure; adjustpos (& R ); * prect = (rect) R;

12. Add a menu item to the System MenuTo add a menu item to the system menu, perform the following three steps: first, use the resource symbols Dialog (select resource symbols in the View menu... define the menu item ID, which must be greater than 0x0f and less than 0xf000. Second, call cwnd: getsystemmenu to obtain the pointer of the System menu and call cwnd :: appendmenu adds menu items to the menu. The following example adds two new int cmainframe: oncreate (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_mysysitem, strmenuitem );...}

13. Run other programs// 1. Run the email or URL char szmailaddress [80]; strcpy (szmailaddress, "mailto: netvc@21cn.com"); ShellExecute (null, "open", szmailaddress, null, null, sw_shownormal); // 2. Run the executable program
Winexec ("notepad.exe", sw_show); // run the event plan 14. add or delete menus dynamically 1, Add menu// Add cmenu * mainmenu; mainmenu = afxgetmainwnd ()-> getmenu (); // obtain 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 (); // re-draw the menu

2, Delete menu// Delete cmenu * mainmenu; mainmenu = afxgetmainwnd ()-> getmenu (); // obtain the main menu cstring STR; For (INT I = (mainmenu-> getsubmenu (0 )) -> getmenuitemcount ()-1; I> = 0; I --) // gets the number of menu items. {(Mainmenu-> getsubmenu (0)-> getmenustring (I, STR, mf_byposition); // copy the label of the specified menu item to the specified buffer. The description of mf_byposition can be found in. If (STR = "Always On & Top") // if it is the menu item we just added, delete it. {(Mainmenu-> getsubmenu (0)-> deletemenu (I, mf_byposition); break ;} 15. Change the application icon
Static change: Modify the icon resource idr_mainframe. It has two icons, one of which is 16*16 and the other is 32*32. Be sure to modify them together.
Dynamic Change: Send the wm_seticon message to the main window. The Code is as follows:
Hicon = afxgetapp ()-> loadicon (idi_icon );
Assert (hicon );
Afxgetmainwnd ()-> sendmessage (wm_seticon, true, (lparam) hicon ); 16. Another method for changing the window titleUse the cwnd * m_pcwnd = afxgetmainwnd () statement, and then call the setwindowtext () function in the following form:
Setwindowtext (* m_pcwnd, (lpctstr) m_windowtext); // m_windowtext can be a cstring variable. 17. Copy image data through Enhanced Meta files on the clipboardThe following code copies image data to any application through a Metafile, which can be placed in the function of the cview derived class. Cmetafiledc * m_pmetadc = new cmetafiledc (); m_pmetadc-> createenhanced (getdc (), null, null, "whatever "); // 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 (callback, hmf); closeclipboard (); // deletemetafile (hmf); Delete m_pmetadc; 18. Transfer of text data on the clipboardPlace the 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 Splicing board: 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 cut version
Void preview: oncutscreen () {showwindow (sw_hide); rect r_bmp = {0,: getsystemmetrics (sm_cxscreen),: getsystemmetrics (sm_cyscreen)}; hbitmap; hbitmap = copyscreentobitmap (& r_bmp); // hwnd is the program window handle if (openclipboard () {emptyclipboard (); setclipboarddata (cf_bitmap, hbitmap); closeclipboard ();} showwindow (sw_show);} hbitmap representation: copyscreentobitmap (lprect) {// lprect represents the selected region {HDC hscrdc, hmemdc; // hbitmap table for screen and memory devices, holdbitmap; // bitmap handle int NX, NY, nx2, ny2; // The coordinates of the selected area int nwidth, nheight; // The bitmap width and height int xscrn, yscrn; // screen resolution // ensure that the selected area is not empty. 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 table hmemdc = createcompatibledc (hscrdc) for the screen device description table ); // obtain the coordinates of the selected region Nx = lprect-> left; ny = lprect-> top; nx2 = lprect-> right; ny2 = lprect-> bottom; // obtain the screen resolution xscrn = getdevicecaps (hscrdc, horzres); yscrn = getdevicecaps (hscrdc, vertres); // make sure 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 hbitmap = createcompatiblebitmap (hscrdc, nwidth, nheight) compatible with the on-screen device description table ); // select the new bitmap to the holdbitmap = (hbitmap) SelectObject (hmemdc, hbitmap) in the memory device description table; // copy the on-screen device description table to the memory device description table

Bitblt (H

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.