学习VC中所得的点点技术心得6(转)
----------------------------------------------------------
CBitmap* pBitmap = new CBitmap;
pBitmap->LoadBitmap(IDB_HAPPY_BITMAP); //IDB_HAPPY_BITMAP为工程中.bmp资源,大小任意,但颜色超过16色的话色彩会失真
m_editCtrl.CreateCaret(pBitmap); //m_editCtrl为编辑框控件变量
POINT point;
point.x=0;
point.y=0;
m_editCtrl.SetCaretPos(point); //改变光标的位置,以像素为单位
m_editCtrl.ShowCaret();
----------------------------------------------------------
252 用主题函数改变对话框窗口的可视风格(MSDN上可查到更多这方面的函数):
----------------------------------------------------------------
#include <uxtheme.h>
#pragma comment(lib,"uxtheme.lib")
void CMyDlg::OnTest()
{
EnableThemeDialogTexture(m_hWnd,ETDT_ENABLE | ETDT_USETABTEXTURE);
Invalidate();
}
----------------------------------------------------------------
253 如何让MDI程序在启动的时候不新建子窗口:http://community.csdn.net/Expert/topic/4619/4619802.xml?temp=.2809107
在App的InitInstance中加一行:
--------------------------------------------------------
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
cmdInfo.m_nShellCommand=CCommandLineInfo::FileNothing; //添加这一句
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
--------------------------------------------------------
254 关机:ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF, 0 ) <=====还没试
强制关机ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF, 0 );
255 hook:http://community.csdn.net/Expert/topic/4615/4615606.xml?temp=.7596094
256 字符串====>日期时间:
-------------------------------------------------
COleDateTime dt;
dt.ParseDateTime(_T("2006-2-14 8:00:00"));
SYSTEMTIME st={0};
dt.GetAsSystemTime(st);
//SetLocalTime(&st); //设置电脑中时间
-------------------------------------------------
日期时间====>字符串:
-------------------------------------------------
SYSTEMTIME st={0};
st.wYear=2006;
st.wMonth=3;
st.wDay=23;
st.wHour=8;
st.wMinute=30;
st.wSecond=59;
COleDateTime dt(st);
AfxMessageBox(dt.Format());
-------------------------------------------------
257 typeid函数可以判断一个对象的类型,详见MSDN,上面有示例代码,不过有点小问题:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_typeid_operator.asp
258 系统休眠,摘自:http://community.csdn.net/Expert/topic/4637/4637403.xml?temp=.8914301
------------------------------------------------------------------------------------------------
if(MessageBox("确实要休眠吗?","关机程序",MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDYES)
{
static HANDLE hToken;
static TOKEN_PRIVILEGES tp;
static LUID luid;
if(::OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
{
::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
tp.PrivilegeCount=1;
tp.Privileges[0].Luid =luid;
tp.Privileges[0].Attributes =SE_PRIVILEGE_ENABLED;
::AdjustTokenPrivileges(hToken,false,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL);
}
::SetSystemPowerState(false,true);
}
-------------------------------------------------------------------------------------------------
259 在录音时如何控制录音属性中麦克风或者线路输入的音量
1.mixerGetNumDevs获取混音设备数量
2.循环获取设备信息mixerOpen/mixerGetLineInfo
3.根据获得的MIXERLINE中dwComponentType判断类型来确定是线入/麦克风
MIXERLINE_COMPONENTTYPE_SRC_LINE/MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
4.mixerSetControlDetails设置音量
260 用资源管理器打开指定文件夹,并置目标文件夹为选定状态:WinExec("explorer /n,/select,f:\\win98", SW_SHOWNORMAL );
261 打开或关闭输入法:IActiveIME::SetActiveContext
262 提取.exe或.dll文件中的图标:ExtractIcon
263 捕捉其他程序最大、最小化消息:http://community.csdn.net/Expert/topic/4647/4647630.xml?temp=.8477289
264 WM_DEVICECHANGLE消息配合DeviceIoControl函数可禁用U盘
265 判断编辑框控件是否为只读的:
----------------------------------------------------
CEdit *temp=(CEdit *)GetDlgItem(IDC_EDIT1);
DWORD a=temp->GetStyle();
if(a & ES_READONLY) //ES_READONLY=0x800
AfxMessageBox("Read only");
else
AfxMessageBox("NOT read only");
----------------------------------------------------
266 获得系统电源状态:
GetSystemPowerStatus
Retrieves the power status of the system. The status indicates whether the system is running on AC or DC power, whether the battery is currently charging, and how much battery life remains.
267 桌面元素的操作:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_adv/activedesktop.asp
268 如何判断是键盘左边的Shift还是右边的Shift键,摘自:http://community.csdn.net/Expert/topic/4661/4661661.xml?temp=8.190554E-02
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/virtualkeycodes.asp
上面URL中MSDN给出了所有按键的ASCII码。
VK_LSHIFT (A0)
Left SHIFT key
VK_RSHIFT (A1)
Right SHIFT key
269 C++开发常用工具:http://blog.vckbase.com/FlowSand/archive/2006/01/27/17447.html
270 sprintf中打印百分号%的方法,摘自:http://blog.vckbase.com/smileonce/archive/2006/03/16/18521.html
------------------------------------------------------------
int percent = 50;
char buf[100];
sprintf(buf, "%d%%%% complete\n! ", percent);
printf(buf); //这样就只打印一个%了
printf("%s",buf); //改成这样后打印出两个%了,恰如我们期望的
------------------------------------------------------------
271 精确延时,KeStallExecutionProcessor函数来帮你,摘自:http://blog.vckbase.com/zaboli/archive/2006/03/24/18705.html
但是问题是KeStallExecutionProcessor的文档说,他的最小周期不能大于 50微秒!不然会造成系统停顿。。
测试得出 KeStallExecutionProcessor(416)在有的机器上会造成鼠标停顿 -_-但是KeDelayExecutionThread(KernelMode, FALSE, &WaitTime); 无论再怎么设置小的超时,结果都如下10毫秒???差太多了吧。。。
272 向控件发送鼠标滚轮的消息:
::SendMessage(m_cListCtrl.m_hWnd,WM_MOUSEWHEEL,(WPARAM)-7864320,0); //参数三是向下滚动的参数,7864320则为向上滚动
273 数日前收到天影朋友的23种设计模式的例子,很浅显易懂。下面的blog中亦有设计模式的探讨,借古喻今,文笔不错,可供参考:
http://blog.csdn.net/junguo/
274 获得文件夹大小的方法,摘自:http://community.csdn.net/Expert/topic/4672/4672088.xml?temp=.2216761
----------------------------------------------------------------------------------
#include <iostream.h>
#import "scrrun.dll" raw_interfaces_only
int main()
{
CoInitialize(NULL);
{
try
{
Scripting::IFileSystem3Ptr fs;
fs.CreateInstance(__uuidof(Scripting::FileSystemObject));
Scripting::IFolderPtr folder;
fs->GetFolder(_bstr_t("f:\\win98"),&folder);
_variant_t vsize;folder->get_Size(&vsize);
cout<<"f:\\win98 size:"<<(long)vsize<<" bytes"<<endl;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
cout<<"Get directory size via FileSystemObject, by masterz"<<endl;
cout<<"COM error occurred,source:"<<(LPCTSTR)bstrSource<<endl;
cout<<"Description:"<<(LPCTSTR)bstrDescription<<endl;
}
}
CoUninitialize();
return 0;
}
----------------------------------------------------------------------------------
275 打开控制台界面,写点文字在上面:
----------------------------------------------------------------------------------
AllocConsole();//打开控制台窗口
HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);//得到控制台句柄
unsigned long lgsize;
char *strbuf="显示这行信息。";
WriteFile(hOutput,strbuf,strlen(strbuf),&lgsize,0);
FreeConsole();//关闭释放用于输入新信息的控制台窗口
-------------------------------------------------------------------------------