程序只运行一个实例(三)

采用 SetProp()设置窗口属性,并用EnumWindows()进行窗口的遍历:

1
1、加入全局变量的定义及枚举窗口函数:
TCHAR g_szPropName[] = _T("{12AA5160-5215-435b-AE3C-60C9E65615CE}");
HANDLE g_hValue = (HANDLE)9527;

BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lParam)
{
	HANDLE hProp = GetProp(hwnd, g_szPropName);
	if(hProp == g_hValue) {
		*(HWND *)lParam = hwnd;
		return FALSE;
	}
	return TRUE;
}

2
2、窗口的枚举以及属性的添加:
//OnInitDialog() 中加入以下代码:
HWND hPreWnd = NULL;
::EnumWindows(EnumWndProc, (LPARAM)&hPreWnd);
if (hPreWnd != NULL) {
	AfxMessageBox(_T("已经有一个实例正在运行中……"));
	::ShowWindow(hPreWnd, SW_NORMAL);
	::SetForegroundWindow(hPreWnd);
	ExitProcess(0);
	return FALSE;
}
::SetProp(m_hWnd, g_szPropName, g_hValue);


3
3、窗口属性的删除:
OnDestroy() 函数中加入以下代码:
::RemoveProp(m_hWnd, g_szPropName);

相关函数解释:

1
This function enumerates all top-level windows on the screen by passing the handle to each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE. 

BOOL EnumWindows( 
  WNDENUMPROC lpEnumFunc, 
  LPARAM lParam 
); 
Parameters
lpEnumFunc 
[in] Long pointer to an application-defined callback function. For more information, see EnumWindowsProc. 
lParam 
[in, out] Specifies an application-defined value to be passed to the callback function. 
Return Values
Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.

Remarks
The EnumWindows function does not enumerate child windows. 

This function is more reliable than calling the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed. 

2
The SetProp function adds a new entry or changes an existing entry in the property list of the specified window. The function adds a new entry to the list if the specified character string does not exist already in the list. The new entry contains the string and the handle. Otherwise, the function replaces the string's current handle with the specified handle. 

Syntax

BOOL SetProp(          HWND hWnd,
    LPCTSTR lpString,
    HANDLE hData
);
Parameters

hWnd
[in] Handle to the window whose property list receives the new entry. 
lpString
[in] Pointer to a null-terminated string or contains an atom that identifies a string. If this parameter is an atom, it must be a global atom created by a previous call to the GlobalAddAtom function. The atom must be placed in the low-order word of lpString; the high-order word must be zero. 
hData
[in] Handle to the data to be copied to the property list. The data handle can identify any value useful to the application. 
Return Value

If the data handle and string are added to the property list, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.




Remarks

Before a window is destroyed (that is, before it returns from processing the WM_NCDESTROY message), an application must remove all entries it has added to the property list. The application must use the RemoveProp function to remove the entries. 

3
This function retrieves a data handle identified by a specified character string from the property list of a specified window.

HANDLE GetProp(
  HWND hWnd,
  LPCTSTR lpString
);
Parameters
hWnd 
[in] Handle to the window for which you want to retrieve a data handle from the property list. 
lpString 
[in] Pointer to a null-terminated character string that identifies the data handle, or an atom that identifies the string. If this parameter is an atom, the application must create the atom by using the GlobalAddAtom function. The application must place the atom, a 16-bit value, in the low-order word of the lpString parameter; the high-order word of lpString must then be zero. 
Return Values
The data handle associated with the specified string indicates that the property list contains the string. NULL indicates that the property list does not contain the string.

Remarks
The application must add the string and handle to the property list by previously calling the SetProp function.

4
The RemoveProp method removes a property specified by name.

Syntax

HRESULT RemoveProp(
  LPCWSTR  pcwszName
);

Parameters

pcwszName

[in]  Specifies the name of the property to be removed.

Return Values

If the method succeeds, it returns S_OK. If it fails, it returns an HRESULT error code.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小米的修行之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值