Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید








The Ex14a Example: Using a Persistent Frame Window Class


The Ex14a program illustrates the use of a persistent frame window class, CPersistentFrame. The following code shows the contents of the files

Persist.h and

Persist.cpp , which are included in the Ex14a project on the companion CD. In this example, we'll insert the new frame class into an MFC Application Wizard– generated SDI application. Ex14a is a "do-nothing" application, but you can insert the persistent frame class into any of your own SDI "do-something" applications.

Persist.h






// Persist.h
#ifndef _INSIDE_VISUAL_CPP_PERSISTENT_FRAME
#define _INSIDE_VISUAL_CPP_PERSISTENT_FRAME
class CPersistentFrame : public CFrameWnd
{ // remembers where it was on the desktop
DECLARE_DYNAMIC(CPersistentFrame)
private:
static const CRect s_rectDefault;
static const char s_profileHeading[];
static const char s_profileRect[];
static const char s_profileIcon[];
static const char s_profileMax[];
static const char s_profileTool[];
static const char s_profileStatus[];
BOOL m_bFirstTime;
protected: // Create from serialization only
CPersistentFrame();
~CPersistentFrame();
public:
virtual void ActivateFrame(int nCmdShow = -1);
protected:
afx_msg void OnDestroy();
DECLARE_MESSAGE_MAP()
};
#endif // _INSIDE_VISUAL_CPP_PERSISTENT_FRAME












Persist.cpp






// Persist.cpp Persistent frame class for SDI apps
#include "stdafx.h"
#include "persist.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////
// CPersistentFrame
const CRect CPersistentFrame::s_rectDefault(10, 10,
500, 400); // static
const char CPersistentFrame::s_profileHeading[] = "Window size";
const char CPersistentFrame::s_profileRect[] = "Rect";
const char CPersistentFrame::s_profileIcon[] = "icon";
const char CPersistentFrame::s_profileMax[] = "max";
const char CPersistentFrame::s_profileTool[] = "tool";
const char CPersistentFrame::s_profileStatus[] = "status";
IMPLEMENT_DYNAMIC(CPersistentFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CPersistentFrame, CFrameWnd)
ON_WM_DESTROY()
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////
CPersistentFrame::CPersistentFrame(){
m_bFirstTime = TRUE;
}
///////////////////////////////////////////////////////////////
CPersistentFrame::~CPersistentFrame()
{
}
///////////////////////////////////////////////////////////////
void CPersistentFrame::OnDestroy()
{
CString strText;
BOOL bIconic, bMaximized;
WINDOWPLACEMENT wndpl;
wndpl.length = sizeof(WINDOWPLACEMENT);
// gets current window position and
// iconized/maximized status
BOOL bRet = GetWindowPlacement(&wndpl);
if (wndpl.showCmd == SW_SHOWNORMAL) {
bIconic = FALSE;
bMaximized = FALSE;
}
else if (wndpl.showCmd == SW_SHOWMAXIMIZED) {
bIconic = FALSE;
bMaximized = TRUE;
}
else if (wndpl.showCmd == SW_SHOWMINIMIZED) {
bIconic = TRUE;
if (wndpl.flags) {
bMaximized = TRUE;
}
else {
bMaximized = FALSE;
}
}
strText.Format("%04d %04d %04d %04d",
wndpl.rcNormalPosition.left,
wndpl.rcNormalPosition.top,
wndpl.rcNormalPosition.right,
wndpl.rcNormalPosition.bottom);
AfxGetApp()->WriteProfileString(s_profileHeading,
s_profileRect, strText);
AfxGetApp()->WriteProfileInt(s_profileHeading,
s_profileIcon, bIconic);
AfxGetApp()->WriteProfileInt(s_profileHeading,
s_profileMax, bMaximized);
SaveBarState(AfxGetApp()->m_pszProfileName);
CFrameWnd::OnDestroy();
}
///////////////////////////////////////////////////////////////
void CPersistentFrame::ActivateFrame(int nCmdShow)
{
CString strText;
BOOL bIconic, bMaximized;
UINT flags;
WINDOWPLACEMENT wndpl;
CRect rect;
if (m_bFirstTime) {
m_bFirstTime = FALSE;
strText = AfxGetApp()->GetProfileString(s_profileHeading,
s_profileRect);
if (!strText.IsEmpty()) {
rect.left = atoi((const char*) strText);
rect.top = atoi((const char*) strText + 5);
rect.right = atoi((const char*) strText + 10);
rect.bottom = atoi((const char*) strText + 15);
}
else {
rect = s_rectDefault;
}
bIconic = AfxGetApp()->GetProfileInt(s_profileHeading,
s_profileIcon, 0);
bMaximized = AfxGetApp()->GetProfileInt(s_profileHeading,
s_profileMax, 0);
if (bIconic) {
nCmdShow = SW_SHOWMINNOACTIVE;
if (bMaximized) {
flags = WPF_RESTORETOMAXIMIZED;
}
else {
flags = WPF_SETMINPOSITION;
}
}
else {
if (bMaximized) {
nCmdShow = SW_SHOWMAXIMIZED;
flags = WPF_RESTORETOMAXIMIZED;
}
else {
nCmdShow = SW_NORMAL;
flags = WPF_SETMINPOSITION;
}
}
wndpl.length = sizeof(WINDOWPLACEMENT);
wndpl.showCmd = nCmdShow;
wndpl.flags = flags;
wndpl.ptMinPosition = CPoint(0, 0);
wndpl.ptMaxPosition =
CPoint(-::GetSystemMetrics(SM_CXBORDER),
-::GetSystemMetrics(SM_CYBORDER));
wndpl.rcNormalPosition = rect;
LoadBarState(AfxGetApp()->m_pszProfileName);
// sets window's position and minimized/maximized status
BOOL bRet = SetWindowPlacement(&wndpl);
}
CFrameWnd::ActivateFrame(nCmdShow);
}











Here are the steps for building the Ex14a application:



  1. Run the MFC Application Wizard to generate the Ex14a project. Accept all default settings but two: Select Single Document and deselect Printing And Print Preview.



  2. Modify

    MainFrm.h . You must change the base class of CMainFrame.

    To do this, simply change the line

    class CMainFrame : public CFrameWnd

    to

    class CMainFrame : public CPersistentFrame

    Also, add this line:

    #include "persist.h"



  3. Modify

    MainFrm.cpp . Globally replace all occurrences of CFrameWnd with CPersistentFrame.



  4. Modify

    Ex14a.cpp . Replace the line

    SetRegistryKey(_T("Local AppWizard-Generated Applications"));

    with this line:

    SetRegistryKey("Programming Visual C++ .NET");



  5. Add the

    Persist.cpp file to the project. You can type in the

    Persist.h and

    Persist.cpp files from the previous code listing, or you can copy the files from the companion CD. Having the files in the \vcppnet\Ex14a directory is not sufficient. You must add the names of the files to the solution. Choose Add Existing Item from Visual C++ .NET's Project menu, and select

    Persist.h and

    Persist.cpp from the list.



  6. Build and test the Ex14a application. Size and move the application's frame window, and then close the application. When you restart the application, does its window open at the same location at which it was closed? Experiment with maximizing and minimizing, and then change the status and position of the control bars. Does the persistent frame remember its settings?



  7. Examine the Windows Registry. Run the Windows Regedit.exe program. Navigate to the HKEY_CURRENT_USER\Software\Programming Visual C++ .NET\Ex14a key. You should see data values similar to those shown here:


    Notice the relationship between the Registry key and the SetRegistryKey function parameter, "Programming Visual C++ .NET". If you supply an empty string as the SetRegistryKey parameter, the program name (Ex14a, in this case) will be positioned directly below the Software key.




/ 319