For Programmers: Free Programming Magazines  


Home > Archive > Smartphone Developer Forum > December 2005 > Activate previous instance and delay/dialog class name









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Activate previous instance and delay/dialog class name
Oliver Schad

2005-12-13, 8:17 am

Hi,

I've created a dialog based application in C++. If I start another
application my application runs in background. If I start the
application again I want to activate the first instance.

The previous instance is shown and I can edit text boxes. But the menu
bar appears 30 seconds later. What is going wrong? I found similar
questions with google but no answer.

I tried to use the generated code from embedded Visual C++ for Hello World.

The "standard mechanism" uses CreateMutex(NULL, FALSE, pszClass),
FindWindow(pszClass, pszTitle) and SetForegroundWindow((HWND) (((ULONG)
hwnd) | 0x01)) to find a older instance and bring it to the front.

I've added ShowWindow(hwnd, SW_SHOW) after SetForegroundWindow().
Without that, only the title of the application appears, 30 seconds
later the dialog and the menu bar.

Another problem:

I don't use CreateMutex for now because I don't know how this works with
dialogs. I can set the class name of a window with WNDCLASS struct. What
is the class name of my dialog? How can I set/get this?

Any hints?

Best regards
Oli
dsr2008

2005-12-13, 7:09 pm

I've got the same problem too. The solution I found is that the dialog should
be created with WS_VISIBLE & WS_SYSMENU & WS_CAPTION styles (check the .rc
file). After I do that, the 30 seconds delay problem gone.

"Oliver Schad" wrote:

> Hi,
>
> I've created a dialog based application in C++. If I start another
> application my application runs in background. If I start the
> application again I want to activate the first instance.
>
> The previous instance is shown and I can edit text boxes. But the menu
> bar appears 30 seconds later. What is going wrong? I found similar
> questions with google but no answer.
>
> I tried to use the generated code from embedded Visual C++ for Hello World.
>
> The "standard mechanism" uses CreateMutex(NULL, FALSE, pszClass),
> FindWindow(pszClass, pszTitle) and SetForegroundWindow((HWND) (((ULONG)
> hwnd) | 0x01)) to find a older instance and bring it to the front.
>
> I've added ShowWindow(hwnd, SW_SHOW) after SetForegroundWindow().
> Without that, only the title of the application appears, 30 seconds
> later the dialog and the menu bar.
>
> Another problem:
>
> I don't use CreateMutex for now because I don't know how this works with
> dialogs. I can set the class name of a window with WNDCLASS struct. What
> is the class name of my dialog? How can I set/get this?
>
> Any hints?
>
> Best regards
> Oli
>

Oliver Schad

2005-12-13, 7:09 pm

Hi,

dsr2008 wrote:
> I've got the same problem too. The solution I found is that the dialog should
> be created with WS_VISIBLE & WS_SYSMENU & WS_CAPTION styles (check the .rc
> file). After I do that, the 30 seconds delay problem gone.


That's my style:
STYLE WS_POPUP | WS_VISIBLE | WS_VSCROLL | WS_SYSMENU | WS_CAPTION

Same problem. The menu bar appears later. Which other styles do you use?

Regards
Oli
dsr2008

2005-12-13, 9:59 pm

WS_POPUP ? Anyway, here is my sample code(at least works for me at 2003 SE &
WM5, both emu and device). The point is, when app is running, if user press
Home, app is still running at background. when back key is pressed at home
screen, OS try bring back our app to foreground, a long delay may occur if
style is not right. CE build-in MS apps have no delay, but i am unable check
the resource in MS app binary to verify the style. I got this guess from
browsing wm5 SDK samples. CE SPY may be a helper.

// from resource.rc

IDD_MAIN DIALOG 0, 0, 113, 116
STYLE WS_VISIBLE | WS_CAPTION | WS_SYSMENU
BEGIN
EDITTEXT IDC_INPUT,7,7,85,82,ES_MULTILINE | ES_AUTOVSCROLL |
ES_WANTRETURN | WS_VSCROLL
END

// from winmain.cpp

#include <windows.h>
#include <aygshell.h>
#include <tpcshell.h>
#include "resource.h"

static HINSTANCE g_hInstance = NULL;
const TCHAR APP_TITLE[] = L"Test";

static void InitDialog(HWND hwnd)
{
// Zoom dialog
SHINITDLGINFO shidi;
ZeroMemory(&shidi, sizeof(shidi));
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hwnd;
SHInitDialog(&shidi);

// Setup menu bar
SHMENUBARINFO shmbi;
ZeroMemory(&shmbi, sizeof(shmbi));
shmbi.cbSize = sizeof(shmbi);
shmbi.hwndParent = hwnd;
shmbi.nToolBarId = IDR_MENUBAR;
shmbi.hInstRes = g_hInstance;
SHCreateMenuBar(&shmbi);

SendMessage(shmbi.hwndMB, SHCMBM_OVERRIDEKEY, VK_TBACK,
MAKELPARAM(SHMBOF_NODEFAULT | SHMBOF_NOTIFY, SHMBOF_NODEFAULT |
SHMBOF_NOTIFY));

// Setup window title
SetWindowText(hwnd, APP_TITLE);
}

BOOL CALLBACK MainDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
lParam)
{
switch (msg)
{
case WM_INITDIALOG:
InitDialog(hwnd);
return TRUE;

case WM_HOTKEY:
// The following is necessary to get the appropriate Backspace
key behavior
// for our edit control
if (HIWORD(lParam) == VK_TBACK)
SHSendBackToFocusWindow(msg, wParam, lParam);
return TRUE;

case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
EndDialog(hwnd, 0);
return TRUE;
}
break;
}
return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
{
// Switch to previous instance if it is found
HWND hwnd = FindWindow(L"Dialog", APP_TITLE);
if (hwnd)
{
SetForegroundWindow(hwnd);
return 0;
}
g_hInstance = hInstance;
DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDialogProc);
return 0;
}

"Oliver Schad" wrote:

> Hi,
>
> dsr2008 wrote:
>
> That's my style:
> STYLE WS_POPUP | WS_VISIBLE | WS_VSCROLL | WS_SYSMENU | WS_CAPTION
>
> Same problem. The menu bar appears later. Which other styles do you use?
>
> Regards
> Oli
>

Oliver Schad

2005-12-14, 7:09 pm

dsr2008 wrote:
> "Oliver Schad" wrote:
> WS_POPUP ? Anyway, here is my sample code(at least works for me at 2003 SE &


That's it. With WS_POPUP you have to wait a long time until the
application window comes up. Without it everything works fine. Thanks a lot.

Regards
Oli
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com