Programming Tips 20 Method (Revised)

xiaoxiao2021-03-06  40

1. How to activate the current screen saver

/ / Activate the current screen saver, Jingzhou Xu

Postmessage (WM_SYSCOMMAND, SC_SCREENSAVE, 0);

2. How to disable / enable screen protection and power management

Static uint dss_getlist [] = {SPI_GetLowPowerTimeout, SPI_GetPowerOfftimeout, SPI_GetScreensavetimeout};

Static uint DSS_SETLIST [] = {SPI_SETLOWPOWPOWPOWERTIMEOUT, SPI_SETPowerOfftimeout, SPI_SETSCREENSAVETIMEOUT};

Static const Int dss_listcount = _countof (DSS_GETLIST);

l Disable screen protection and power management

{

m_pvalue = new int [DSS_ListCount];

For (int x = 0; x

{

// Disable screen protection and power management

Verify (SystemParametersInfo (DSS_SETLIST [X], 0, NULL, 0));

}

delete [] m_pvalue;

}

l Enable screen protection and power management

{

m_pvalue = new int [DSS_ListCount];

For (int x = 0; x

{

// Enable screen protection and power management

Verify (SystemParametersInfo (DSS_SETLIST [X], M_PValue [x], NULL, 0));

}

delete [] m_pvalue;

}

3. How to activate and close IE browser

// activate and open IE

Void Lounchie ()

{

HWND H = FindWindowEx (Null, Null, Null,

"Microsoft Internet Explorer");

Shellexecute (H, "Open", "C: //simple.html",

NULL, NULL, SW_SHOWNORMAL);

}

// Turn off IE and other applications

Void Closeie ()

{

INT app = bsm_applications;

Unsigned long bsm_app = (unsigned long) app;

BroadcastsystemMessage (BSF_POSTMESSAGE, & BSM_APP,

WM_Close, NULL, NULL;

}

4. How to join the tool tips to the tree control

l First, add the TVS_INFOTIP attribute style to the tree control, as shown below:

IF (! m_ctrltree.create (ws_child | ws_visible |

TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_SHOWSELALWAYS | TVS_Infotip, // Join TVS_INFOTIP, JINGZHOU XU (Tree Control Id: 100)

CRECT (0, 0, 0, 0), & M_WndtreeBar, 100)))

{

Trace0 ("Failed to Create Instant Bar Child / N");

Return -1;

}

l Secret to join the mapping message declaration, as shown below:

AFX_MSG VOID ONGETINFOTIP (NMHDR * PNMHDR, LRESULT * PRESULT); / / Tree Control Join the prompt message, Jingzhou Xu

ON_NOTIFY (TVN_GetInfotip, 100, OnGetInfotip) / / Tree Control Enterprise Add to Joching, Jingzhou XUL finally adds the respected culvert processing:

Void ccreatetreedlg :: ONGETINFOTIP (nmhdr * pnmhdr,

LRESULT * PRESULT)

{

* PRESULT = 0;

NMTVGETINFOTIP * PTVTIPINFO = (NMTVGETITIP *) PNMHDR;

LPARAM ITEMDATA = (DWORD) PTVTIPINFO-> LPARAM;

/ / Corresponding to each entry

HtreeItem HITEM = PTVTIPINFO-> HITEM;

CSTRING TIP;

HtreeItem HrootItem = m_chassistree.getrootItem ();

IF (hrootItem! = ptvtipinfo-> HITEM)

{

TIP = "Tips for Tree Node";

}

Else

{

Tip = "Tips on Tree Roots";

}

STRCPY (ptvtipinfo-> psztext, (lpctstr) TIP);

}

5. How to get the path to the system information box

#include

#define ids_reg_key_msinfo_path1_t ("Software // Microsoft // Shared Tools // Msinfo")

#define IDS_REG_KEY_MSINFO_PATH2_T ("Software // Microsoft // Shared Tools Location")

#define IDS_REG_VAL_MSINFO_PATH1_T ("Path")

#define IDS_REG_VAL_MSINFO_PATH2_T ("msinfo")

#define IDS_MSINFO_EXE_NAME_T ("msinfo32.exe")

// ...

Bool getSysInfoPath (CString & Strpath)

{

StrPath.empty ();

LPTSTR pszpath = strpath.getBuffer (MAX_PATH);

CregKey REG;

DWORD dwsize = max_path;

Long nret = reg.open (HKEY_LOCAL_MACHINE, IDS_REG_KEY_MSINFO_PATH1, KEY_READ);

// Look for the first "msinfo32.exe" location in the registry

IF (nret == error_success)

{

#if (_MFC_VER> = 0x0700)

NRET = reg.queryStringValue (IDS_REG_VAL_MSINFO_PATH1, PSZPATH, & DWSIZE);

#ELSE

NRET = reg.queryvalue (pszpath, ids_reg_val_msinfo_path1, & dwsize);

#ENDIF

Reg.close ();

}

// If you fail for the first time, I will find a second time.

IF (nret! = error_success)

{

NRET = reg.open (HKEY_LOCAL_MACHINE, IDS_REG_KEY_MSINFO_PATH2, Key_Read);

IF (nret == error_success)

{

#if (_MFC_VER> = 0x0700)

Reg.QueryStringValue (IDS_REG_VAL_MSINFO_PATH2, PSZPATH, & DWSIZE);

#ELSE

REG.QUERYVALUE (pszpath, ids_reg_val_msinfo_path2, & dwsize);

#ENDIF

// The path name does not include the EXE file name

IF (nret == error_success)

Verify (:: pathappend (pszpath, ids_msinfo_exe_name);

Reg.close ();

}

}

StrPath.releaseBuffer ();

StrPath.Freeextra ();

// Check if the file is valid.

Return :: PathfileExists (STRPATH);

}

6. How to run a program directly in a resource

BOOL Run ()

{

CFILE F;

Char * pfilename = "execution.exe";

IF (! f.open (pfileename, cfile :: modecreate | cfile :: Modewrite, Null)

{

AfxMessageBox ("Can Not Create File!");

Return 0;

}

CString path = f.getfilepath ();

Hglobal HRES;

HRSRC HRESINFO;

// Get application instances

Hinstance Insapp = AfxGetInstanceHandle ();

// Look for EXE resources name

HRESINFO = FindResource (INSAPP, (LPCSTR) IDR_EXE4, "EXE");

HRES = loading (insapp, hresinfo); // loading it

DWORD DFILENGTH = SizeOfResource (Insapp, HRESINFO); // Calculate EXE file size

F.Writehuge ((lpstr) hres, dfilelength); // Write temporary file

f.close ();

Hinstance hinssd = shellexecute (NULL, "Open", Path, NULL, NULL, SW_SHOWNORMAL);> // Run it.

Return 1;

}

7. How to traverse the entire directory

#include

#include

// Browse the directory.

Void Browsefolder (Void)

{

TCHAR PATH [MAX_PATH];

Browseinfo bi = {0};

bi.lpsztitle = ("Remissive Call All Directory");

LpitemidList PIDL = SHBROWSEFORFOLDER (& BI);

IF (PIDL! = 0)

{

/ / Get the directory path

ShgetPathFromidList (PIDL, PATH);

/ / Set to the current path

SetCurrentDirectory (PATH);

/ / Search all subdiresses

SearchFolder (PATH);

/ / Release memory

IMALLOC * IMALLOC = 0;

IF (Succeeded (ShuTmalloc (& IMALLOC)))

{

Imalloc-> free (PIDL);

IMALLOC-> Release ();

}

// Search all subdirectories and files.

Void SearchFolder (Tchar * PATH)

{

Win32_find_data findfiledata;

Handle hfind;

TCHAR FileName [MAX_PATH 256];

TCHAR PATHBAK [MAX_PATH];

// Copy the initial user selection directory

STRCPY (Pathbak, PATH);

/ / Looking for the first file

Hfind = findfirstfile ("*. *", & findfiledata);

/ / Search all files and subdirectories

DO

{

IF (HFIND! = INVALID_HANDLE_VALUE)

{

// If it is the current directory or parent directory, skip

IF (! ") ||! (FindfileData.cfileName,")))

{

CONTINUE;

}

// Restore the initial user selection directory

STRCPY (PATH, PATHBAK);

/ / List all discovered files

Sprintf (Path, "% S / /% S", PATH, FINDFILEDATA.CFILENAME);

// If setCurrentDirectory is successful, it is a directory, recursive calls Continue search subdirectory

IF ((SETCURRENTDIRECTORY (PATH)))

{

SearchFolder (PATH);

}

// Insert the file and path name to the list box m_listbox_hwnd

SendMessage (m_listbox_hwnd, lb_addstring, 0, path); // <- insert what you want done here!

}

}

While (FindNextFile (HFIND, & FINDFILEDATA) && Hfind! = Invalid_Handle_Value);

FindClose (HFIND);

}

8. How to disable / enable system hot keys

Bool bold;

l Prohibition system hotkey

// Shield off the system key

SystemParametersInfo (SPI_SETSCREENSAVERRUNNING, TRUE, & BOLD, SPIF_UPDATEINIFILE);

l Enable system hotkey

// Restore system hotkey

SystemParametersInfo (SPI_SETSCREENSAVERRUNNING, FALSE, & BOLD, SPIF_UPDATEINIFILE);

9. How to hide / display the Windows system taskbar

l Hidden system taskbar

// Hide Windows System Task Bar

:: ShowWindow (:: FindWindow ("shell_traywnd", null), sw_hide;

l Display system taskbar

/ / Restore the Windows System Task Bar Normal Display

:: ShowWindow (:: FindWindow ("shell_trayWnd", null), sw_show;

10. How to implement the animation effect between the window to the system area icon

// ******************************************************** ***************************************

// * Name: FindtrayWnd

// * Author: Xu Jing Zhou (jingzhou_xu@163.net)

// * Function: Before displaying the form animation effect, look for system area location // *************************************** ***********************************************************

Bool Callback FindtrayWnd (HWND HWND, LPARAM LPARAM)

{

Tchar szclassname [256];

GetclassName (HWND, SZClassName, 255);

// Compare the window class name

IF (_TCSCMP (SzclassName, _T ("TRAYNOTIFYWND")) == 0)

{

CRECT * prect = (create *) lparam;

:: getWindowRect (hwnd, prect);

Return True;

}

// When you find a clock window, you can end.

IF (_TCSCMP (SzclassName, _T ("TRAYCLOCKWCLASS") == 0)

{

CRECT * prect = (create *) lparam;

CRECT RectClock;

:: getWindowRect (HWND, RectClock);

Prect-> Right = RectClock.Left;

Return False;

}

Return True;

}

// ******************************************************** ***************************************

// * Name: Winanimation

// * Author: Xu Jing Zhou (jingzhou_xu@163.net)

// * Function: Display the number of window animation effects

// ******************************************************** ***************************************

Void cscreensnapdlg :: WinaniMation (Bool Showflag)

{

CRECT RECT (0, 0, 0);

/ / Find Tray Window

CWND * PWND = FINDWINDOW ("shell_traywnd", null);

IF (PWND)

{

PWND-> getWindowRect (RECT);

EnumChildWindows (PWND-> M_HWND, FindtrayWnd, (LPARAM) & Rect);

// RECT is a rectangle of the tray area

CRECT RCWND;

GetWindowRect (rcwnd);

IF (Showflag) // Form Sliding System Area

DrawanimatedRects (GetSafehWnd (), IDANI_CAPTION, RCWND, RECT)

ELSE // Form slides from the system area

DrawanImatedRects (GetSafehWnd (), IDANI_CAPTION, RECT, RCWND);

}

}

Usage is as follows:

IF (iswindowvisible ()) // Has hidden

{

ShowWindow (sw_hide); // Hide form

WinaniMation (TRUE); // Form Animation Sliding into System Area

}

Else

{

WinaniMation (false); // Form Animation Sliding from the system area

ShowWindow;

}

11. How to determine the version of the current operating system

/ / -------------------------------------------------------------------------------------------- ------------------------------------ // Judging the operating system Caution and variables, Jingzhou XU

Typedef Enum tagwin32systype {

Windows32s,

Windowsnt3,

Windows95,

Windows98,

Windowsme,

WindowsNT4,

Windows2000,

Windowsxp

Win32SYSTYPE;

/ / Judgment Operating System Caution and Variables, Jingzhou XU

Win32Systype isshellsystype ()

{

Win32Systype shellType;

DWORD WINVER;

OsversionInfo * OSVI;

Winver = getversion ();

IF (WinVER <0x80000000) {/ * nt * /

ShellType = WindowsNT3;

Osvi = (OsversionInfo *) Malloc (Sizeof (OsversionInfo);

IF (OSVI! = NULL) {

MEMSET (OSVI, 0, SIZEOF (OsversionInfo);

Osvi-> dwosversioninfosize = sizeof (OsversionInfo);

GetVersionex (OSVI);

IF (OSVI-> dwmajorversion == 4L) shellType = WindowsNT4;

Else IF (OSVI-> dwmajorversion == 5L && OSVI-> dwminorversion == 0L) shellType = Windows2000;

Else if (Osvi-> dwmajorversion == 5L && OSVI-> dwminorversion == 1L) shellType = windowsxp;

Free (OSVI);

}

}

Else IF (LOBYTE (WinVER) <4)

ShellType = Windows32s;

Else {

ShellType = Windows95;

Osvi = (OsversionInfo *) Malloc (Sizeof (OsversionInfo);

IF (OSVI! = NULL) {

MEMSET (OSVI, 0, SIZEOF (OsversionInfo);

Osvi-> dwosversioninfosize = sizeof (OsversionInfo);

GetVersionex (OSVI);

IF (OSVI-> dwmajorversion == 4L && OSVI-> dwminorversion == 10L) shellType = Windows98;

Else IF (OSVI-> dwmajorversion == 4L && OSVI-> dwminorversion == 90L) shellType = Windowsme;

Free (OSVI);

}

}

Return shellType;

}

/ / -------------------------------------------------------------------------------------------- ------------------------------------------------

12. How to display multi-line text in the specified rectangle box

///

// Description:

/ / Display multi-line text horizontally or vertically in the rectangular box, jingzhou Xu.// LMODE: arrangement, 0: horizontal mode; 1: vertical alignment

// lhori: horizontally aligned, 0: Left alignment; 1: Case; 2: Right alignment; 3: Customize

// Lvert: Vertical alignment, 0: top alignment; 1: Case; 2: Bottom alignment; 3: Custom

///

CRECT DRAWTILINRECT (CDC * PDC, CSTRING SZSTRING, LPRECT LPRECT, LONG LMODE, Long Lhori, Long Lvert)

{

TextMetric TM;

PDC-> GetTextMetrics; & TM

Int tmpWidth = tm.tmavecharwidth, tmpheight = tm.tmHeight;

CRECT RCINNER (LPRECT);

IF (LMODE == 0)

{

Rcinner.Left = TmpWidth;

Rcinner.right- = tmpwidth;

RCINNER.TOP- = TmpWidth;

RCINNER.BOTTOM = TmpWidth;

}

IF (LMODE == 1)

{

Rcinner.Left = TmpWidth;

Rcinner.right = RCINNNER.LEFT TMPWIDTH;

RCINNER.TOP- = TmpWidth;

RCINNER.BOTTOM = TmpWidth;

}

PDC-> DrawText (Szstring, RCINNER, DT_CALCRECT);

Switch (lhori)

{

Case 0:

Break;

Case 1:

{

Long Xoutcent = (LPRECT-> Right LPRECT-> LEFT) / 2;

Long XinNCent = (RCINNER.Right Rcinner.Left) / 2;

RCINNER.LEFT = (Xoutcent-xinncent);

Rcinner.right = (xoutcent-xinncent);

}

Break;

Case 2:

{

Long Linwidth = RCINNER.right-rcinner.Left;

Rcinner.right = lpRect-> Right-tmpWidth;

RCINNER.LEFT = RCINNER.Right-linWidth;

}

Break;

DEFAULT:

Break;

}

Switch (LVERT)

{

Case 0:

Break;

Case 1:

{

Long YouTcent = (LPRECT-> BOTTOM LPRECT-> TOP) / 2;

Long Yinncent = (RCINNER.BOTTOM RCINNER.TOP) / 2;

RCINNER.TOP - = (YINNCENT-YOUTCENT);

RCINNER.BOTTOM - = (YINNCENT-YOUTCENT);

}

Break;

Case 2:

{

Long Linheigh = rcinner.top-rcinner.bottom;

Rcinner.bottom = lpRect-> Bottom TmpWidth;

RCINNER.TOP = RCINNER.BOTTOM Linheigh;

}

Break;

DEFAULT:

Break;

}

/ / -------------------------------------------------------------------------------------------- ------------------------------------------- // Function: New, old rectangles, recalculate the number of lines, make the text Multi-line, Jingzhou Xu

/ / -------------------------------------------------------------------------------------------- ---------------------------------------------

// Maximum number of characters in a row

INT nmaxlinechar = abs (lpRect-> Right - LPRECT-> LEFT) / TMPWIDTH;

// Record the width of the current line

Short thelinelarngth = 0;

/ / Record the number of Chinese characters in the current line to prevent half of the Chinese characters from dividing

Unsigned short halfchinese = 0;

For (int i = 0; i <= szstring.getLength () - 1; i )

{

IF ((unsigned char) szstring.get (i) == 0x0d) && (unsigned char) szstring.getat (i 1) == 0x0a)))

ThelineLength = 0;

/ / Greater than 0xA1 bytes are Chinese character bytes

IF ((unsigned char) szstring.getat (i)> = 0xA1)

Halfchinese ;

ThelineLength ;

/ / If the line width is greater than the maximum width per row, special processing

IF (ThelineLength> NmaxlineChar)

{

/ / Prevent one Chinese character from dividing into two lines, backtracking

IF (HalfChinese% 2)

{

Szstring.insert (i, (unsigned char) 0x0a);

Szstring.insert (i, (unsigned char) 0x0d);

}

Else

{

Szstring.insert (i-1, (unsigned char) 0x0a);

Szstring.insert (i-1, (unsigned char) 0x0d);

}

ThelineLength = 0;

}

}

/ / Re-calculate the rectangular boundaries

// int Tmpline = Int (Szstring.getLength () * TmpWidth / ABS (LPRECT-> Right - LPRECT-> LEFT) -0.5));

// tmpline = (szstring.getLength () * tmpwidth% ABS (lpRect-> Right - lprect-> left))? 1: 0;

// if (tmpline == 0)

// tmpline = 1;

IF (RCINNER.BOTTOM> LPRECT-> BOTTOM)

Rcinner.bottom = lpRect-> Bottom

IF (rcinner.top TOP)

Rcinner.top = lpRect-> top;

/ / -------------------------------------------------------------------------------------------- ---------------------------------------------

IF (lhori == 0)

PDC-> DrawText (Szstring, RCINNER, DT_WORDBREAK | DT_LEFT);

Else if (lhori == 1) PDC-> DrawText (Szstring, RCINNER, DT_WORDBREAK | DT_CENTER);

Else IF (lhori == 2)

PDC-> DrawText (Szstring, RCINNER, DT_WORDBREAK | DT_RIGHT);

Return Rcinner;

}

13. How to rotate the text in the specified rectangle

///

// Description:

/ / Turn the text in the rectangular box, Jingzhou Xu

//parameter:

// PDC: DC pointer

// Str: Display text

// RECT: Display range

// Angle: Rotation angle

// NOPTIONS: EXTTEXTOUT () Set

///

Void DrawrotatedText (CDC * PDC, Const Cstring Str, CRECT RECT,

Double Angle, Uint NOPTIONS)

{

// Depending on the conversion angle value

Double pi = 3.141592654;

Double Radian = Pi * 2/360 * Angle;

// Get the display text center point

CSIZE TEXTSIZE = PDC-> GetTextExtent (STR);

Cpoint center;

Center.x = Textsize.cx / 2;

Center.y = Textsize.cy / 2;

// Calculate the new center point of the text

Cpoint rCenter;

RCenter.x = long (COS (Radian) * center.x - sin (radian) * center.y);

RCenter.y = long (sin (radian) * center.x cos (radian) * center.y);

// Draw a text

PDC-> setTextAlign (ta_baseline);

PDC-> setbkmode (transparent);

PDC-> EXTTEXTOUT (Rect.Left Rect.width () / 2 - rCenter.x,

Rect.top Rect.Height () / 2 rCenter.y,

NOPTIONS, RECT, STR, NULL;

}

14. How to convert 32 x 32 pixel icons to 16 x 16 pixel values ​​icon

Hicon Convert32x32iconto16x16 (Hicon H32x32icon)

{

HDC HMAINDC, HMEMDC1, HMEMDC2;

Hicon H16X16CI;

Bitmap BMP;

Hbitmap HoldBMP1, HoldBMP2;

Iconfo iconInfo32x32, iconinfo16x16;

GeticonInfo (H32X32ICON, & iconInfo32x32);

HmainDC = :: getDC (m_hwnd);

HMEMDC1 = CREATECOMPATIBLEDC (HMAINDC);

HMEMDC2 = CreateCompatibleDC (HMAINDC);

GetObject (iconInfo32x32.hbmcolor, sizeof (bitmap), & bmp);

IconInfo16x16.hbmcolor = CreateBitmap (16, 16,

Bmp.bmplanes,

BMP.BMBITSPIXEL,

NULL);

HoldBMP1 = (hbitmap) SelectObject (hmemdc1, iconinfo32x32.hbmcolor);

HoldBMP2 = (hbitmap) SelectObject (HMEMDC2,

IconInfo16x16.hbmcolor);

Stretchblt (HMEMDC2,

0, 0,

16, 16,

HMEMDC1,

0, 0,

32, 32,

SRCCopy

);

GetObject (iconInfo32x32.hbmmask, sizeof (bitmap), & bmp);

IconInfo16x16.hbmmask = CreateBitmap (16, 16,

Bmp.bmplanes,

BMP.BMBITSPIXEL,

NULL);

SelectObject (HMEMDC1, IconInfo32x32.hbmmask);

SelectObject (hmemdc2, iconinfo16x16.hbmmask);

Stretchblt (HMEMDC2,

0, 0,

16, 16,

HMEMDC1,

0, 0,

32, 32,

SRCCopy

);

SelectObject (HMEMDC1, HoldBMP1);

SelectObject (HMEMDC2, HoldBMP2);

Iconfo16x16.ficon = true;

H16X16CION = CREATEICONIIRECT (& iconInfo16x16);

DeleteObject (iconInfo32x32.hbmcolor);

DeleteObject (iconInfo16x16.hbmcolor);

DeleteObject (iconInfo32x32.hbmmask);

DeleteObject (iconInfo16x16.hbmmask);

Deletedc (HMEMDC1);

Deletedc (HMEMDC2);

:: ReleaseDC (M_HWND, HMAINDC);

Return h16x16icon;

}

15. How to build a grayscale icon

Hicon Creategrayscaleicon (HiCon Hicon)

{

Hicon hgrayicon = null;

HDC hmaindc = null,

hmemdc1 = null,

HMEMDC2 = NULL;

Bitmap BMP;

Hbitmap HoldBMP1 = NULL,

HoldBMP2 = NULL;

IconInfo CSII, CSGRAYII;

Bool BretValue = FALSE;

BretValue = :: GeticonInfo (Hicon, & CSII);

IF (BretValue == false) Return NULL;

HmainDC = :: getDC (m_hwnd);

HMEMDC1 = :: CreateCompatibleDC (Hmaindc);

HMEMDC2 = :: CreateCompatibleDC (Hmaindc);

IF (hmaindc == null ||

HMEMDC1 == Null ||

HMEMDC2 == NULL)

Return NULL;

IF (:: getObject (csii.hbmcolor,

SizeOf (Bitmap), &

AMP; BMP))

{

CSGRAYI.HBMCOLOR =

:: CreateBitmap (CSII.XHOTSPOT * 2,

CSII.YHOTSPOT * 2,

Bmp.bmplanes,

Bmp.Bmbitspixel, NULL);

IF (csgrayi.hbmcolor)

{

HoldBMP1 =

(Hbitmap) :: SelectObject (hmemdc1,

CSIi.hbmcolor);

HoldBMP2 =

(Hbitmap) :: SelectObject (hmemdc2,

CSGRAYI.HBMCOLOR);

:: Bitblt (Hmemdc2, 0, 0, CSII.XHOTSPOT * 2,

CSII.YHOTSPOT * 2, HMEMDC1, 0, 0,

Srccopy);

DWORD DWLOOPY = 0, dwloopx = 0;

ColorRef crpixel = 0;

BYTE BYNEWPIXEL = 0;

For (dwloopy = 0; dwloopy

{

For (dwloopx = 0; dwloopx

{

CRPixel = :: getpixel (hmemdc2, dwloopx, dwloopy);

BYNewpixel = (BYTE) (GetRvalue (CRPixel) * 0.299)

(GETGVALUE (CRPIXEL) * 0.587)

GetBValue (CRPIXEL) * 0.114));

IF (crpixel) :: setPixel (HMEMDC2,

Dwloopx,

Dwloopy,

RGB (Bynewpixel,

Bynewpixel,

BYNewpixel);

} // for

} // for

:: SelectObject (HMEMDC1, HOLDBMP1);

:: SelectObject (HMEMDC2, HoldBMP2);

Csgrayii.hbmmask = csii.hbmmask;

CSGRAYII.FICON = TRUE;

Hgrayicon = :: Createicon Indirect (& CSGRAYI);

} // if

:: DeleteObject (csgrayii.hbmcolor);

//:: deleteObject(CSGRAYI.HBMMASK);

} // if

:: DeleteObject (csii.hbmcolor);

:: deleteObject (csii.hbmmask);

:: Deletedc (HMEMDC1);

:: Deletedc (HMEMDC2);

:: ReleaseDC (M_HWND, HMAINDC);

Return hgraphicon;

}

16. How to rotate in the specified angle to display the memory bitmap (usage and Bitblt)

Void Rotblt (HDC DESTDC, INT SRCX1, INT SRCY1, INT SRCX2, INT SRCY2,

HDC SRCDC, Int Destx1, Int Desty1, Int ThetaIndegrees, DWORD MODE

{

Double Theta = THHINDEGREES * (3.14159 / 180);

// Original image original size

Int width = SRCX2 - SRCX1;

INT Height = SRCY2 - SRCY1;

// Original image center point

INT CENTREX = INT (Float (SRCX2 SRCX1) / 2);

INT CENTREY = INT (Float (SRCY2 SRCY1) / 2);

/ / Judgment the rectangular frame that can be rotated in any direction

IF (width> height) height = width;

Else

Width = height;

HDC MEMDC = CREATECOMPATIBLEDC (DESTDC);

Hbitmap membmp = createcompatiblebitmap (destdc, width, height);

Hbitmap obmp = (hbitmap) SelectObject (MEMDC, MEMBMP);

// Memory DC new in central point

INT newcentre = int (float (width) / 2);

// Start rotation

For (int x = srcx1; x <= srcx2; x )

For (int y = srcy1; y <= srcy2; y )

{

ColorRef color (SRCDC, X, Y);

INT NEWX = INT * SIN (THHETA) (Y-Centrey) * COS (Theta));

INT newy = int ((x-centralrex) * cos (ly) - (Y-Centrey) * sin (theeta));

SetPixel (MEMDC, NewX Newcentre, Newy Newcentre, Col);

}

// Copy to the target DC

Bitblt (Destdc, DestX1, Desty1, Width, Height, MEMDC, 0, 0, MODE);

/ / Release memory

SELECTOBJECT (MEMDC, OBMP);

DELETEDC (MEMDC);

DeleteObject (MEMBMP);

}

usage:

Rotblt (DC, 0, 0, 150, 150, MEMDC, 200, 0, 45, SRCCopy);

17. How to copy the specified form to the system shear board in a bitmap

Void cscreensnapdlg :: TOCLIPBOARD_BIO (CWND * WND, BOOL FULLWND)

{

CDC * DC;

IF (FullWnd)

{/ * Cut the entire window * /

DC = New CWINDOWDC (WND);

} / * Scratch the entire window * /

Else

{/ * Only grabborn * /

DC = New CclientDC (WND);

} / * Only capture the client area * /

CDC MEMDC;

MEMDC.CREATECOMPALDC (DC);

CBITMAP BM;

CRECT R;

IF (FullWnd)

WND-> GetWindowRect (& R);

Else

WND-> GetClientRect (& R);

CString S;

WND-> getWindowText (s);

CSIZE SZ (R.Width (), R.HEIGHT ());

Bm.createCompaTibleBitmap (DC, SZ.CX, SZ.CY);

CBITMAP * OLDBM = MEMDC.SELECTOBJECT (& BM);

Memdc.bitblt (0, 0, SZ.CX, SZ.CY, DC, 0, 0, SRCCOPY);

// Call directly OpenClipboard () without WND-> getParent () -> OpenClipboard ();

WND-> OpenClipboard ();

:: EmptyClipboard ();

:: setClipboardData (cf_bitmap, bm.m_hobject);

CloseClipboard ();

/ / Restore the original environment

MEMDC.SELECTOBJECT (OLDBM);

bm.detach ();

DELETE DC;

}

18. How to replace the color value in HBitmap #define colorref2RGB (Color) (Color & 0xFF00) | ((Color >> 16) & 0xFF) /

| ((Color << 16) & 0xff0000)

HBitmap ReplaceColor (Hbitmap HBMP, ColorRef CNEWCOLOR)

{

Hbitmap RetBMP = NULL;

IF (HBMP)

{

HDC bufferdc = createcompatibledc (null); // source bit map DC

IF (bufferdc)

{

SelectObject (bufferDC, HBMP); // Select DC

HDC DirectDC = CreateCompatibleDC (NULL); // Target DC

IF (DirectDC)

{

// Get the source bit map size

Bitmap BM;

GetObject (HBMP, SIZEOF (BM), & BM);

// Initialize BitmapInfo information to use createDibsection

BitmapInfo RGB32BitsBitmapInfo;

ZeromeMory (& rgb32bitsbitmapinfo, sizeof (bitmapinfo);

Rgb32bitsbitmapinfo.bmiheader.bisize = sizeof (bitmapinfoheader);

RGB32BITSBITMAPINFO.BMIHEADER.BIWIDTH = BM.BMWIDTH;

RGB32Bitsbitmapinfo.Bmiheader.biheight = bm.bmheight;

RGB32BitsBitmapinfo.Bmiheader.Biplanes = 1;

RGB32BitsbitMapInfo.Bmiheader.biBitcount = 32;

Uint * ptpixels;

Hbitmap DirectBitMap = CREATEDIBSECTION (DirectDC,

(BitMapInfo *) & rgb32bitsbitmapinfo,

DIB_RGB_COLORS, (Void **) & PTPixels, NULL, 0);

IF (DirectBitMap)

{

HgDiobj previousObject = selectObject (DirectDC, DirectBitmap);

Bitblt (DirectDC, 0, 0, Bm.Bmwidth, BM.Bmheight, Bufferdc, 0,0, Srccopy);

// Converting ColorRef for RGB

ColdColor = ColorRef2RGB (ColdColor);

CNewColor = ColorRef2RGB (CNEWCOLOR);

// Replace color

For (INT I = ((BM.BMWIDTH * BM.BMHEIGHT) -1); i> = 0; I -)

{

IF (PTPixels [I] == ColdColor) PTPixels [i] = CNewcolor;

}

// Modify bitmap DirectBitmap

SelectObject (DirectDC, PreviousObject);

// carry out

RetBMP = DirectBitMap;

}

// Release DC

DELETEDC (DirectDC);

}

// Release DC

Deletedc (BufferDC);

}

}

Return RetBMP;

}

usage:

Hbitmap hbmp2 = loadingbitmap (g_hinstance, makeintresource (idb_samplebitmap); hbitmap hbmp = replacecolor (HBMP2, 0xFF0000, 0x00FF00); // Replace Blue is green

......

DeleteObject (HBMP2);

DeleteObject (HBMP);

19. How to convert and save a bitmap

// ******************************************************** ***************************************

// * Name: DDBTODIB

// * Author: Xu Jing Zhou (jingzhou_xu@163.net)

// * Function: Device related conversion to device is unlocked

// ******************************************************** ***************************************

Handle Cscreensnapdlg :: DDBTODIB (CBITMAP & BITMAP, DWORD DWCompRESSION / * = BI_RGB * /)

{

Bitmap BM;

BitmapInfoheader Bi;

LpbitmapInfoHeader LPBI;

DWORD DWLEN;

Handle HDIB;

Handle Handle;

HDC HDC;

HPALETTE HPAL;

CWindowDC DC (this);

CPALETTE PAL;

// If you support the palette, create it

IF (DC.GETDEVICECAPS (RasterCaps) & rc_palette

{

UINT nsize = sizeof (logpalette) (SIZEOF (PALETTEENTRY) * 256);

Logpalette * PLP = (logpalette *) New byte [nsize];

PLP-> paversion = 0x300;

PLP-> PALNUMENTRIES = (Unsigned Short) GetsystemPaletteentries (DC, 0, 255,

PLP-> PALPALENTRY

PAL.CREATEPALETTTE (PLP);

//freed

DELETE [] PLP;

}

Assert (Bitmap.getsafeHandle ());

/ / Do not support bi_bitfields type

IF (dwcompression == bi_bitfields)

Return NULL;

// If the palette is empty, use the default palette

HPAL = (HPALETTE) PAL.GETSAFEHANDLE ();

IF (hpal == null)

HPAL = (HPALETTE) GetStockObject (default_palette);

// Get bitmap information

Bitmap.GetObject (SizeOf (BM), (LPSTR) & BM);

// Initialization bitmap information head

bi.bisize = sizeof (bitmapinfohead);

Bi.biwidth = bm.bmwidth;

Bi.biheight = bm.bmheight;

Bi.BIPLANES = 1; bi.biBitcount = (unsigned short) (BM.BMPLANES * BM.BMBITSPIXEL);

Bi.Bicompression = dwcompression;

Bi.bisizeImage = 0;

bi.bixpelspermeter = 0;

Bi.biypelspermeter = 0;

Bi.bi.biRused = 0;

Bi.Biclrimportant = 0;

// Calculate the information head and color table size

INT ncolors = 0;

IF (bi.bibitcount <= 8)

{

Ncolors = (1 << bi.biBitcount);

}

Dwlen = bi.bisize ncolors * sizeof (rgbquad);

HDC = :: getDC (null);

HPAL = SELECTPALETTTE (HDC, HPAL, FALSE);

RealizePalette (HDC);

/ / Assign memory for the information head and color table

HDIB = GLOBALLOC (GMEM_FIXED, DWLEN);

IF (! hdib) {

SelectPalette (HDC, HPAL, FALSE);

:: ReleaseDC (NULL, HDC);

Return NULL;

}

LPBI = (LPbitMapInfoHeader) Globalock (HDIB);

* lpbi = bi;

// Call getDibits to calculate the image size

GetDibits (HBitmap) Bitmap.getsafehandle (), 0L, (DWORD) BI.BIHEIGHT,

(Lpbyte) NULL, (LPBITMAPINFO) LPBI, (DWORD) DIB_RGB_COLORS;

Bi = * lpbi;

// Each line of the image is aligned (32bit) boundary

IF (bi.bisizeimage == 0) {

Bi.bisizeImage = (((bi.biwidth * bi.biBitcount) 31) & ~ 31) / 8)

* bi.biheight;

IF (dwcompression! = bi_rgb)

Bi.bisizeImage = (bi.bisizeimage * 3) / 2;

}

/ / Re-allocate the memory size in order to put down all data

DWLEN = bi.bisizeImage;

Handle = GlobalRealloc (HDIB, DWLEN, GMEM_MOVEABLE);

IF (Handle! = NULL)

HDIB = Handle;

Else

{

GlobalFree (HDIB);

// Reset the original palette

SelectPalette (HDC, HPAL, FALSE);

:: ReleaseDC (NULL, HDC);

Return NULL;

}

// Get bitmap data

LPBI = (lpbitmapinfoheader) HDIB;

// finally obtained DIB

Bool bgotbits = getDibits (hbitmap) Bitmap.getsafehandle (),

0L, // Scanning start

(Dword) bi.biheight, // Scan line

(Lpbyte) lpbi // bitmap data address

(bi.bisize ncolors * sizeof (rgbquad)), (lpbitmapinfo) lpbi, // bitgraph information address

(DWORD) DIB_RGB_COLORS); // Color Board Using RGB

IF (! bgotbits)

{

GlobalFree (HDIB);

SelectPalette (HDC, HPAL, FALSE);

:: ReleaseDC (NULL, HDC);

Return NULL;

}

SelectPalette (HDC, HPAL, FALSE);

:: ReleaseDC (NULL, HDC);

Return HDIB;

}

// ******************************************************** ***************************************

// * Name: SaveBitmaptofile

// * Modification: Jingzhou_xu@163.net)

// * Features: Save as bitmap file

// ******************************************************** ***************************************

Bool CscreensnaPdlg :: SaveBitmaptofile (Hbitmap Hbitmap, CString LpfileName)

{

HDC HDC; // Device Description Table

INT iBITS; // Currently display the number of items per pixel at the resolution

Word wbitcount; // number of bytes per pixel in the bitmap

DWORD dwpalettesize = 0, // Defines the sliding board size, bitmap pixel byte size, bitmap file size, write file byte

DWBMBITSSIZE,

DWDIBSIZE, DWWWWRITEN

Bitmap bitmap;

BitmapfileHeader BMFHDR; // bit map attribute structure

BitmapInfoHeader Bi; // bitmap file header structure

LPbitmapInfoHeader lpbi; // bitmap information head structure

Handle FH, HDIB, HPAL, HOLDPAL = NULL; / / Pointing the bitmap information head structure, defining files, assigning memory handles, palette handle

/ / Calculate the number of bytes per pixel per pixel

HDC = CREATEDC ("Display", NULL, NULL, NULL;

Ibits = getDeviceCaps (HDC, Bitspixel) *

GetDeviceCaps (HDC, Plaso);

Deletedc (HDC);

IF (iBITS <= 1)

Wbitcount = 1;

Else IF (iBITS <= 4)

Wbitcount = 4;

ELSE IF (iBITS <= 8)

Wbitcount = 8;

Else IF (iBITS <= 24)

Wbitcount = 24;

/ / Calculate the size of the palette

IF (wbitcount <= 8)

DWPALETTESIZE = (1 << wbit) * sizeof (rgbquad); // Setting the bitmap information head structure

GetObject (HbitMap, Sizeof (Bitmap), (LPSTR) & Bitmap;

bi.bisize = sizeof (bitmapinfohead);

bi.biwidth = bitmap.bmwidth;

bi.biheight = bitmap.bmheight;

Bi.Biplanes = 1;

Bi.biBitcount = WbitCount;

Bi.Bicompression = BI_RGB;

Bi.bisizeImage = 0;

bi.bixpelspermeter = 0;

Bi.biypelspermeter = 0;

Bi.bi.biRused = 0;

Bi.Biclrimportant = 0;

DWBMBITSSIZE = ((bitmap.bmwidth *

Wbitcount 31) / 32) * 4

* Bitmap.bmheight;

/ / Distribute memory for bitmap content

HDIB = GLOBALLOC (GHND, DWBMBITSSIZE

DWPALETESIZE SIZEOF (BitmapInfoHeader);

LPBI = (LPbitMapInfoHeader) Globalock (HDIB);

* lpbi = bi;

// Treat palette

HPAL = getStockObject (Default_palette);

IF (HPAL)

{

HDC = :: getDC (null);

Holdpal = SELECTPALETTE (HDC, HPALETTE) HPAL, FALSE);

RealizePalette (HDC);

}

/ / Get new pixel values ​​under this palette

GetDibits (HDC, Hbitmap, 0, (UINT) Bitmap.Bmheight,

(LPSTR) LPBI SIZEOF (BitMapInfoHeader) DWPALETESIZE,

(Lpbitmapinfo) lpbi, dib_rgb_colors;

// Restore palette

IF (Holdpal)

{

SelectPalette (HPALETTE) HOLDPAL, TRUE;

RealizePalette (HDC);

:: ReleaseDC (NULL, HDC);

}

// Create a bitmap file

FH = CREATEFILE (lpfilename, generic_write,

0, NULL, CREATE_ALWAYS,

File_attribute_normal | file_flag_sequential_scan, null;

IF (fH == invalid_handle_value)

Return False;

/ / Set the bitmap file header

BMFHDR.BFTYPE = 0x4d42; // "BM"

DWDIBSIZE = SizeOf (BitmapfileHeader) Sizeof (BitmapInfoHeader) DWPALETESIZE DWBMBITSSIZE

BMFHDR.BFSIZE = DWDIBSIZE;

BMFHDR.BFRESERVED1 = 0;

BMFHDR.BFRESERVED2 = 0;

BMFHDR.BFOFFBITS = (DWORD) SIZEOF (DWORD) SIZEOF (BitmapInfoHeader)

DWPALETESIZE;

// Write the bitmap file header

Writefile (FH, (LPSTR) & BMFHDR, SIZEOF

(BitmapfileHeader), & dwwritten, null;

/ / Write the rest of the file

Writefile (FH, (LPSTR) LPBI, DWDIBSIZE,

& dwwritten, null;

// Eliminate memory allocation

GlobalUnlock (HDIB);

GlobalFree (HDIB);

CloseHandle (FH);

Return True;

}

20. How to get a local area online computer name and their IP address

l Connect WS2_32.LIB and MPR.LIB libraries

l #include winsock2.h

CString Strtemp;

Struct Hostent * Host;

Struct in_addr * ptr; // Retrieve IP Address

DWORD DWSCOPE = Resource_Context;

NetResource * NetResource = NULL;

Handle Henum;

WNETOpenenum (DWSCope, Null, NULL,

NULL, & Henum

Wsadata wsadata;

WSASTARTUP (Makeword (1, 1), & WSADATA);

IF (henum)

{

DWORD count = 0xfffffffff;

DWORD BUFFERSIZE = 2048;

LPVOID BUFFER = New Char [2048];

WneetenumResource (Henum, & Count,

Buffer, & buffersize;

NetResource = (NetResource *) Buffer;

Char szhostname [200];

Unsigned Int i;

For (i = 0;

i

i , NetResource )

{

IF (NetResource-> dwusage ==

Resourceusage_Container &&

NetResource-> DWTYPE ==

Resourcetype_any

{

IF (NetResource-> LPREMOTENAME)

{

CString strfullname =

NetResource-> LPREMOTENAME;

IF (0 ==

Strfullname.Left (2) .compare (""))

Strfullname =

Strfullname.right

Strfullname.getLength () - 2);

Gethostname (szhostname,

Strlen (szhostname);

Host = gethostbyname (strfullname);

IF (Host == NULL) Continue;

PTR = (struct in_addr *)

Host-> h_addr_list [0];

// =. Separate IP: 211.40.35.76.

INT A = PTR-> S_un.s_un_b.s_b1; // 211

INT B = Ptr-> S_un.s_un_b.s_b2; // 40int c = Ptr-> S_un.s_un_b.s_b3; // 35

INT D = PTR-> S_un.s_un_b.s_b4; // 76

Strtemp.Format ("% s ->% D.% d.% d.% d",

StrfullName, A, B, C, D);

AfxMessageBox (Strtemp);

}

}

}

DELETE BUFFER;

WNETCloseenum (Henum);

}

WSACLEANUP ();

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

New Post(0)