MFC中的CStatic类是用来显示静态文本信息的。这些信息能够可以作为纯信息(例如,显示在信息对话框中的错误消息), 或作为小的标签等。在Windows应用程序的文件打开对话框中,你会发现有六个这样的标签。
CStatic CStatic CStatic *cs; ... cs = new CStatic(); cs->Create("hello world", WS_CHILD|WS_VISIBLE|SS_CENTER, CRect(50,80, 150, 150), this); 这两行代码是典型的 Create lpszText: rect: pParentWnd: nID: dwStyle: CStatic 所有的控制都有各种显示样式。样式是在用 从 CStatic 这些常数中,“ CStatic 所有的其它样式选项都是可选的,它们控制着标签的外观。在 CStatic 下面的代码对于理解 //static1.cpp #i nclude <afxwin.h> // Declare the application class class CTestApp : public CWinApp { public: virtual BOOL InitInstance(); }; // Create an instance of the application class CTestApp TestApp; // Declare the main window class class CTestWindow : public CFrameWnd { CStatic* cs; public: CTestWindow(); }; // The InitInstance function is called // once when the application first executes BOOL CTestApp::InitInstance() { m_pMainWnd = new CTestWindow(); m_pMainWnd->ShowWindow(m_nCmdShow); m_pMainWnd->UpdateWindow(); return TRUE; } // The constructor for the window class CTestWindow::CTestWindow() { CRect r; // Create the window itself Create(NULL, "CStatic Tests", WS_OVERLAPPEDWINDOW, CRect(0,0,200,200)); // Get the size of the client rectangle GetClientRect(&r); r.InflateRect(-20,-20); // Create a static label cs = new CStatic(); cs->Create("hello world", WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER, r, this); } 下面是窗口构造函数加上了行编号: { CRect r; // Create the window itself 1 Create(NULL, "CStatic Tests", WS_OVERLAPPEDWINDOW, CRect(0,0,200,200)); // Get the size of the client rectangle 2 GetClientRect(&r); 3 r.InflateRect(-20,-20); // Create a static label 4 cs = new CStatic(); 5 cs->Create("hello world", WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER, r, this); } 首先在单击 在第 理解这段代码时可能会有两个问题 在我们的情况下,我们要在窗口中间显示“ 实际上, 通过修改不同的样式属性,你可以理解 CTestWindow::CTestWindow() { CRect r; // Create the window itself Create(NULL, "CStatic Tests", WS_OVERLAPPEDWINDOW, CRect(0,0,200,200)); // Get the size of the client rectangle GetClientRect(&r); r.InflateRect(-20,-20); // Create a static label cs = new CStatic(); cs->Create("Now is the time for all good men to / come to the aid of their country", WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER, r, this); } 上面的代码除了所显示的文本比较长外没有什么不同。运行该代码你就可以看到, 如果边框矩形太小不能包含所有的文本行,则文本会被剪切以适应之。你减小矩形大小或增大字符串长度就可以看到 在我们所看到的所有代码中,样式 SS_LEFTNOWORDWRAP CStatic CStatic 当指定了矩形或框架属性后, 字体 你可以使用 CTestWindow::CTestWindow() { CRect r; // Create the window itself Create(NULL, "CStatic Tests", WS_OVERLAPPEDWINDOW, CRect(0,0,200,200)); // Get the size of the client rectangle GetClientRect(&r); r.InflateRect(-20,-20); // Create a static label cs = new CStatic(); cs->Create("Hello World", WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER, r, this); // Create a new 36 point Arial font font = new CFont; font->CreateFont(36,0,0,0,700,0,0,0, ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "arial"); // Cause the label to use the new font cs->SetFont(font); } 上面的代码开始于建立窗口和 要想更多的了解 SetFont 结论 在本教程中,我们勘察了 查看 在 编译多个可执行程序 在本教程中,有几个例子程序。有两种方式来编译和运行它们。第一种方式是把每个程序都放到自己的目录中,然后为每个程序分别建立一个项目。使用该技术,你可以分别编译每个程序,并且可以同时或独立地使用他们。该方法的缺点是需要比较大的磁盘空间。 第二种方法是为所有的程序只建立一个目录。你可以一个项目文件。为了编译每个程序,你可以编辑项目和改变源文件。当你重新编译项目时,新的可执行程序就是你所选择的源文件的。该方法可以使用减少磁盘空间。
Create函数建立控制时传递给它的dwStyle参数所决定的。对CStatic有效的样式简介如下:CWnd继承来的样式:所必须的。
MFC的CStatic类(转载)
最新推荐文章于 2025-09-17 21:53:07 发布
本文详细介绍了MFC中的CStatic类,包括如何创建和使用CStatic对象,以及如何通过不同的样式属性来改变静态文本的外观。文章还探讨了如何调整文本对齐方式、使用不同的矩形显示模式及如何修改字体。
1342

被折叠的 条评论
为什么被折叠?



