Common VC programming experience

Source: Internet
Author: User
1. Open CD-ROM
Mcisendstring ("set cdaudio door open wait", null, 0, null );
Ii. Disable cd_rom
Mcisendstring ("set cdaudio door closed wait", null, 0, null );
3. Disable 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
// Process before exitingProgram 
}
4. restart the computer
Typedef int (callback * shutdowndlg) (INT); // display the pointer of the function 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
: 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;
}
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 position
Cpoint pt;
Getcursorpos (& pt); // obtain the location
8. Context Menu event trigger event: oncontextmenu event

9. Show and hide the program menu
cwnd * pwnd = afxgetmainwnd ();
If (B _m) // hide the menu
{< br> pwnd-> setmenu (null);
pwnd-> drawmenubar ();
B _m = false;
}< br> else
{< br> cmenu menu;
menu. loadmenu (idr_mainframe); // you can change the menu item
pwnd-> setmenu (& menu);
pwnd-> drawmenubar ();
B _m = true;
menu. detach ();
}< br> 10. Obtain the executable file icon
hicon =: extracticon (AfxGetInstanceHandle (), _ T ("notepad.exe" ), 0);
If (hicon & hicon! = (Hicon)-1)
{< br> PDC-> drawicon (10, 10, hicon);

}< br> destroyicon (hicon);
11. Demonstration of window auto-bypass Program
bool adjustpos (crect * lprect)
{// automatic edge selection
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 fails, use getsystemmetrics to obtain the screen area
rcwa = crect (, ISX, ISY );
}< br> else
rcwa = rworkarea;
int IX = lprect-> left;
int Iy = lprect-> top;

If (IX {// Adjust left
// pwnd-> setwindowpos (null, rcwa. left, Iy, 0, 0, swp_nosize);
lprect-> offsetrect (rcwa. left-IX, 0);
adjustpos (lprect);
return true;
}< br> If (Iy {// adjust
// pwnd-> setwindowpos (null, IX, rcwa. top, 0, 0, swp_nosize);
lprect-> offsetrect (0, rcwa. top-Iy);
adjustpos (lprect);
return true;
}< br> If (IX + lprect-> width ()> rcwa. right-detastep & Ix! = Rcwa. Right-lprect-> W

idth ()
{// Adjust right
// pwnd-> setwindowpos (null, rcwa. right-rcW.Width (), Iy, swp_nosize);
lprect-> offsetrect (rcwa. right-lprect-> right, 0);
adjustpos (lprect);
return true;
}< br> If (Iy + lprect-> height ()> rcwa. bottom-detastep & Iy! = Rcwa. bottom-lprect
-> height ()
{// adjust
// pwnd-> setwindowpos (null, IX, rcwa. bottom-rcW.Height (), swp_nosize);
lprect-> offsetrect (0, rcwa. bottom-lprect-> bottom);
return true;
}< br> return false;
}< br> // call the process in the onmoveing event

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, perform the following three steps:
first, use the resource symbols Dialog (select resource symbols from the View menu... you can
show this dialog box) define the menu item ID, which should be greater than 0x0f and less than 0xf000;
second, call cwnd :: getsystemmenu gets the pointer to the system menu and calls cwnd: appendmenu to add the
menu item to the menu. The following example adds two new
int cmainframe: oncreate (lpcreatestruct) to the system menu.
{< br>...
// 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);
...
}< br> 13. 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 program
winexec ("notepad.exe", sw_show ); // run the event plan
14. add or delete menus dynamically
1. Add menus
// 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 the menu
// Delete
cmenu * mainmenu;
mainmenu = afxgetmainwnd ()-> getmenu (); // obtain the main menu.

cstring STR;
for (INT I = (mainmenu-> getsubmenu (0)-> getmenuitemcount ()-1; I> = 0; I --) // obtain the number of items in the menu
.
{< br> (mainmenu-> getsubmenu (0)-> getmenustring (I, STR, mf_byposition );
// copy the label of the specified menu item to the specified buffer zone. 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.
{< br> (mainmenu-> getsubmenu (0)-> deletemenu (I, mf_byposition);
break;
}< br> 15. Change the application icon
static change: Modify the icon resource idr_mainframe. It has two icons, one being 16*16 and the other being 3

2*32. Be sure to modify them together.
Dynamic Change: Send the wm_seticon message to the main window.CodeAs follows:
Hicon = afxgetapp ()-> loadicon (idi_icon );
Assert (hicon );
Afxgetmainwnd ()-> sendmessage (wm_seticon, true, (lparam) hicon );
16. Another method for changing the window title
Use the cwnd * m_pcwnd = afxgetmainwnd () statement, and then call setwindowte as follows:
XT () function:
Setwindowtext (* m_pcwnd, (lpctstr) m_windowtext); // m_windowtext can be a CST
Ring variables.
17. Copy image data through Enhanced Meta files on the clipboard
The following code copies the image data to any application through a Metafile, which can be placed in a letter of the cview derived class

Medium.
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 (cf_enhmetafile, hmf );
Closeclipboard ();

// Deletemetafile (hmf );
Delete m_pmetadc;
18. Transfer of text data on the clipboard
Place the text on the cut 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 cut 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 cshowbmp indlgdlg: oncutscreen ()
{
Showwindow (sw_hide );
Rect r_bmp = {0, 0,: getsystemmetrics (sm_cxscreen ),
: Getsystemmetrics (sm_cyscreen )};
Hbitmap;
Hbitmap = copyscreentobitmap (& r_bmp );

// hwnd is the program window handle
If (openclipboard ()
{< br> emptyclipboard ();
setclipboarddata (cf_bitmap, hbitmap );
closeclipboard ();
}< br> showwindow (sw_show);
}< br> hbitmap cshowbmp indlgdlg: copyscreentobitmap (lprect)
{< br> // lprect indicates the selected region
{< br> HDC hscrdc and hmemdc;
// screen and memory device description table
hbitmap, holdbitmap;
// bitmap handle
int NX, NY, nx2, ny2;
// coordinates of the selected region
int nwidth and nheight;
// The bitmap width and height
int xscrn and yscrn;
// screen resolution

// make sure that the selected area is not empty.
If (isrectempty (lprect)
return NULL;
// create a device description table for the screen
hscrdc = createdc ("display", null );
// create a compatible memory device description table for the screen device description table
hmemdc = createcompatibledc (hscrdc );
// 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 compatible with the on-screen device description table
hbitmap = createcompatiblebitmap
(hscrdc, nwidth, nheight );
// select the new bitmap to the memory device description table
holdbitmap = (hbitmap) SelectObject (hmemdc, hbitmap );
// copy the on-screen device description table to the memory device description table
bitblt (hmemdc, 0, 0, nwidth, nheight,
hscrdc, NX, NY, srccopy);
// obtain the screen bitmap handle
hbitmap = (hbitmap) SelectObject (hmemdc, holdbitmap);

// clear
deletedc (hscrdc);
deletedc (hmemdc);
// return the bitmap handle
return hbitmap;
}< BR >}< br> 20. How to scale a bitmap to the static control
// display the bitmap in the Staic Control
void cshowbmp indlgdlg :: showbmp instaic ()
{< br> cbitmap hbmp;
hbitmap;
// point pstatic to the location to be displayed
cstatic * pstaic;
pstaic = (cstatic *) getdlgitem (idc_image);
// load resource mm.bmp is a file name of mine. Replace it with yours.
hbitmap = (hbitmap): LoadImage (: AfxGetInstanceHandle (), "mm.bmp",
image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection);

hbmp. attach (hbitmap);
// obtain the image 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 (lrect. left, lrect. top, lrect. width (), lrect. heigh
T (),
& dcmem, 0, 0, BM. bmwidth, BM. bmheight, srccopy);
dcmem. selectObject (& poldbitmap);
}

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.