怎样在退出时保存、启动时恢复窗口大小及位置? 要求考虑窗口最大化或最小化退出时等特殊情况! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SetWindowPlacementGetWindowPlacement 使用文件记录怎样?study! 在CWinApp::ExitInstance中获取窗口大小及位置,保存到文件或注册表中;在CWinApp::InitInstance中恢复。 用函数SetWindowPlacement和GetWindowPlacement可以处理最大化或最小化退出时等特殊情况! 参见vc技术内幕// perist.hclass CPersistentFrame : public CFrameWnd{ // remembers where it was on the desktop DECLARE_DYNAMIC(CPersistentFrame)protected: static const char s_profileHeading[]; static const CRect s_rectDefault; 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(); //{{AFX_VIRTUAL(CPersistentFrame) public: virtual void ActivateFrame(int nCmdShow = -1); protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); //}}AFX_VIRTUAL //{{AFX_MSG(CPersistentFrame) afx_msg void OnDestroy(); //}}AFX_MSG DECLARE_MESSAGE_MAP()}; // persist.cpp Persistent frame class for SDI apps#include "stdafx.h"#include "persist.h"#ifdef _DEBUG#undef THIS_FILEstatic char BASED_CODE THIS_FILE[] = __FILE__;#endif///////////////////////////////////////////////////////////////// CPersistentFrameconst CRect CPersistentFrame::s_rectDefault(10, 10, 500, 400); // staticconst 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) //{{AFX_MSG_MAP(CPersistentFrame) ON_WM_DESTROY() //}}AFX_MSG_MAPEND_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 iconized/maximized status BOOL bRet = SetWindowPlacement(&wndpl); } CFrameWnd::ActivateFrame(nCmdShow);}BOOL CPersistentFrame::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Add your specialized code here // and/or call the base class// cs.dwExStyle |= WS_EX_TOPMOST; // window always on top! return CFrameWnd::PreCreateWindow(cs);} 这是单文档的,你使用的时候,只要继承这个类即可class CMainFrame : public CPersistentFrame{ } Detach用法求教! 获取股市实时行情方法分析 绿色版VC C++2003编译器 免框架.(转) 关于调用COM接口内存泄漏的问题.... 为什么我建立不了新类 关于生成。exe文件 为什么不能开辟内存 键盘钩子问题:我想在我的dll中加如键盘钩子,我希望任何进程调用它的时候,都能响应键盘事件,不需要进程序loadlabary请问如何做?(详细 在ISPro中做的安装程序,如何开始就显示别的窗体? VC++一个十进制转换为二进制的小程序 显式dll调用不成功的问题 我用VC编译程序出现黑屏的现象,请问这是为什么?怎样解决??
GetWindowPlacement
study!
在CWinApp::InitInstance中恢复。
// perist.hclass CPersistentFrame : public CFrameWnd
{ // remembers where it was on the desktop
DECLARE_DYNAMIC(CPersistentFrame)
protected:
static const char s_profileHeading[];
static const CRect s_rectDefault;
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(); //{{AFX_VIRTUAL(CPersistentFrame)
public:
virtual void ActivateFrame(int nCmdShow = -1);
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL //{{AFX_MSG(CPersistentFrame)
afx_msg void OnDestroy();
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};
#include "persist.h"#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////
// CPersistentFrameconst 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)
//{{AFX_MSG_MAP(CPersistentFrame)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
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 iconized/maximized status
BOOL bRet = SetWindowPlacement(&wndpl);
}
CFrameWnd::ActivateFrame(nCmdShow);
}BOOL CPersistentFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here
// and/or call the base class
// cs.dwExStyle |= WS_EX_TOPMOST; // window always on top!
return CFrameWnd::PreCreateWindow(cs);
}
class CMainFrame : public CPersistentFrame
{
}