现在的位置: 首页 > 综合 > 正文

VC技巧–02

2012年08月01日 ⁄ 综合 ⁄ 共 10869字 ⁄ 字号 评论关闭

101 怎样取得程序自己占用的内存和CPU占用率:GetProcessMemoryInfo和GetPerformanceInfo
102 如何让你的程序运行在release模式下:build->set active configuration
103 监视文件夹是否被更新:FindFirstChangeNotification、FindNextChangeNotification、FindCloseChangeNotification这三个函数
             范例见:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/obtaining_directory_change_notifications.asp
105 动态菜单:http://community.csdn.net/Expert/topic/4441/4441893.xml?temp=.2887384
http://community.csdn.net/Expert/topic/4506/4506791.xml?temp=.2409326
106 如何获取客户区的中心坐标:http://community.csdn.net/Expert/topic/4449/4449444.xml?temp=8.642215E-02
107 强行操作内存虚拟地址中某个指定地方的内容:
----------------------------------------------
    int *a=(int*)0x00440000;     //这里以访问0x00440000地址为例
cout<<*a<<endl;
----------------------------------------------
108 如何响应条码机:http://community.csdn.net/Expert/topic/4453/4453026.xml?temp=.1966516
     条码扫描仪主要有三种接口: 1.RS232 2.共用接盘接口 3.USB外设. 对于RS232,需要编程来监视和读取条码; 对于共用接盘接口,条码信息被转换成相应的键盘消息,具有输入焦点的应用程序会收到键盘输入消息,我们以前的做法是做一个全局keyboard Hook或应用程序级别上 keyboard hook, 监视键盘消息,当有连续的键盘消息(在很短的时间内),并且这些键盘字符能构成完成的条码信息,就产生一条自定义消息,通知窗口(向监视程序注册的窗口)条码信息到达,条码机只是相当于一个键盘,所以你也可以在界面上放一个edit框,条码机读出条码后还会在字符串后面加一个回车(这个一般是可设置的,可加可不加),如果条码机自动加回车,则你重写OnOK函数,将edit框的内容取出放入list即可。

当然也可不放edit框,而直接接收键盘字符(比如重写OnChar函数等,方法很多),但要考虑到这种情况:条码读不出来的情况,此时应该用手动输入条码,所以还是放一个edit框为好。

109 检查指定文件夹是否存在:PathIsDirectory()
方法一:
检查给定路径是否根目录:BOOL PathIsRoot(LPCTSTR pPath);
说明:Returns TRUE for paths such as “\”, “ X:\”, “\\ server\ share”, or “\\ server\”。Paths such as “..\path2” will return FALSE.
     用这两个函数要先:#include <shlwapi.h>;
         再把这个文件加入工程:shlwapi.lib
方法二:
GetFileAttributes检查文件是否存在,并且检查是否文件夹属性FILE_ATTRIBUTE_DIRECTORY
----------------------------------------------------------
DWORD = GetFileAttributes(_T("f:\\win98"));
if(dwAttr != 0xFFFFFFFF && (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
cout<<"exist"<<endl;
else
cout<<"NOT exist"<<endl;
----------------------------------------------------------
方法三:
用下面第113条的_access函数同样可以
----------------------------------------------------------
if(_access("f:\\win98",0)!=-1)
cout<<"exist"<<endl;
else
cout<<"NOT exist"<<endl;
----------------------------------------------------------
方法四:
用PathFileExists函数,见MSDN介绍,需要的条件同方法一。
还有一个:BOOL SHGetPathFromIDList(LPCITEMIDLIST pidl,LPTSTR pszPath);
  Converts an item identifier list to a file system path.
110 去掉单文档标题栏上的“无标题—”:http://community.csdn.net/Expert/topic/4454/4454093.xml?temp=.2896997
111 打开显示器: ::SendMessage(GetSafeHwnd(), WM_SYSCOMMAND, SC_MONITORPOWER, -1); //从bobob的blog上抄来的^_^
   关闭显示器: ::SendMessage(GetSafeHwnd(), WM_SYSCOMMAND, SC_MONITORPOWER, 1); //从bobob的blog上抄来的^_^
   得到它的工作状态:
休眠状态是指用SendMessage(Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1)关闭的
--------------------------------------------------------------------------------
The GetDevicePowerState function is supposed to retrieve the current power state of the specified device. However, Apps may fail to use GetDevicePowerState on the display, as they can't get a handle on "\\.\Display#", while the # index is 1-based, or "\\.\LCD", for security reasons.
If you are trying to do this on Windows XP, then you can use SetupDiGetDeviceRegistryProperty and Property: SPDRP_DEVICE_POWER_DATA to get the power management information. This is documented in the Windows XP DDK.
The WMI Class Win32_DesktopMonitor does not report the power state. use SPI_GETPOWEROFFACTIVE or DeviceIOControl with IOCTL_VIDEO_GET_POWER_MANAGEMENT will simply reports power management is enabled or not. SPI_GETPOWEROFFACTIVE just determines whether the power-off phase of screen saving is enabled or not.
BTW, you can always use the SetThreadExecutionState or other APIs (you have used) to switch ON the monitor no matter the monitor is in the ON or OFF state.
References
http://msdn.microsoft.com/library/en-us/Display_r/hh/Display_r/VideoMiniport_Functions_b47b2224-5e0b-44af-9d04-107ff1299381.xml.asp
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_desktopmonitor.asp
112 得到系统时间、语言等的设置
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITIME, lpLCData, cchData); //从bobob的blog上抄来的^_^
113 文件是否存在(记得先包含头文件#include <io.h>)
----------------------------------
if(_access("c:\\somefile.txt",0)!=-1)
//存在
else
//不存在
---------------------------------
还有一个方法:
---------------------------------------------
if(GetFileAttributes("f:\\test.txt")!=0xFFFFFFFF)
{
//存在
}
else
{
//不存在
}
---------------------------------------------
114 得到剪贴板数据
-------------------------------------------------
if ( OpenClipboard() )                      
{
HANDLE hData = GetClipboardData(CF_TEXT);
char * buffer = (char*)GlobalLock(hData);     //剪贴板中的文本内容保存在buffer中
GlobalUnlock(hData);
CloseClipboard();
}
----------------------------------------------
115 在CStatic上面关联图片
----------------------------------------------
CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC);
pWnd->ModifyStyle(0, SS_BITMAP);
pWnd->SetBitmap((HBITMAP)::LoadImage(0,
"c:\\zzzzz.bmp",                        //只能显示.bmp文件
IMAGE_BITMAP,
0,0,LR_CREATEDIBSECTION |LR_DEFAULTSIZE |LR_LOADFROMFILE));
----------------------------------------------
116 显示一个打开文件夹的对话框,并得到用户选择的目录:
-------------------------------------------
char szDir[MAX_PATH];
BROWSEINFO bi;
ITEMIDLIST *pidl;
bi.hwndOwner = this->m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szDir;
bi.lpszTitle = "请选择目录";//strDlgTitle;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;

pidl = SHBrowseForFolder(&bi);
if(pidl == NULL)
return;
if(!SHGetPathFromIDList(pidl, szDir))
return;
AfxMessageBox(szDir);    //szDir中存放的内容为用户选定的目录
------------------------------------------------
117 去除字符串中指定的字符:
-----------------------------------------
CString strtemp;
strtemp.Format("%s","abc\n123\ndef");
strtemp.Remove('\n');     //这里以去除换行符为例,结果保存在strtemp中了
-------------------------------------------
118 有关数据结构的地址:http://student.zjzk.cn/course_ware/data_structure/web/main.htm
119 假如当前时间2005-09-09,如何计算在该时间前12345天,是哪年哪月哪日?
---------------------------
CTime tm(2005,9,9,0,0,0);
tm-=86400*12345;
cout<<tm.Format("%Y-%m-%d")<<endl;
----------------------------
120 PeekMessage是干什么用的: http://community.csdn.net/Expert/topic/4462/4462828.xml?temp=.8852045
121 拖动控件时实现类似windows拖动窗口的效果:CRectTracker
    Mackz朋友的blog中有它的范例:http://blog.csdn.net/Mackz/archive/2005/10/27/517747.aspx
122 有关UNICODE、ANSI字符集和相关字符串操作的总结:http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=199372
123 寻找系统中的打印机:EnumPrinters              
124 用代码加入外部模块的方法:#pragma comment(lib,"mylib.lib")
125 判断指定点是否在一个矩形框内:CRect::PtInRect(POINT point)
126 winAPI 函数GetTextExtentPoint32()可以得出一个以像素为单位的字符串的宽度。
127 RGB转换成YV12(YUV 4:2:0)的方法:http://www.fourcc.org/fccyvrgb.php
128 获得指定进程占用内存的情况,用GetProcessMemoryInfo()函数。
129 把CONSOLE程序的输出导入到文件中,用程序控制:http://community.csdn.net/Expert/topic/4403/4403431.xml?temp=.7469599
                                                 http://www.codeproject.com/dialog/quickwin.asp
130 把CRichEditCtrl中的文字保存到rtf文件:http://community.csdn.net/Expert/topic/4478/4478640.xml?temp=.1313135    
   在codeproject上还有从CRichEditCtrl类派生新类的,功能增强了很多:http://www.codeproject.com/richedit/autoricheditctrl.asp
   还有一个开发类似写字板那样程序的完整范例:http://www.codeproject.com/tools/simplewordpad.asp
131 MFC中使用ATL字符转换宏:在你的函数开关加上USES_CONVERSION;语句,详见MSDN或这里:http://community.csdn.net/Expert/topic/4479/4479609.xml?temp=.6256983
132 如何建立共享目录:直接调用标准的Win32API函数NetShareAdd和NetShareDel
   详见MSDN及:http://community.csdn.net/Expert/topic/4481/4481371.xml?temp=.4405023
133 位图文件读写基础:http://www.vckbase.com/document/viewdoc/?id=674
134 用VC实现支持多语言的程序:http://www.vckbase.com/document/viewdoc/?id=1102                  //还没试过,以后用到了再仔细研究吧
135 Menu系列函数:
     GetMenu
     GetMenuInfo
     GetMenuItemCount
     GetMenuItemID
     GetMenuString
     EnableMenuItem
     CheckMenuItem
     ModifyMenu
     RemoveMenu
     InsertMenu
     GetSystemMenu
     ::LoadMenu
     ::SetMenu
136 得到SYSTEMMENU(系统菜单)的高度:GetSystemMetrics(SM_CYMENU);
   得到当前屏幕分辨率:
GetSystemMetrics(SM_CXFULLSCREEN);      //得x值(如1024)
GetSystemMetrics(SM_CYFULLSCREEN);      //得y值( 如768-任务栏高度)
     此外这个函数还可以得到很多别的系统设置值,详见MSDN:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemmetrics.asp
137 在属性页控件的标签上加图片:http://www.codeguru.com/cpp/controls/propertysheet/article.php/c611/
    http://community.csdn.net/Expert/topic/4492/4492593.xml?temp=.9977991
--------------------------------------------------
BOOL CMyPropSheet::OnInitDialog()
{
BOOL bResult = CPropertySheet::OnInitDialog();
m_imageTab.Create( IDB_TABIMAGES, 13, 1, RGB(255,255,255) );
CTabCtrl *pTab = GetTabControl();
pTab->SetImageList( &m_imageTab );

TC_ITEM tcItem;
tcItem.mask = TCIF_IMAGE;
for( int i = 0; i < 3; i++ )
{
tcItem.iImage = i;
pTab->SetItem( i, &tcItem );
}
return bResult;
}
----------------------------------------------------
138 这里有很多CRichEditCtrl控件的高级用法:http://www.codeguru.com/Cpp/controls/richedit/
139 CGridCtrl控件的一些应用:http://www.vckbase.com/code/listcode.asp?mclsid=3&sclsid=327
140 一个不错的地方:http://www.vckbase.com/document/listdoc.asp?mclsid=3&sclsid=323
141 怎样使右键菜单也能变灰、打勾:http://community.csdn.net/Expert/topic/4501/4501123.xml?temp=.4074823
142 MD5加密解密的API:http://community.csdn.net/Expert/topic/4502/4502325.xml?temp=.6894647
MD5Init
MD5Final
MD5Update
143 用CBrush::CreateStockObject(HOLLOW_BRUSH);或CBrush::CreateStockObject(NULL_BRUSH);这两个可以创建镂空的画刷。
144 创建不规则窗体:http://www.vckbase.com/document/viewdoc/?id=1345
145 拖动一个没有标题栏的窗体:http://community.csdn.net/Expert/topic/4499/4499796.xml?temp=6.763858E-02
146 屏蔽浏览器中的弹出广告,下面地址中是在ATL中采用BHO服务的方法:http://www.codeproject.com/atl/popupblocker2.asp
CSDN上有个讨论贴:http://community.csdn.net/Expert/topic/4496/4496918.xml?temp=.157284
147 远程线程注入:
http://www.codeproject.com/threads/RmThread.asp
http://www.codeproject.com/library/InjLib.asp
http://www.codeproject.com/win32/Remote.asp
148 CEditView中两个函数的用法:
GetEditCtrl().ModifyStyle (0 , WS_VSCROLL |ES_AUTOHSCROLL   |ES_AUTOVSCROLL |WS_HSCROLL |ES_WANTRETURN | ES_MULTILINE);
GetEditCtrl().GetSel(m_nStartChar, m_nEndChar ) ;
149 给对话框窗体底部加上状态条,把下面代码加进对话框的OnInitDialog()函数中:
-----------------------------------------------------------
static UINT indicators[] =
{
ID_SEPARATOR,           // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
//下面m_wndStatusBar必须先在对话框类中声明:CStatusBar m_wndStatusBar;
if (!m_wndStatusBar.Create(this,WS_CHILD|WS_VISIBLE|WS_BORDER)||
!m_wndStatusBar.SetIndicators(indicators,
    sizeof(indicators)/sizeof(UINT)))
{
         AfxMessageBox("Status Bar not created!", NULL, MB_OK );
  
}
CRect rect;
this->GetWindowRect(&rect);
m_wndStatusBar.MoveWindow(2,rect.bottom-GetSystemMetrics(SM_CYSIZE)-27,rect.Width()-4,20);
m_wndStatusBar.ShowWindow(SW_SHOW);
m_wndStatusBar.SetWindowText("Ready");
-----------------------------------------------------------
把上面代码加在OnInitDialog里,注意:m_wndStatusBar变量的声明语句CStatusBar m_wndStatusBar;要放在你的对话框类C****Dlg中才行。
150 从注册表中读取cpu的频率:
读取 ~MHz 这个键值 ,记住那键前有个~别忘了,在注册表的HKEY_LOCAL_MACHINE\\Hardware\\Description\\System\\CentralProcessor\\0 //0 为第一个cpu
            Hardware\\Description\\System\\CentralProcessor\\1 //1 为第二个cpu
  得到cpu的数量,摘自:http://community.csdn.net/Expert/topic/4635/4635246.xml?temp=.7182123
-----------------------------------------------------------------------
SYSTEM_INFO siSysInfo;
GetSystemInfo(&siSysInfo);
printf(" Number of processors: %u\n", siSysInfo.dwNumberOfProcessors);
-----------------------------------------------------------------------

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/snow_ice11111/archive/2006/04/10/552696.aspx

抱歉!评论已关闭.