zoukankan      html  css  js  c++  java
  • 在MFC SDI应用程序中保持窗口位置

    介绍 在本文中,我们将展示一种方法,它可以在一次执行和另一次执行之间保持应用程序窗口的位置和大小。 步骤 添加一个WM_CLOSE处理程序到CMainFrame类:复制Code

    void CMainFrame::OnClose() 
    {
        WINDOWPLACEMENT    wp;
    
        // before it is destroyed, save the position of the window
        wp.length = sizeof wp;
    
        if ( GetWindowPlacement(&wp) )
        {
    
            if ( IsIconic() )
              // never restore to Iconic state
              wp.showCmd = SW_SHOW ;
    
            if ((wp.flags & WPF_RESTORETOMAXIMIZED) != 0)
              // if maximized and maybe iconic restore maximized state
              wp.showCmd = SW_SHOWMAXIMIZED ;
    
            // and write it to the .INI file
            WriteWindowPlacement(&wp);
        }
        
        CFrameWnd::OnClose();
    }

    重写CMainFrame类的ActivateFrame()方法:Hide  收缩,复制Code

    void CMainFrame::ActivateFrame(int nCmdShow)
    {
        // nCmdShow is the normal show mode this frame should be in
        // translate default nCmdShow (-1)
        if (nCmdShow == -1)
        {
            if (!IsWindowVisible())
                nCmdShow = SW_SHOWNORMAL;
            else if (IsIconic())
                nCmdShow = SW_RESTORE;
        }
    
        // bring to top before showing
        BringToTop(nCmdShow);
    
        if (nCmdShow != -1)
        {
            // show the window as specified
          WINDOWPLACEMENT wp;
          
          if ( !ReadWindowPlacement(&wp) )
          {
              ShowWindow(nCmdShow);
          }
          else
          {
             if ( nCmdShow != SW_SHOWNORMAL )  
               wp.showCmd = nCmdShow;
    
             SetWindowPlacement(&wp);
             // ShowWindow(wp.showCmd);
          }
    
          // and finally, bring to top after showing
          BringToTop(nCmdShow);
        }
    
        return ;
    }

    ReadWindowPlacement和WriteWindowPlacement如下所示:收缩,复制Code

    static char szSection[]   = "Settings";
    static char szWindowPos[] = "WindowPos";
    static char szFormat[] = "%u,%u,%d,%d,%d,%d,%d,%d,%d,%d";
    
    BOOL CMainFrame::ReadWindowPlacement(WINDOWPLACEMENT *pwp)
    {
        CString strBuffer;
        int    nRead ;
    
        strBuffer = AfxGetApp()->GetProfileString(szSection, szWindowPos);
        if ( strBuffer.IsEmpty() )  return FALSE;
    
        nRead = sscanf(strBuffer, szFormat,
                    &pwp->flags, &pwp->showCmd,
                    &pwp->ptMinPosition.x, &pwp->ptMinPosition.y,
                    &pwp->ptMaxPosition.x, &pwp->ptMaxPosition.y,
                    &pwp->rcNormalPosition.left,  &pwp->rcNormalPosition.top,
                    &pwp->rcNormalPosition.right, &pwp->rcNormalPosition.bottom);
        if ( nRead != 10 )  return FALSE;
        pwp->length = sizeof(WINDOWPLACEMENT);
    
        return TRUE;
    }
    
    // Write a window placement to settings section of app's ini file.
    
    void CMainFrame::WriteWindowPlacement(WINDOWPLACEMENT *pwp)
    {
        char szBuffer[sizeof("-32767")*8 + sizeof("65535")*2];
        int max = 1;
        CString s;
    
        sprintf(szBuffer, szFormat,
                pwp->flags, pwp->showCmd,
                pwp->ptMinPosition.x, pwp->ptMinPosition.y,
                pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
                pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
                pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);
         AfxGetApp()->WriteProfileString(szSection, szWindowPos, szBuffer);
    
    }

    这两个函数的灵感来自微软Visual c++ 1.52附带的SUPERPAD示例。 如果您希望将窗口位置信息存储在注册表而不是.ini文件中,只需在项目主.cpp文件的InitInstance()方法中调用AddDocTemplate之后将其添加。隐藏,复制Code

    SetRegistryKey("My Company Name");

    本文转载于:http://www.diyabc.com/frontweb/news7078.html

  • 相关阅读:
    Hbase shell 常用命令
    HTable基本概念
    通过HBase Shell与HBase交互
    把Nutch爬虫部署到Hadoop集群上
    wso2esb安装及helloworld
    nDPI 的论文阅读和机制解析
    Ubuntu 编译出现 ISO C++ 2011 不支持的解决办法
    404 Note Found 队-课堂实战-项目UML设计
    nDPI的安装与测试
    精读 SBAR SDN flow-Based monitoring and Application Recognition
  • 原文地址:https://www.cnblogs.com/Dincat/p/13467471.html
Copyright © 2011-2022 走看看