重载窗口的 PreCreateWindow( CREATESTRUCT& cs );函数
可以通过修改参数cs中的lpszClass的值,来修改classname

解决方案 »

  1.   

    你是什么意思?
    窗口的Class Name 在创建时就应该被指定;如果你用CButton等的成员函数直接创建时,Class Name 时可以不用指定.你的设置是什么意思呢?你的目的是什么呢?
      

  2.   

    在MFC基于对话框的程序中如何设置窗口的ClassName为了让程序不能同时运行两次以上,我想通过设置窗口的ClassName值来实现,我在程序启动时加入
    FindWindow函数检测窗口是否存在,如果已运行就退出.现在问题是无法更改ClassName的值.不知还有什么办法可让程序只能运行一次
      

  3.   

    to:hide
    你的方法不行.改变不了.
      

  4.   

    我刚抄了一个让应用程序只运行一个实例的代码,不知能不能帮上忙BOOL CDlgApp::InitInstance()
    {
    ///////////////////////////////////这一段是要加的
    HANDLE hSem = CreateSemaphore( NULL, 1, 1, m_pszAppName );
    if ( GetLastError() == ERROR_ALREADY_EXISTS )
    {
    CloseHandle(hSem);
    HWND hWndPrevious = ::GetWindow( ::GetDesktopWindow(), GW_CHILD );
    while ( ::IsWindow(hWndPrevious) )
    {
    if ( ::GetProp( hWndPrevious, m_pszAppName ) )
    {
    if ( ::IsIconic(hWndPrevious) )
    ::ShowWindow( hWndPrevious, SW_RESTORE ); 
    ::SetForegroundWindow(hWndPrevious);
    ::SetForegroundWindow(::GetLastActivePopup(hWndPrevious)); 
    return FALSE;
    }
    hWndPrevious = ::GetWindow( hWndPrevious, GW_HWNDNEXT );

    return FALSE;

    ////////////////////////////////////////////到此为止
    AfxEnableControlContainer();// Standard initialization
    // If you are not using these features and wish to reduce the size
    // of your final executable, you should remove from the following
    // the specific initialization routines you do not need.#ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endifSetRegistryKey(_T("abc"));CDlgDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    // dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    // dismissed with Cancel
    }// Since the dialog has been closed, return FALSE so that we exit the
    // application, rather than start the application's message pump.
    return FALSE;
    }
    int CDlgDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;// TODO: Add your specialized creation code here::SetProp( m_hWnd, ::AfxGetApp()->m_pszAppName, (HANDLE)1 );//加这句return 0;
    }
    void CDlgDlg::OnDestroy() 
    {
    CDialog::OnDestroy();// TODO: Add your message handler code here::RemoveProp( m_hWnd, ::AfxGetApp()->m_pszAppName );//加这句}就可以实现了
      

  5.   

    to:phoenixtree
    你提供的程序解决了只运行一个实例的问题