20 kinds of programming techniques and methods in VC programming20 kinds of programming techniques and methods in VC programming
1. How to activate the current screen saver
2. How to disable/Enable screen saver and power management
3. How to activate and deactivate IE browser
4. How to add a ToolTip to a tree control
5. How to get the path to the System Information box
6. How to run a program directly from a resource
7. How to traverse the entire directory
8. How to disable/enable the system hotkey
9. How to Hide/show the Windows system taskbar
10. How to animate windows to system area icons
11. How to determine the current operating system version
12. How to display multiple lines of text horizontally/vertically within a specified rectangular box
13. How to rotate the display text in the specified rectangle
14. How to convert the x 32 pixel icon to an icon with a value of 16 pixels
15. How to create a gray level icon
16. How to rotate the display memory bitmap by the specified angle (usage and bitblt are similar)
17. How to copy the specified form to the system Clipboard as a bitmap
18. How to replace the color values in the HBITMAP
19. How to convert and save bitmaps
20. How to obtain the computer name and their IP address on the LAN
1. How to activate the current screen saver
Activating the current screen saver, Jingzhou Xu
PostMessage (wm_syscommand,sc_screensave,0);
2. How to disable/Enable screen saver and power management
Static UINT dss_getlist[] = {spi_getlowpowertimeout, spi_getpowerofftimeout, S
Pi_getscreensavetimeout};
Static UINT dss_setlist[] = {spi_setlowpowertimeout, spi_setpowerofftimeout, S
Pi_setscreensavetimeout};
static const int dss_listcount = _countof (dss_getlist);
• Disable screen protection and power management
{
M_pvalue = new Int[dss_listcount];
for (int x=0;x<dss_listcount;x++)
{
Disable screen saver and power management
VERIFY (SystemParametersInfo (Dss_setlist[x], 0, NULL, 0));
}
Delete[] M_pvalue;
}
• Enable screen saver and power management
{
M_pvalue = new Int[dss_listcount];
for (int x=0;x<dss_listcount;x++)
{
Enable screen saver and power management
VERIFY (SystemParametersInfo (dss_setlist[x], m_pvalue[x], NULL, 0));
}
Delete[] M_pvalue;
}
3. How to activate and deactivate IE browser
Activating and opening IE
void Lounchie ()
{
HWND H=findwindowex (Null,null,null,
"Microsoft Internet Explorer");
ShellExecute (H, "open", "C:\simple.html",
Null,null,sw_shownormal);
}
Close 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 add a ToolTip to a tree control
L First add the Tvs_infotip attribute style to the tree control as follows:
if (!m_ctrltree.create (ws_child| ws_visible|
tvs_haslines| tvs_hasbuttons| Tvs_linesatroot| tvs_showselalways| Tvs_infotip,
Join hint 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 Next Add the map message declaration as follows:
afx_msg void Ongetinfotip (nmhdr* pnmhdr,lresult* pResult); Join on tree control
Hint message, Jingzhou Xu
On_notify (Tvn_getinfotip, Ongetinfotip)//tree control entry Plus
Hint, Jingzhou Xu
L finally add the echo number processing:
void Ccreatetreedlg::ongetinfotip (nmhdr* pnmhdr,
lresult* PResult)
{
*presult = 0;
nmtvgetinfotip* Ptvtipinfo = (nmtvgetinfotip*) pnmhdr;
LPARAM ItemData = (DWORD) ptvtipinfo->lparam;
Data corresponding to each entry
Htreeitem HItem = ptvtipinfo->hitem;
CString tip;
Htreeitem Hrootitem = M_chassistree.getrootitem ();
if (hrootitem! = Ptvtipinfo->hitem)
{
tip = "Hints for tree nodes";
}
Else
{
tip = "hints on roots";
}
strcpy (Ptvtipinfo->psztext, (LPCTSTR) tip);
}
5. How to get the path to the System Information box
#include <atlbase.h>
#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);
Find 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, &d
Wsize);
#else
Nret = Reg. QueryValue (Pszpath, Ids_reg_val_msinfo_path1, &dwsize
);
#endif
Reg. Close ();
}
//If the first search fails, a second search is performed
if (nret! = Error_ SUCCESS)
{
nret = Reg. Open (HKEY_LOCAL_MACHINE, IDS_REG_KEY_MSINFO_PATH2, Key_rea
D);
if (nret = = ERROR_SUCCESS)
& nbsp {
#if (_mfc_ver >= 0x0700)
Reg. Querystringvalue (Ids_reg_val_msinfo_path2, Pszpath, &dwsi
Ze);
#else
Reg. QueryValue (Pszpath, Ids_reg_val_msinfo_path2, &dwsize);
#endif
Path name does not include EXE file name
if (nret = = ERROR_SUCCESS)
VERIFY (::P athappend (Pszpath, ids_msinfo_exe_name));
Reg. Close ();
}
}
Strpath.releasebuffer ();
Strpath.freeextra ();
Check that the file is valid.
return::P athfileexists (strpath);
}
6. How to run a program directly from a resource
BOOL Run ()
{
CFile F;
char* pfilename = "Execution.exe";
if (!f.open (Pfilename, Cfile::modecreate | Cfile::modewrite, NULL))
{
AfxMessageBox ("Can not create file!");
return 0;
}
CString path = F.getfilepath ();
Hglobal hres;
HRSRC Hresinfo;
Get an App instance
HInstance Insapp = AfxGetInstanceHandle ();
Find EXE Resource Name
Hresinfo = FindResource (Insapp, (LPCSTR) Idr_exe4, "EXE");
hres = LoadResource (Insapp,hresinfo); Load it
DWORD dfilelength = Sizeofresource (Insapp, hresinfo); Calculate EXE file size
F.writehuge ((LPSTR) hres,dfilelength); Write Temporary files
F.close ();
HInstance HINSSD = ShellExecute (null, "open", path, NULL, NULL, Sw_shownorm
AL);>//Run it.
return 1;
}
7. How to traverse the entire directory
#include <windows.h>
#include <shlobj.h>
Browse the directory.
void Browsefolder (void)
{
TCHAR Path[max_path];
Browseinfo bi = {0};
Bi.lpsztitle = ("Recursive Invocation of all Directories");
Lpitemidlist pidl = SHBrowseForFolder (&BI);
if (pidl! = 0)
{
Get directory path
SHGetPathFromIDList (pidl, path);
Set to current path
SetCurrentDirectory (path);
Search All subdirectories
SearchFolder (path);
Freeing memory
IMalloc * IMalloc = 0;
if (SUCCEEDED (Shgetmalloc (&imalloc)))
{
Imalloc->free (PIDL);
Imalloc->release ();
}
}
Search for all subdirectories and files under it.
void SearchFolder (TCHAR * path)
{
Win32_find_data Findfiledata;
HANDLE Hfind;
TCHAR filename[MAX_PATH + 256];
TCHAR pathbak[MAX_PATH];
Copy initial User Selection directory
strcpy (Pathbak, path);
Find 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 (! (strcmp (Findfiledata.cfilename, ".")) || ! (strcmp
(Findfiledata.cfilename, "...")))
{
Continue
}
Restore initial User Selection directory
strcpy (path, Pathbak);
List all discovered files
sprintf (Path, "%s\%s", Path, findfiledata.cfilename);
If the setcurrentdirectory succeeds, then it is a directory, recursively calling the following
Continue to search sub-directories
if ((setcurrentdirectory (path)))
{
SearchFolder (path);
}
Insert file and path name into list box M_listbox_hwnd
SendMessage (M_listbox_hwnd, lb_addstring, 0, path); <--
INSERT What's want done here!
}
}
while (FindNextFile (Hfind, &findfiledata) && hfind! = Invalid_hand
Le_value);
FindClose (hfind);
}
8. How to disable/enable the system hotkey
BOOL BOld;
L Disable System Hotkey
Shield off System keys
SystemParametersInfo (Spi_setscreensaverrunning,true,&bold,spif_updateinifile);
L Enable System hotkeys
Restore System Hotkey
SystemParametersInfo (Spi_setscreensaverrunning,false,&bold,spif_updateinifile)
;
9. How to Hide/show the Windows system taskbar
L Hide System taskbar
Hide Windows system taskbar
:: ShowWindow (:: FindWindow ("Shell_traywnd", NULL), sw_hide);
L Display System taskbar
Restore Windows system taskbar normal display
:: ShowWindow (:: FindWindow ("Shell_traywnd", NULL), sw_show);
10. How to animate windows to system area icons
//****************************************************************************
****
* Name: Findtraywnd
* Author: Xu Jingzhou (jingzhou_xu@163.net)
* Function: Look for the system area position before displaying the form animation effect
//****************************************************************************
****
BOOL CALLBACK Findtraywnd (HWND hwnd, LPARAM LPARAM)
{
TCHAR szclassname[256];
GetClassName (hwnd, szclassname, 255);
Compare window class names
if (_tcscmp (Szclassname, _t ("traynotifywnd") = = = 0)
{
CRect *prect = (crect*) LParam;
:: GetWindowRect (hwnd, prect);
return TRUE;
}
When the clock window is found, it means that it can end.
if (_tcscmp (Szclassname, _t ("trayclockwclass") = = = 0)
{
CRect *prect = (crect*) LParam;
CRect Rectclock;
:: GetWindowRect (hwnd, rectclock);
Prect->right = Rectclock.left;
return FALSE;
}
return TRUE;
}
//****************************************************************************
****
* Name: Winanimation
* Author: Xu Jingzhou (jingzhou_xu@163.net)
* Function: Display the number of the window animation effect
//****************************************************************************
****
void Cscreensnapdlg::winanimation (BOOL showflag)
{
CRect rect (0,0,0,0);
Find a pallet window
cwnd* pWnd = FindWindow ("Shell_traywnd", NULL);
if (pWnd)
{
Pwnd->getwindowrect (rect);
Enumchildwindows (Pwnd->m_hwnd, Findtraywnd, (LPARAM) &rect);
Rect is a tray rectangle
CRect Rcwnd;
GetWindowRect (Rcwnd);
if (showflag)//form slide to System area
DrawAnimatedRects (GetSafeHwnd (), idani_caption,rcwnd,rect);
else//form slides out of the system area
DrawAnimatedRects (GetSafeHwnd (), Idani_caption,rect,rcwnd);
}
}
Use the following:
if (iswindowvisible ())//form is hidden
{
ShowWindow (Sw_hide); Hide the form first
Winanimation (TRUE); Form animations slide into the system area
}
Else
{
Winanimation (FALSE); Form animations slide out of the system area
ShowWindow (Sw_show);
}
11. How to determine the current operating system version
//----------------------------------------------------------------------------
--------------------
Determine operating system number and variables, Jingzhou Xu
typedef enum tagwin32systype{