VC小技巧汇总之窗口技巧


本文搜集汇总了VC小技巧的窗口技巧,对于VC程序开发的窗口设计而言有一定的借鉴价值,详情如下:

1.让窗口一启动就最大化

把应用程序类(CxxxApp)的 InitInstance() 函数中的

m_pMainWnd->ShowWindow(SW_SHOW); 

改为

m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

则窗口一启动就最大化显示。

2.如何设置窗口的初始尺寸

在将应用程序类(CxxAPP)的 InitInstance() 函数中加入:

m_pMainWnd->SetWindowPos(NULL,x,y,Width,Height,SWP_NOMOVE);

Width为窗口宽度,Height为窗口高度
SWP_NOMOVE表示忽略位置(x,y)。
如:

BOOL CDzyApp::InitInstance()
{
  AfxEnableControlContainer(); 
  …… 
  // The one and only window has been initialized, so show and update it.
  m_pMainWnd->SetWindowPos(NULL,0,0,750,555,SWP_NOMOVE);//设置窗口的初始大小为750*555
  m_pMainWnd->ShowWindow(SW_SHOW); 
  m_pMainWnd->UpdateWindow(); 
  return TRUE; 
}

3.让窗口居中显示

以下两种方法可任选其一:

①在应用程序类(CxxxApp)的 InitInstance() 函数中加入:

m_pMainWnd->CenterWindow( GetDesktopWindow() );

②在主框架类(MainFrm.cpp)的OnCreate()函数中加入:

CenterWindow( GetDesktopWindow() );

如:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
  if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 
  return -1; 
  …… 

  // TODO: Delete these three lines if you don't want the toolbar to 
  // be dockable 
  m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); 
  EnableDocking(CBRS_ALIGN_ANY); 
  DockControlBar(&m_wndToolBar); 

  CenterWindow( GetDesktopWindow() ); //使窗口打开时处于屏幕正中

  return 0; 
}

4.如何修改窗口标题

窗口标题一般形式为:文档标题 - 程序标题

(1)设置文档标题:

在文档类(CxxxDoc)的OnNewDocument()函数中加入语句:SetTitle("文档名");
如:TextEditorDoc.cpp:

BOOL CTextEditorDoc::OnNewDocument() 
{ 
  if (!CDocument::OnNewDocument()) 
    return FALSE; 
  // TODO: add reinitialization code here 
  // (SDI documents will reuse this document) 
  SetTitle("未命名.txt");  //设置文档标题
  return TRUE; 
}

(2)设置程序标题:

在框架类(CMainFrame)的PreCreateWindow()函数中加入语句:m_strTitle = _T("程序标题");
如:MainFrm.cpp:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) 
{ 
  if( !CFrameWnd::PreCreateWindow(cs) ) 
    return FALSE; 
  // TODO: Modify the Window class or styles here by modifying 
  // the CREATESTRUCT cs 
  m_strTitle = _T("文本整理器");  //设置程序标题
  return TRUE; 
}

以上两点比较适用于视图-文档结构的程序,在新建文档时,系统会自动运行OnNewDocument()函数,在其中可以设置合适的标题。对于未采用文档的程序可以用下面的方法修改标题:

(3)修改窗口标题:

修改窗口标题一般在打开文件函数OnFileOpen()和另存为函数OnFileSaveAs()中进行,可以使用下面的函数:

AfxGetMainWnd()->SetWindowText("文档标题"+" - "+"程序标题");

其中文档标题和程序标题可使用定义过的串变量。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3