思路:
1.找到这个实例的窗口句柄
2.如果找到就发送一个自定义的消息,如果没有就建立这个实例
eg.
#include
<
windows.h
>
#include < WindowsX.h >
#include < tchar.h > // _ttoi
#include " Resource.h "
#include " CmnHdr/CmnHdr.h "
#define ESM_POKECODEANDLOOKUP (WM_USER + 100)
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {
switch (id) {
case IDCANCEL:
EndDialog(hwnd, id);
break;
}
}
//////////////////////////////////////////////////////////////////////////
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) {
chSETDLGICONS(hwnd, IDI_MAIN);
// Look up the command-line passed error number
SendMessage(hwnd, ESM_POKECODEANDLOOKUP, lParam, 0);
return(TRUE);
}
///////////////////////////////////////////////////////////////////////////////
INT_PTR WINAPI Main_DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Main_OnInitDialog);
chHANDLE_DLGMSG(hwnd, WM_COMMAND, Main_OnCommand);
case ESM_POKECODEANDLOOKUP:
SetForegroundWindow(hwnd);
break;
}
return(FALSE);
}
//////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int ) {
HWND hwnd = FindWindow(TEXT("#32770"), TEXT("pc2pc"));
if (IsWindow(hwnd)) {
// An instance is already running, activate it and send it the new #
SendMessage(hwnd, ESM_POKECODEANDLOOKUP, _ttoi(pszCmdLine), 0);
} else {
DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_MAINDLG),
NULL, Main_DlgProc, _ttoi(pszCmdLine));
}
return(0);
}
#include < WindowsX.h >
#include < tchar.h > // _ttoi
#include " Resource.h "
#include " CmnHdr/CmnHdr.h "
#define ESM_POKECODEANDLOOKUP (WM_USER + 100)
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {
switch (id) {
case IDCANCEL:
EndDialog(hwnd, id);
break;
}
}
//////////////////////////////////////////////////////////////////////////
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) {
chSETDLGICONS(hwnd, IDI_MAIN);
// Look up the command-line passed error number
SendMessage(hwnd, ESM_POKECODEANDLOOKUP, lParam, 0);
return(TRUE);
}
///////////////////////////////////////////////////////////////////////////////
INT_PTR WINAPI Main_DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Main_OnInitDialog);
chHANDLE_DLGMSG(hwnd, WM_COMMAND, Main_OnCommand);
case ESM_POKECODEANDLOOKUP:
SetForegroundWindow(hwnd);
break;
}
return(FALSE);
}
//////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int ) {
HWND hwnd = FindWindow(TEXT("#32770"), TEXT("pc2pc"));
if (IsWindow(hwnd)) {
// An instance is already running, activate it and send it the new #
SendMessage(hwnd, ESM_POKECODEANDLOOKUP, _ttoi(pszCmdLine), 0);
} else {
DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_MAINDLG),
NULL, Main_DlgProc, _ttoi(pszCmdLine));
}
return(0);
}