有的选项可能仅提供C++和Windows Platform SDK中的方法(API):
(1)将窗口设为顶层窗口:
c++:
//[注意]:BringWindowToTop( hwnd )不会使窗口成为TopMost窗口!
SetWindowPos(hDlg,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE); //忽略x,y,cx,cy参数
c#:
form1.TopMost=true;
(2)设置窗口透明度:
//将窗口设置为图层窗口样式
SetWindowLong(hDlg,GWL_EXSTYLE, GetWindowLong(hDlg, GWL_EXSTYLE) | WS_EX_LAYERED);
//设置alpha值,从0~255
BYTE alpha=200;
SetLayeredWindowAttributes(hDlg,0, alpha, LWA_ALPHA);
(3)已知一个窗口句柄,获取窗口所在程序的HINSTANCE:
HINSTANCE hInstance = (HINSTANCE)GetWindowLong(m_hParentWnd, GWL_HINSTANCE);
(4)获取光标位置:
BOOL GetCursorPos(LPPOINT
lpPoint);
(5)使对话框中的某个TextBox(或其他控件)具有输入焦点:
HWND SetFocus(HWND
hWnd);
如果该方法不能工作,则可以模拟发送一个鼠标点击的消息给该控件:
PostMessage(hWnd, WM_LBUTTONDOWN, 0, 0);//表示于客户区坐标(0,0)处单击
PostMessage(hWnd, WM_LBUTTONUP, 0, 0);
(6)使对话框定时关闭:
在对话框窗口过程中,在对话框初始化消息分支中安装和设定一个定时器,在WM_TIMER消息分支中调用EndDialog。
(7)使一个菜单灰化或者重新使能:
设置:BOOL EnableMenuItem(HMENU hMenu, UINT uIDEnableItem, UINT uEnable);
查询:UINT GetMenuState(HMENU hMenu, UINT uId, UINT uFlags);
(8)获取对话框中的CheckBox是否被选中,设置CheckBox的状态:
查询:if ( IsDlgButtonChecked(hDlg, nIDButton) == BST_CHECKED) ...
设置:BOOL CheckDlgButton(HWND
hDlg, int
nIDButton, UINT
uCheck);
(9)获取对话框中的文本输入框填写的数字:
UINT GetDlgItemInt(
HWND
hDlg,
int
nIDDlgItem,
BOOL *
lpTranslated, //告知调用方是否转换成功
BOOL
bSigned //是否需要考虑负号。
);
获取对话框控件的文本:
UINT GetDlgItemText(
HWND
hDlg,
int
nIDDlgItem,
LPTSTR
lpString,
int
nMaxCount
);
(10)如何处理对话框中的TreeView控件的选择节点变化,节点展开,节点折叠等事件:
对话框中的控件上发生事件时,通常会给它们的父窗口发送WM_NOTIFY消息。
LRESULT OnNotify(WORD uMsg, WORD wParam, LONG lParam, BOOL& bHandled)
{
LPNMHDR hdr=(LPNMHDR)lParam;
/*
code
Notification code. This member can be a control-specific notification code or it
can be one of the common notification codes.
*/
if(hdr->code==TVN_SELCHANGED)
{
//树节点发生改变
}
else if(hdr->code==TVN_ITEMEXPANDED)
{
LPNMTREEVIEW pnmtv = (LPNMTREEVIEW) lParam;
/*
#define TVE_COLLAPSE 0x0001
#define TVE_EXPAND 0x0002
#define TVE_TOGGLE 0x0003
*/
}
return 0;
}
(11)如果计算SYSTEMTIME加上一段时间后的SYSTEMTIME:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code_AddSeconds
typedef union
{
FILETIME fileTime; //文件时间
ULONGLONG uint64; //64-bit unsigned integer.
} UNION_FILETIME;
//为一个系统时间增加指定的秒数
BOOL AddSeconds(CONST SYSTEMTIME *lpSrcTime, SYSTEMTIME *lpDestTime, int seconds)
{
UNION_FILETIME uFileTime;
if(! SystemTimeToFileTime(lpSrcTime, (LPFILETIME)&uFileTime))
return FALSE;
//在文件时间上加上指定的分钟(转化为。。) 文件时间的单位0.0000001 sec, (10^(-7) sec)
//判断分钟的符号
if(seconds>0)
uFileTime.uint64 += UInt32x32To64(seconds,10000000);
else
uFileTime.uint64 -= UInt32x32To64(-seconds,10000000);
return FileTimeToSystemTime((FILETIME*)&uFileTime, lpDestTime);
}
(12)获取用户最近一次鼠标键盘输入到现在的时间(用户离开电脑的时间):
LASTINPUTINFO lastInput;
lastInput.cbSize = sizeof(lastInput); //important,donot forget!
GetLastInputInfo(&lastInput);
DWORD dwTicksSinceLastInput = GetTickCount() - lastInput.dwTime; (单位:毫秒)
备注:GetTickCount:获取自开机到现在的毫秒数。
(13)防止一个程序运行多个进程实例:通过创建mutex对象来判断当前是否已经有进程在运行;
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code_Mutex
//打开mutex,参数:request full access,handle not inheritable,object name
hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, WINLOCK_MUTEX_NAME);
if(hMutex == NULL)
{
//说明系统中尚无本进程,进程退出后系统自动销毁该对象
//创建mutex,参数:no security descriptor,mutex not owned,object name
hMutex = CreateMutex(NULL, FALSE, WINLOCK_MUTEX_NAME);
}
else
{
//退出
exit(0);
}
(14)控制窗口是否在任务栏显示一个按钮:
a. 通过设定对话框的样式实现:
显示:创建一个没有OWNER的窗口,并设置WS_EX_APPWINDOW 扩展样式;
不显示:创建一个没有OWNER的窗口,并设置WS_EX_TOOLWINDOW 扩展样式;
b. 通过任务栏的COM对象来实现:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code_TaskbarList
HRESULT result;
ITaskbarList* pTaskbarList;
result = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
IID_ITaskbarList, (void**)&pTaskbarList);
pTaskbarList->HrInit();
if(bShowInTaskbar) //在任务栏显示按钮
{
pTaskbarList->AddTab(hWnd);
}
else //在任务栏隐藏按钮
{
pTaskbarList->DeleteTab(hWnd);
}
pTaskbarList->Release();
(15)如何在通知栏(System Notify Area)放置一个图标(NotifyIcon):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code_Shell_NotifyIcon
NOTIFYICONDATA m_NotifyData;
HICON hicon = LoadIcon(hInstance,MAKEINTSOURCE(IDI_TRAYICONID));
m_NotifyData.cbSize = sizeof(NOTIFYICONDATA );//字节大小
m_NotifyData.hIcon = hicon;//图标句柄
m_NotifyData.hWnd = hwnd;//窗口句柄
m_NotifyData.uID = 0;//图标ID
#define WM_TRAYICON (WM_USER+120)
m_NotifyData.uCallbackMessage = WM_TRAYICON; //产生的消息
strcpy(m_NotifyData.szTip, "Icon's Tip Text");//鼠标在图标上悬停时的ToolTip Text
m_NotifyData.uFlags = NIF_MESSAGE|NIF_TIP|NIF_ICON; //设置要修改哪些属性
Shell_NotifyIcon(NIM_ADD, (PNOTIFYICONDATA)&m_NotifyData);//添加
(16)如何关闭或者注销计算机:
用户进程启动以后通常不具有关机权限,因此要关闭计算机,首先需要调整我们的进程的权限,获取关机权限:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code_ExitWindowsEx
//关闭计算机
LRESULT ShutDown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
HANDLE hProcess;
int err;
LPVOID lpMsgBuf;
//当前进程的句柄
hProcess=GetCurrentProcess();
//获取调整权限和查询权限的token
if (OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
// open and check the privileges for to perform the actions
// #define SE_SHUTDOWN_NAME TEXT("SeShutdownPrivilege")
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
if(AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0))
{
//adjust the privilege to perform the action,
//现在我们的进程已经具有了权限!
//关机
ExitWindowsEx(EWX_SHUTDOWN|EWX_POWEROFF|EWX_FORCE,0);
//注销
//ExitWindowsEx(EWX_LOGOFF|EWX_FORCE,0);
//重启
//ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0);
}
else
MessageBox(NULL,"无法关机","HanTing Hotels",MB_OK);
}
err = GetLastError();
if(err)
{
// 将错误代码转换成文本
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, //dwFlags
NULL, //lpSource
err, //dwMessageId
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //dwLanguageId
(LPTSTR)&lpMsgBuf, //lpBuffer
0, //nSize
NULL //Arguments
);
MessageBox(NULL,(LPTSTR)lpMsgBuf, "", MB_OK|MB_ICONERROR);
LocalFree(lpMsgBuf);
}
else
{
//没有错误出现, 结束自身进程
PostQuitMessage(0);
exit(0);
}
return 1;
}
(17)如何在注册表中写入一个键值:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code_RegSetValueEx
//--------------------------
// write a keyvalue
//--------------------------
HKEY hKey;
LONG result;
DWORD dwType=REG_SZ;//C字符串
char lpData[128];
DWORD cbData=strlen(value)+1;//设置字符串时,size必须要包含结尾的\0
if (RegOpenKey(HKEY_LOCAL_MACHINE, "keyName", &hKey) != ERROR_SUCCESS)
{
//如果键不存在,则创建它
if (RegCreateKey(HKEY_LOCAL_MACHINE, "keyName", &hKey) != ERROR_SUCCESS)
return FALSE;
}
result = RegSetValueEx(hKey, "valueName", 0, dwType, (VOID*)lpData, cbData);
RegCloseKey(hKey);
//--------------------------
// query a keyvalue
//--------------------------
if (RegOpenKey(HKEY_LOCAL_MACHINE, "keyName", &hKey) != ERROR_SUCCESS)
{
return FALSE;
}
result = RegQueryValueEx(hKey, "valueName", NULL, &dwType, lpData, &cbData);
RegCloseKey(hKey);