Click here to Skip to main content
15,671,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
[no name] 12-Oct-12 7:47am    
Simple, show the dialog modal then you will not have this problem.

Use FindWindow() to get handle to your window.

HWND hDlgExists = ::FindWindow(0,"MyDialogTitle"); // hDlgExists will be NULL if dlg is not exist.
Share this answer
Vaibhav_J_Jaiswal 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 );
Vaibhav_J_Jaiswal 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);
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 ?
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()
Share this answer
Vaibhav_J_Jaiswal 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.
Vaibhav_J_Jaiswal 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.
Vaibhav_J_Jaiswal 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)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900