Click here to Skip to main content
12,697,103 members (26,494 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VS2005 Visual-Studio dialog , +
I have one issue regarding to the dialog box. I have one menu, after clicked on this a dialog is popped up. When I clicked again on the same menu without closing the last opened dialog an another instance of same dialog is opened. This happens till I close all the created instance of the dialog. How I found that the dialog's instance is already run or not or how to detect that the dialog is opened or not? Please somebody help me
Posted 11-Oct-12 21:44pm
Comments
Wes Aday 12-Oct-12 7:47am
   
Simple, show the dialog modal then you will not have this problem.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use FindWindow() to get handle to your window.


HWND hDlgExists = ::FindWindow(0,”MyDialogTitle”); // hDlgExists will be NULL if dlg is not exist.


http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499%28v=vs.85%29.aspx
  Permalink  
Comments
vaibhavj_navdurga 12-Oct-12 3:09am
   
I used this but in future if the name of the dialog is changed then this method is not useful. Is there any another way in which I pass the handle of that dialog and then it detect that this dialog is opened or not?
Santhosh_G 12-Oct-12 3:32am
   
// The handle to be tested.
HWND hWindow = GetSafeHwnd();

// Check whether the window is still there.
BOOL bWindowAlive = IsWindow( hWindow );
vaibhavj_navdurga 12-Oct-12 4:05am
   
I got the handle of the dialog as null. How I get the handle of the window? I used the below code:

void CVTXMIOCaptureDlg::OnContextViewsetup()
{
CEngineSetupDialog *dlg = new CEngineSetupDialog();
HWND hWindow = dlg->GetSafeHwnd();
BOOL bWndAlive = ::IsWindow(hWindow);
if(!bWndAlive)
{
dlg->DoModal();
}
}
Santhosh_G 12-Oct-12 6:14am
   
Simply creating a CDialog instance will not give a valid window handle.
TestDlg* pDlg = new TestDlg();
HWND hNullHandle = pDlg->GetSafeHwnd(); // Handle will be NULL
pDlg->Create( IDD_DIALOG_ID, 0 );
HWND hValidaHandle = pDlg->GetSafeHwnd(); // Handle will be Valid
pDlg->ShowWindow( SW_SHOW );
Santhosh_G 12-Oct-12 5:36am
   
If it is DoModal() to display the dialog, how you will get more than one dialog ?
Are you creating new process, or new thread to display new dialog ?
vaibhavj_navdurga 12-Oct-12 7:15am
   
No, I can't use or create any process or thread. It is just simple dialog whenever user select the menu it shows that dialog. It is DoModal(), but whenever I click on the menu it creates the new one dialog which is wrong. This menu item is handled from a tray icon, that's why user select that menu item again and again and the new dialog instance get created. How to resolved it please tell me.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

bool bAlreadyRunning;

HANDLE hMutexOneInstance = ::CreateMutex( NULL, FALSE, _T("Your_Dialog_Name"));

bAlreadyRunning = ( ::GetLastError() == ERROR_ALREADY_EXISTS ||
                    ::GetLastError() == ERROR_ACCESS_DENIED);

if( !bAlreadyRunning )
{
  // Initialize Dialog and Call DoModal()
}
  Permalink  
Comments
vaibhavj_navdurga 12-Oct-12 8:12am
   
instead of using mutex I directly used FindWindow() method because both are used the Name of the dialog instead of Handle.
Santhosh_G 12-Oct-12 8:22am
   
Its not required to have the same name of Dialog name.
Any unique name to indicate your dialog. It should be unique. If anyone else opened a mutex with same name, your dialog will not appear.
vaibhavj_navdurga 12-Oct-12 8:33am
   
I used the above code it creates an issue: As first time it creates the window then if I again select the menu it didn't allow to create dialog again. But if I selects the Apply or Cancel button, the dialog closed and after that I select the menu then it didn't allow to create the dialog. It happens till I exit the tray icon. When I start the application again it work and did the same thing as I above explained.
Santhosh_G 12-Oct-12 8:48am
   
After DoModal(), please call ReleaseMutex() and CloseHandle() to close the mutex instance.
vaibhavj_navdurga 12-Oct-12 9:15am
   
Thanx for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100