龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VC开发 >

Visual C++编程技巧小结(3)

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
9. 如何改变窗口的图标? 向窗口发送 WM_SECTION消息。 Example: HICON hIcon=AfxGetApp() -LoadIcon(IDI_ICON); ASSERT(hIcon); AfxGetMainWnd() -SendMessage(WM_SECTION,TRUE,(LPARAM) hIcon); 1

   9. 如何改变窗口的图标?

   向窗口发送 WM_SECTION消息。

   Example:

HICON hIcon=AfxGetApp() ->LoadIcon(IDI_ICON);
ASSERT(hIcon);
AfxGetMainWnd() ->SendMessage(WM_SECTION,TRUE,(LPARAM) hIcon); 

   10. 如何改变窗口的缺省风格?

   重写 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);

   11. 如何将窗口居中显示?

Easy, Call Function CWnd:: Center Windows
Example(1): Center Window( ); //Relative to it's parent
// Relative to Screen
Example(2): Center Window(CWnd:: GetDesktopWindow( ));
//Relative to Application's MainWindow
AfxGetMainWnd( ) -> Center Window( ); 

   12. 如何让窗口和 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的 风格。

   13. 如何使程序保持极小状态?

   很有意思的问题

   这么办: 在恢复程序窗体大小时, Windows 会发送WM_QUERY-OPEN 消息,用ClassWizard设置成员函数 OnQueryOpen() ,add following code:

Bool CMainFrame:: OnQueryOpen( )
{
  Return false;

   14. 如何限制窗口的大小?

   也就是 FixedDialog 形式。 Windows 发送 WM_GETMAXMININFO消息来跟踪, 响应它,在OnGetMAXMININFO 中写代码:

   15. 如何使窗口不可见?

   很简单,用SW_HIDE 隐藏窗口,可以结合 FindWindow,ShowWindow 控制.

   16. 如何使窗口始终在最前方?

   两种途径.

BringWindowToTop(Handle); 

   SetWindowPos函数,指定窗口的 最顶风格,用WS_EX_TOPMOST扩展窗口的 风格

   Example:

void ToggleTopMost( CWnd *pWnd)
{
  ASSERT_VALID(pWnd);
  pWnd ->SetWindowPos(pWnd-> GetStyle( ) &WS_EX_TOPMOST)?
  &wndNoTopMOST: &wndTopMost,0,0,0,0,SSP_NOSIZE|WSP_NOMOVE};
}

精彩图集

赞助商链接