|
Posted by bill on December 5, 2005, 9:18 am
Please log in for more thread options
On smartphone, ActivatePreviousInstance can¡¯t Activate Previous Instance
successfully. It does find the previous dialog and show its title ¡°hello¡±
immediately. But the main body is not shown until half a minute later.
Anyone could help me? Thanks.
BOOL DlgProcSP2(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
BOOL bRtn = FALSE;
/* CDialogSP *pDlg = NULL;
CWndSP::Lock();
try
{
if(uiMsg == WM_INITDIALOG)
{
pDlg = (CDialogSP *)lParam;
pDlg->Attach(hWnd);
s_mapHwnd2Dlg[hWnd] = pDlg;
bRtn = pDlg->OnInitDialog();
}
else
{
CMapHWND2Dlg::iterator itr =
s_mapHwnd2Dlg.find(hWnd);
if(itr == s_mapHwnd2Dlg.end())
bRtn = FALSE;
else
{
pDlg = itr->second;
bRtn = pDlg->HookDlgProc(uiMsg, wParam,
lParam);
}
}
}
catch(...)
{
wstring s = L"err";
}
Unlock();*/
return bRtn;
}
#define ARRAYSIZE(a) (sizeof(a)/sizeof(*a))
const TCHAR* g_szAppWndClass = TEXT("Dialog");
TCHAR g_szMessage[30];
HRESULT ActivatePreviousInstance(
const TCHAR* pszClass,
const TCHAR* pszTitle,
BOOL* pfActivated
)
{
HRESULT hr = S_OK;
int cTries;
HANDLE hMutex = NULL;
*pfActivated = FALSE;
cTries = 5;
while(cTries > 0)
{
hMutex = CreateMutex(NULL, FALSE, pszClass); // NOTE: We don't want
to own the object.
if(NULL == hMutex)
{
// Something bad happened, fail.
hr = E_FAIL;
goto Exit;
}
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
HWND hwnd;
CloseHandle(hMutex);
hMutex = NULL;
// There is already an instance of this app
// running. Try to bring it to the foreground.
//pszTitle = NULL;
hwnd = FindWindow(pszClass, pszTitle);
if(NULL == hwnd)
{
// It's possible that the other window is in the process of
being created...
Sleep(500);
hwnd = FindWindow(pszClass, pszTitle);
}
int nRtn;
BOOL b;
HWND hw;
if(NULL != hwnd)
{
// Set the previous instance as the foreground window
// The "| 0x01" in the code below activates
// the correct owned window of the
// previous instance's main window.
b = SetForegroundWindow((HWND) (((ULONG) hwnd) | 0x01));
/* b = SetForegroundWindow(hwnd);
hw = SetActiveWindow(hwnd);
b = BringWindowToTop(hwnd);
//UpdateWindow(hwnd);
InvalidateRect(hwnd, NULL, TRUE);
//PostMessage(hwnd,
UM_SetForegroundWindow, 0, 0);*/
// We are done.
*pfActivated = TRUE;
break;
}
// It's possible that the instance we found isn't coming up,
// but rather is going down. Try again.
cTries--;
}
else
{
// We were the first one to create the mutex
// so that makes us the main instance. 'leak'
// the mutex in this function so it gets cleaned
// up by the OS when this instance exits.
break;
}
}
if(cTries <= 0)
{
// Someone else owns the mutex but we cannot find
// their main window to activate.
hr = E_FAIL;
goto Exit;
}
Exit:
return(hr);
}
/*****************************************************************************
WinMain
***************************************************************************/
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow
)
{
HWND hwnd = NULL;
BOOL fActivated;
TCHAR szAppTitle[20];
if(0 == LoadString(theApp.GetAppInst(), IDS_HELLO_TITLE, szAppTitle,
ARRAYSIZE(szAppTitle)))
{
return(0);
}
if(FAILED(ActivatePreviousInstance(g_szAppWndClass, szAppTitle,
&fActivated)) ||
fActivated)
{
return(0);
}
return DialogBoxParam(hInstance,
MAKEINTRESOURCE(IDD_DIALOG3), NULL,
DlgProcSP2,
NULL
);
}
|
| Similar Threads | Posted | | MMS COMPOSER + Delivery Report [forget previous post this one is complete and important] | January 5, 2006, 12:27 pm |
| Activate Bluetooth with a button | October 13, 2005, 10:06 am |
| How to programatically activate the speakerphone | June 27, 2006, 8:41 pm |
| Automatically activate application on receiving SMS | December 21, 2006, 1:12 am |
| File Open dialog | December 4, 2006, 11:53 am |
| Save Dialog Box for Smartphone 5.0 | May 16, 2007, 11:13 am |
| Notifications Appear Above Password Dialog | August 2, 2007, 3:46 pm |
| Adding a hyperlink in dialog | February 11, 2008, 7:38 am |
| wireless manager connecting dialog box | August 21, 2006, 7:18 pm |
| How do I launch a Call History dialog? | January 3, 2007, 2:10 am |
|