1 如何通过代码获得应用程序主窗口的指针?
主窗口的指针保存在CWinThread::m_pMainWnd,调用AfxGetMainWnd实现。
AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)//
使程序最大化.

2 确定应用程序的绝对路径
Use GetModuleFileName
获得应用程序的路径,然后去掉可执行文件名。
Example:
TCHAR
exeFullPath[MAX_PATH] // MAX_PATH
API中定义了吧,好象是128
GetModuleFileName(NULL,exeFullPath,MAX_PATH)
GetCurrentDirectory的区别:

GetCurrentDirectory获取当前目录, 比如CFileDialog就会改变这个目录
GetModuleFileName
是获得当前程序的路径,一般用这个来获得程序完成路径,以及拼接你其他文件路径


3 如何在程序中获得其他程序的图标?
两种方法:
(1) SDK
函数 SHGetFileInfo 或使用 ExtractIcon获得图标资源的 handle,
(2) SDK
函数 SHGetFileInfo 获得有关文件的很多信息,如大小图标,属性, 类型等.
Example(1):
在程序窗口左上角显示 NotePad图标.
void CSampleView:
OnDraw(CDC * pDC)
{
if( ::SHGetFileInfo(_T("c:""pwin95""notepad.exe"),0,&stFileInfo,sizeof(stFileInfo),SHGFI_ICON))
{
pDC ->DrawIcon(10,10,stFileInfo.hIcon)
}
}
Example(2):
同样功能,Use ExtractIcon Function
void CSampleView:: OnDraw(CDC *pDC)
{
HICON hIcon=:: ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0)
if (hIcon &&hIcon!=(HICON)-1)
pDC->DrawIcon(10,10,hIcon)
}
    
说明: 获得notepad.exe的路径正规上来说用GetWindowsDirectory函数得到, 如果是调用 win95下的画笔,应该用访问注册表的方法获得其路径,要作成一个比较考究的程序,考虑应该全面点.

4 获得各种目录信息
Windows
目录: Use "GetWindowsDirectory"
Windows
下的system目录: Use "GetSystemDirectory"
temp
目录: Use "GetTempPath"
当前目录: Use "GetCurrentDirectory"
请注意前两个函数的第一个参数为目录变量名,后一个为缓冲区;后两个相反.

5 如何自定义消息
1)
手工定义消息,可以这么写
#define WM_MY_MESSAGE(WM_USER+100),
MS
推荐的至少是 WM_USER+100
2)
写消息处理函数,WPARAM,LPARAM返回LRESULT.
LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam)

{
temp
目录: Use "GetTempPath"
//
加入你的处理函数
}

6 如何改变窗口的图标?
向窗口发送 WM_SECTION消息。
Example:
HICON hIcon=AfxGetApp() ->LoadIcon(IDI_ICON)
ASSERT(hIcon)
AfxGetMainWnd() ->SendMessage(WM_SECTION,TRUE,(LPARAM)hIcon)


7 如何改变窗口的缺省风格?
重载 CWnd:: PreCreateWindow 并修改CREATESTRUCT结构来指定窗口风格和其他创建信息.
Example: Delete "Max" Button and Set Original Window's Position and Size
BOOL CMainFrame:: PreCreateWindow(CREATESTRUCT &cs)
{
cs.style &=~WS_MAXINIZEMOX
cs.x=cs.y=0
cs.cx=GetSystemMetrics(SM_CXSCREEN/2)
cs.cy=GetSystemMetrics(SM_CYSCREEN/2)
return CMDIFramewnd ::PreCreateWindow(cs)
}

8 如何将窗口居中显示?
答:Call Function CWnd::Center Windows
Example(1):
Center Window( )
//Relative to it's parent
// Relativeto Screen
Example(2):
Center Window(CWnd:: GetDesktopWindow( ))
//Relative to Application's MainWindow
AfxGetMainWnd( ) ->Center Window( )

9 如何让窗口和 MDI窗口一启动就最大化和最小化?
先说窗口。
InitStance 函数中设定 m_nCmdShow的取值.

m_nCmdShow=SW_SHOWMAXMIZED //最大化
m_nCmdShow=SW_SHOWMINMIZED //
最小化
m_nCmdShow=SW_SHOWNORMAL //
正常方式
MDI
窗口:
如果是创建新的应用程序,可以用MFC AppWizard Advanced 按钮并在MDI子窗口风格组中检测最大化或最小化还可以重载 MDI Window PreCreateWindow函数,设置WS_MAXMIZE or WS_MINMIZE
如果从 CMDIChildWnd派生,调用 OnInitialUpdate函数中的 CWnd::Show Window来指定 MDI Child Window的风格。

10 如何限制窗口的大小?
也就是 FixedDialog形式。 Windows发送 WM_GETMAXMININFO消息来跟踪, 响应它, OnGetMAXMININFO 中写代码

posted on 2009-06-26 09:54  芊芊  阅读(346)  评论(0编辑  收藏  举报