读书人

100分用MFC建立的对话框程序怎么设置

发布时间: 2012-03-07 09:13:51 作者: rapoo

100分求助:用MFC建立的对话框程序,如何设置它的classname?
如题,请各位高手解答!

[解决办法]
对话框有自己的窗口类名,你可以用Spy++看看;另外你确认创建窗口的时候用了这个注册的窗口类吗?

如果目的是只运行一个进程实例,建议使用Mutex。
[解决办法]
使用没有问题
BOOL COneT32App::FirstInstance()
{
CWnd *pWndPrev, *pWndChild;

// Determine if a window with the class name exists...
if (pWndPrev = CWnd::FindWindow(_T( "MyNewClass "),NULL))
{
// If so, does it have any popups?
pWndChild = pWndPrev-> GetLastActivePopup();

// If iconic, restore the main window
if (pWndPrev-> IsIconic())
pWndPrev-> ShowWindow(SW_RESTORE);

// Bring the main window or its popup to the foreground
pWndChild-> SetForegroundWindow();

// and you are done activating the other application
return FALSE;
}
}
这是msdn上的例子,对话框在不修改注册类名的情况下类名为“#32770(对话框)”,简单的方法是
lz使用窗口标题查找
if (pWndPrev = CWnd::FindWindow(_T( "MyNewClass "),NULL))改为
if (pWndPrev = CWnd::FindWindow(NULL,_T( "MyDialogTitleName ")))

如果想让程序只有一个实例,除了上面的方法也可以使用mutex信号量。
// Create mutex, because there cannot be 2 instances of Updater for same application
HANDLE hMutex = CreateMutex(NULL, FALSE, "update_ " + pSettings-> GetAppName());

// Check if mutex is created succesfully
switch(GetLastError())
{
case ERROR_SUCCESS:
// Mutex created successfully. There is no instance running
break;

case ERROR_ALREADY_EXISTS:
// Mutex already exists so there is a running instance of our app.
return FALSE;

default:
// Failed to create mutex by unknown reason
return FALSE;
}

读书人网 >VC/MFC

热点推荐