荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Peter (小飞侠), 信区: Program
标  题: vc编程指南68篇(9)
发信站: BBS 荔园晨风站 (Tue Jan 26 22:36:24 1999), 转信


61、如何查询和设置系统参数

    在Windows 3.1 SDK中介绍过SDK函数SystemParametersInfo,调用该函数可
以查询和设置系统参数,诸如按键的重复速率设置、鼠标双击延迟时间、图标字体
以及桌面覆盖位图等等。

//Create a font that is used for icon titles.
LOGFONT stFont;
: : SystemParametersInfo (SPIF_GETICONTITLELOGFONT,
     sizeof (LOGFONT), &stFont, SPIF_SENDWININICHANGE);
m_font.CreateFontIndirect (&stFont);

//Change the wallpaper to leaves.bmp.
: : SystemParametersInfo (SPI_SETDESKWALLPAPER, 0,
       _T (" forest.bmp"), SPIF_UPDATEINIFILE);

62、如何使用一个预定义的Windows光标

    调用CWinApp:: LoadStandardCursor并传送光标标识符。
     BOOL CSampleDialog:: OnSetCursor (CWnd* pWnd, UINT nHitTest, UINT
message)
{
     //Display wait cursor if busy.
     if (m_bBusy)
     {
         SetCursor (AfxGetApp () ->LoadStandardCursor (IDC_WAIT));
         return TRUE;
     }

     return CDialog:: OnSetCursor (pWnd. nHitTest,message);
}

63、如何确定当前屏幕分辨率

    调用SDK函数GetSystemMetrics,该函数可以检索有关windows显示信息,诸如
标题大小、边界大小以及滚动条大小等等。

//Initialize CSize object with screen size.
CSize sizeScreen (GetSystemMetrics (SM_CXSCREEN),
     GetSystemMetrics (SM_CYSCREEN));

64、如何检索原先的Task Manager应用程序使用的任务列表

    原先的Task Manager应用程序显示顶层窗口的列表。为了显示该列表,窗口
必须可见、包含一个标题以及不能被其他窗口拥有。调用CWnd:: GetWindow可以
检索顶层窗口的列表,调用IsWindowVisible、GetWindowTextLength以及GetOwner
可以确定窗口是否应该在列表中。下例将把TaskManager窗口的标题填充到列表中。

void GetTadkList (CListBox&list)
{
     CString strCaption;        //Caption of window.

     list.ResetContent ();       //Clear list box.

     //Get first Window in window list.
     ASSERT_VALID (AfxGetMainWnd ());
     CWnd* pWnd=AfxGetMainWnd () ->GetWindow (GW_HWNDFIRST);

     //Walk window list.
     while (pWnd)
     {
         // I window visible, has a caption, and does not have an owner?
         if (pWnd ->IsWindowVisible () &&
            pWnd ->GetWindowTextLength () &&! pWnd ->GetOwner ())
         {
            //Add caption o window to list box.
            pWnd ->GetWindowText  (strCaption);
            list.AddString (strCaption);
         }
         //Get next window in window list.
         pWnd=pWnd->GetWindow (GW_HWNDNEXT);
     }
}

65、如何确定Windows和Windows系统目录

    有两个SDK函数可以完成该功能。GetWindowsDirectory和GetSystemDirectory,
下例说明了如何使用这两个函数:

TCHAR szDir [MAX_PATH];
//Get the full path of the windows directory.
: : GetWindowsDirectory (szDir, MAX_PATH);
TRACE ("Windows directory %s\n", szDir);
//Get the full path of the windows system directory.
: : GetSystemDirectory (szDir, MAX_PATH);
TRACE ("Windows system directory %s\n", szDir);

66、在哪儿创建临文件

    调用SDK函数GetTemPath可以确定临时文件的目录,该函数首先为临时路径
检测TMP环境变量:如果没有指定TMP,检测TMP环境变量,然后返回到当前目录。
下例说明了如何创建一个临时文件。

     //get unique temporary file.
     CString strFile;
     GetUniqueTempName (strFile);

     TRY
     {
        //Create file and write data.Note that file is closed
        //in the destructor of the CFile object.
        CFile file (strFile,CFile:: modeCreate | CFile:: modeWrite);

        //write data
     }

     CATCH (CFileException, e)
     {
        //error opening file
     }
     END_CATCH


Void GetuniqueTempName (CString& strTempName)
{
     //Get the temporary files directory.
     TCHAR szTempPath  [MAX_PATH];
     DWORD dwResult=:: GetTempPath (MAX_PATH, szTempPath);
     ASSERT (dwResult);

     //Create a unique temporary file.
     TCHAR szTempFile  [MAX_PATH];
     UINT nResult=GetTempFileName (szTempPath, _T ("~ex"),0,szTempfile);
     ASSERT (nResult);

     strTempName=szTempFile;
}

67、如何访问桌面窗口

    静态函数CWnd:: GetDesktopWindow 返回桌面窗口的指针。下例说明了MFC
函数CFrameWnd::BeginModalStae是如何使用该函数进入内部窗口列表的。

void CFrameWnd::BeginModalState ()
{
     …

     //first count all windows that need to be disabled
     UINT nCount=0;
     HWND hWnd=:: GetWindow (:: GetDesktopWindow (), GW_CHILD);
     while (hWnd!=NULL)
     {
        if (:: IsWindowEnabled (hwnd) &&
            CWnd::FromHandlePermanent (hWnd)!=NULL &&
            AfxIsDescendant (pParent->m_hWnd, hWnd) &&
            :: SendMessage (hWnd, WM_DISABLEMODAL, 0, 0)==0)
        {
            ++nCount;
        }
        hWnd=:: GetWindow (hWnd, GW_HWNDNEXT);
     }
     …
}

68、
    12. 如何让窗口和 MDI窗口一启动就最大化和最小化?
     该条两条合并为一条了.

--
※ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: 192.168.1.3]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店