VC development tips 20

zhaozj2021-02-17  51

VC development tips 20

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 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.

// some processing prior to exiting

}

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 = (Shutdowndialog) (* Shutdowndialog); (* shutdowndialog); (* shutdowndialog) (* shutdowndialog) } 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 (lppelf-> 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 once, if you have run, exit

IF (FindWindow)) EXIT (0); seven, get the current mouse location

Cpoint pt;

GetCursorpos (& PT); // Get position

Eight, context menu event trigger event: onContextMenu event

Nine, display and hidden programs menu

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 for executable

Hicon Hicon = :: Extracticon (AfxGetInstanceHandle (), _ T ("Notepad.exe"), 0)

IF (Hicon && HiCon! = (HICON) -1)

{

PDC-> Drawicon (10, 10, Hicon);

}

DESTROYICI (HICON);

Eleven, window automatic leaning program demonstration

BOOL AdjustPos (CRECT * LPRECT)

{// automatic side

INT isx = getSystemMetrics; SM_CXFULLSCREEN;

INT isy = getSystemMetrics (SM_CYFULLSCREEN);

Rect rworkarea;

Bool Bresult = SystemParametersInfo (SPI_GETWORKAREA, SIZEOF (RECT), & RWORKAREA, 0);

CRECT RCWA;

IF (! BRESULT)

{// If the call is unsuccessful, use GetSystemMetrics to get the screen area.

RCWA = CRECT (0, 0, ISX, ISY);

}

Else

RCWA = RWORKAREA;

Int ix = lprect-> left;

INT IY = LPRECT-> TOP;

IF (ix

{// adjust left

//pwnd->etwindowpos (Null, Rcwa.left, iY ,0, 0 ,swp_nosize);

LPRECT-> OffsetRect (rcwa.left-ix, 0);

AdjustPos (LPRECT);

Return True;

}

IF (IY

{// adjustment

// 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 && = rcwa.right-lprect-> width ())

{// adjust the 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 ())

{// adjustment

// 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 call in the onMoveing ​​event.

CRECT R = * prect;

AdjustPos (& R);

* prect = (RECT) R;

12. Add a menu item to the system menu

Add a menu item to the system menu to make the following three steps:

First, use the Resource Symbols dialog (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;

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 next example adds two new items to the system menu.

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_mysysiSISISISISIM, STRMENUITEM);

...

}

Thirteen, run other procedures

// 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 to

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 the menu

//delete

Cmenu * mainmenu;

Mainmenu = AFXGETMAINWND () -> getMenu (); // Get the main menu

CString Str;

For (INT I = (MainMenu-> GetSubMenu (0)) -> getMenuItemCount () - 1; i> = 0; I -) // Number of items to get 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 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 () -> 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 the setWindowText () function in the following form: setWindowText (* m_pcwnd, (lpctstr) m_windowtext); // m_windowText can be a variable of a CString class.

17. Copying image data by enhancement metafile on the clipboard

The following code copies the image data through the meta file to any application, which can be placed in a function of the CVIEW derived class.

Cmetafiledc * m_pmetadc = new cmetafiledc ();

m_pmetadc-> createenhanced (getDC (), NULL, NULL, "Wherever");

// 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

Place the text onto the splic panel:

CSTRING SOURCE;

// Put your text in Source

IF (OpenClipboard ())

{

Hglobal Clipbuffer; Char * Buffer;

EMPTYCLIPBOARD ();

Clipbuffer = GlobalAlloc (gmem_ddeshare, source.getLength () 1);

Buffer = (char *) Globalock (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 Something with buffer Here

// before it goes out of scope

}

CloseClipboard ();

XII, will capture the screen image to the cutting 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 program 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;

// bit map handle

INT NX, NY, NX2, NY2;

// Select region coordinates

INT NWIDTH, NHEIGHT;

// bitmap width and height

Int xscrn, yscrn;

// Screen Resolution

/ / Make sure the selected area is not empty rectangle

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 for the screen device description table

HMEMDC = CREATECOMPATIBLEDC (HSCRDC);

/ / Get selected area coordinates

Nx = lprect-> left;

NY = lpRect-> TOP;

NX2 = lpRect-> Right;

NY2 = LPRECT-> BOTTOM;

// Get the screen resolution

Xscrn = getDeviceCaps (HSCRDC, Horzres);

YSCRN = GetDeviceCaps (HSCRDC, Vertres);

/ / Ensure 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 with a screen device description table

(HSCRDC, NWIDTH, NHEIGHT);

/ / Put the new bitmap in the memory device descriptor table

Holdbitmap = (hbitmap) SelectObject (HMEMDC, HBitmap);

/ / Copy the screen device description table to the memory device descriptor table

Bitblt (Hmemdc, 0, 0, NWIDTH, NHEIGHT,

HSCRDC, NX, NY, SRCCOPY;

/ / Get the handle of the screen bitmap

Hbitmap = (hbitmap) SelectObject (HMEMDC, Holdbitmap);

// Clear

Deletedc (HSCRDC);

Deletedc (HMEMDC);

// Return the bitmap section

Return hbitmap;

}

}

Twenty, how to display bitmap scales in the Static control

// Display bitmap void cshowbmpindlgdlg :: showbmpinstaic () {cbitmap hbmpinstaic () {cbitmap hbmp; // points PSTATIC * Pstain; PSTAIC = (cstatic *) getdlgitem (IDC_IMAGE);

// Loading resources mm.bmp is my file name, with your replacement hbitmap = (hbitmap) :: loadImage (: :: AfETITINSTANCEHANDLE (), "mm.bmp", image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection; HBMP.Attach (HBitmap);

/ / Get the picture format Bitmap BM; HBMP.GetBitmap (& BM);

CDC DCMEM; DCMem.createCompatibleDC (GETDC ()); CBITMAP * PoldbitMap = (cBitmap *) DCMem.selectObject (HBMP);

CRECT LRECT; PSTAIC-> GetClientRect (& LRECT); LRECT.NORMALIZERECT ();

// Display bitmap pstaic-> getdc () -> stretchblt (LRECT.WIDT, LRECT.TOP, LRECT.WIDTH (), LRECT.HEIGHT (), & DCMEM, 0, 0, BM.BMWIDTH, BM.BMHEIGHT, SRCCOPY );

DcMem.SelectObject (& Poldbitmap);

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

New Post(0)