[分享]总结:VC小知识![9]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 seuu 的 blog

原先的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)
}
}

(69) 如何确定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)

(70) 在哪儿创建临文件
调用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
}

(71) 我怎样才能建立一个等待光标?
调 用 beginwaitcursor 函 数 来 启 动 等 待 光 标,调 用 endwaitcursor 函 数 来 结 束 等 待 光 标。要 注 意,二 者 都 要 调 用 app 的 成 员 函 数,如 下 所 示:

    afxgetapp()->beginwaitcursor();
    // 要做的事
    afxgetapp()->endwaitcursor();

(72) 我在mdi框架中有个 form 视窗。它有个取消按钮,我需要当用户按取消按钮时可关闭form视窗。我应该如何关闭该文档?
调 用 onclosedocument 函 数。

(73) 如何访问桌面窗口
静态函数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)
}

(74) 什么是colorref? 我该怎样用它?
colorref 是 一 个 32-bit 整 型 数 值,它 代 表 了 一 种 颜 色。你 可 以 使 用 rgb 函 数 来 初 始 化 colorref。例 如:

    colorref color = rgb(0, 255, 0);
rgb 函 数 接 收 三 个 0-255 数 值,一 个 代 表 红 色, 一 个 代 表 绿 色, 一 个 代 表 蓝 色。在 上 面的 例 子 中, 红 色 和 蓝 色 值 都 为 0,所 以 在 该 颜 色 中 没 有 红 色 和 蓝 色。绿 色 为 最 大 值 255。所 以 该 颜 色 为 绿 色。0,0,0 为 黑 色,255,255,255 为 白 色。

另 一 种 初 始 化 colorref 的 方 法 如 下 所 示:

    ccolordialog colordialog;
    colorref color;

    if( colordialog.domodal() == idok )
    {
        color = colordialog.getcolor();
    }
这 段 代 码 使 用 了 mfc 中 的 颜 色 对 话 框,它 需 要 文 件。

(75) appwizard所产生的stdafx文件是干什么用的?
它 主 要 是 协 助 产 生 预 编 译 头 文 件 的。通 常 你 是 不 需 要 修 改 它 的。

(76) 我在我的程序中是了cdwordarray。我向它添加了约10,000个整数,这使得它变得非常非常慢。为什么会这么糟?
cdwordarray 是 很 好 用 的,只 是 因 为 你 没 有 指 定 数 组 的最大尺寸。因 此,当 你 添 加 新 元 素 时,该 类 会 从 堆 中 重 新 分 配 空 间。不 幸 的 是,该 类 会 在 每 次 插 入 新 元 素 时 都 为 数 组 重 新 分 配 空 间。如 果 你 向 它 添 加 了 很 多 新 元 素,所 有 这 些 分 配 和 复 制 数 组 的 操 作 会 就 会 使 它 变 慢。解 决 该 问 题 的 方 法 是,你 可 以 使 用 setsize 函 数 的 第 二 个 参 数 来 改 变 这 种 重 新 分 配 的 频 率。例 如,如 果 你 把 该 参 数 设 置 为 500,则 每 次 数 组 空 间 超 出 时 它 才 重 新 分 配 并 添 加 500 个 新 空 间,而 不 是 1 个。这 样 一 来,你 就 可 以 不 用 重 新 分 配 而 添 加 了 另 外 499 个 元 素 空 间,这 也 会 大 大 提 高 程 序 的 运 行 速 度。

(77) 我该如何改变mdi框架窗口的子窗口的大小以使在窗口以一定的大小打开?

本文关键:[分享]总结:VC小知识!
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top