VC学习笔记9:几个指针

为了在视类中能够调用放在其它类中的数据或函数,应该先获得相应类的指针。

1、指向文档类(CxxDoc)的指针:
CxxDoc*pDoc = GetDocument();//文档指针
则用pDoc->xxxx的方法可在视类中访问文档类中的变量和函数。

2、指向应用程序类(CxxApp)的指针:
CxxApp*app = (CxxApp *)AfxGetApp();//应用程序指针
用app->xxxx的方法可在文档类或视类中调用CxxApp中放置的数据了。
我一般把文档和视公用的数据放在CxxApp类中,再用以上方法访问。

3、指向主框架(CMainFrame)的指针:
CFrameWnd *pFrameWnd=GetParentFrame();//获取框架窗口指针
用pFrameWnd->xxxx访问CWnd类的函数。
这种方法多用于获取窗口的位置、大小等操作,用于视类。

4、指向某一控件的指针:
如:CWnd *pEdit = GetDlgItem(IDC_EDIT1); //获取编辑控件指针
IDC_EDIT1为控件ID号。
这主要是为了能修改控件大小、位置,获取控件风格等。

5、指向状态条的指针:
CStatusBar* pStatus;//状态栏指针
pStatus=(CStatusBar *)(app->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR));
app为上面的应用程序指针。
这主要是为了更改状态栏中显示的内容。


VC学习笔记10:状态栏:

1、在状态栏中显示提示:
CxxApp*app = (CxxApp *)AfxGetApp();//应用程序指针
CStatusBar* pStatus;//状态栏指针
if(pStatus)
{
pStatus=(CStatusBar *)(app->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR));
pStatus->SetPaneText(0,str);//0为状态栏窗格号,str为显示的字符串
}

2、在状态栏中增加窗格:

下例中增加了三个新窗格:
(1)修改MainFrm.cpp:

static UINT indicators[] =
{
ID_SEPARATOR,// status line indicator

ID_SEPARATOR,//新增窗格1:放置总字节数
ID_SEPARATOR,//新增窗格2:半角字数
ID_SEPARATOR,//新增窗格3:全角字数

ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};

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

if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1;// fail to create
}

if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1;// fail to create
}

m_wndStatusBar.SetPaneInfo(1,ID_SEPARATOR,0,95); //设置新增窗格1的宽度
m_wndStatusBar.SetPaneInfo(2,ID_SEPARATOR,0,100); //设置新增窗格2的宽度
m_wndStatusBar.SetPaneInfo(3,ID_SEPARATOR,0,100); //设置新增窗格3的宽度

…………
}

(2)在新窗格中显示内容

以下程序为视类中显示状态信息的部分,
CStatusBar *pStatus=(CStatusBar *)AfxGetApp()->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR);
if(pStatus)
{
CString str;
str.Format("字节数:%d",len);
pStatus->SetPaneText(1,str);//在窗格1显示内容
str.Format("半角字符:%d",chnum);
pStatus->SetPaneText(2,str);//在窗格2显示内容
str.Format("全角字符:%d",hannum/2);
pStatus->SetPaneText(3,str);//在窗格3显示内容
}


VC学习笔记11:设置程序窗口的初始大小:

在应用程序类(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;
}


VC学习笔记12:如何让窗口一启动就最大化?

方法一:

将应用程序类(CxxAPP)的 InitInstance 函数中的

m_pMainWnd->ShowWindow(SW_SHOW); 改为
m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED); //让窗口一启动就最大化

例:
BOOL CMyEditApp::InitInstance()
{
…………

m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

…………
}

方法二:

在应用程序类(CxxAPP)的 InitInstance 函数中设定 m_nCmdShow的取值.
m_nCmdShow=SW_SHOWMAXIMIZED; //最大化
m_nCmdShow=SW_SHOWMINIMIZED; //最小化
m_nCmdShow=SW_SHOWNORMAL; //正常方式

例:
BOOL CMyEditApp::InitInstance()
{
AfxEnableControlContainer();
m_nCmdShow=SW_SHOWMAXIMIZED; //最大化
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

…………
}


VC学习笔记13:使窗口打开时处于屏幕正中:

在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;
}


VC学习笔记14:修改程序窗口标题:

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

1、修改文档标题:
文档标题是由工程中相应的文档类所控制的,因此我们可以在文档中利用SetTitle()函数来改变文档标题。
在文档类的OnNewDocument()函数中加入语句:
BOOL CWEditDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;

// TODO: add reinitialization code here
// (SDI documents will reuse this document)
SetTitle("未命名");//设置文档标题

return TRUE;
}

2、修改程序标题:
在MainFrm.cpp的PreCreateWindow()函数中加入:
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("记事本");//设置程序标题

cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
| WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_MAXIMIZE;

return TRUE;
}
以上两条比较适合于窗口标题是固定值的情况,或用来设置初始时的窗口标题。若标题经常改变,可采用CWnd类的SetWindowText()函数。

3、修改整个标题:
AfxGetMainWnd()->SetWindowText(m_Title);
m_Title为新标题的字符串变量

一般把该语句放在应用程序类的 InitInstance 函数中。

例:
BOOL CMyEditApp::InitInstance()
{
AfxEnableControlContainer();

…………

m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();

AfxGetMainWnd()->SetWindowText("未命名.txt - 记事本"); //修改标题

return TRUE;
}
该函数也可在视图类中随时修改标题。
Posted on 2005-11-15 14:53 艾凡赫 阅读(1424) 评论(0)  编辑 收藏 引用 所属分类: MFC技术

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理