persistantsize.cpp

来自「用VC實現截取Window的消息」· C++ 代码 · 共 48 行

CPP
48
字号
#include "stdafx.h"
#include "PersistantSize.h"
#include "SubclassExample.h"

extern CExampleApp theApp;

// Constructor
// All that we do here is save the section name which will be used
// later when persisting the windows position.

CPersistantSize::CPersistantSize(HWND hWnd, LPCTSTR szSection)
	: m_sSection(szSection)
{
	SubclassWindow(hWnd);
	CRect rect;
	if ((rect.left = theApp.GetProfileInt(m_sSection, "Left", -1)) == -1)
		return;
	if ((rect.right = theApp.GetProfileInt(m_sSection, "Right", -1)) == -1)
		return;
	if ((rect.top = theApp.GetProfileInt(m_sSection, "Top", -1)) == -1)
		return;
	if ((rect.bottom = theApp.GetProfileInt(m_sSection, "Bottom", -1)) == -1)
		return;
	MoveWindow(hWnd, rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
}

void CPersistantSize::OnFinalMessage()
{
	delete this;
}

// OnDestroy
// This is the "magic" mechanism that allows us to have peristant sizes.
// This is a message handler for the WM_DESTROY message received by
// the window we've "hooked".

void CPersistantSize::OnDestroy()
{
	HWND hWnd = GetHandle();
	CRect rect;
	GetWindowRect(hWnd, &rect);
	theApp.WriteProfileInt(m_sSection, "Left", rect.left);
	theApp.WriteProfileInt(m_sSection, "Right", rect.right);
	theApp.WriteProfileInt(m_sSection, "Top", rect.top);
	theApp.WriteProfileInt(m_sSection, "Bottom", rect.bottom);
	DefWindowProc();	// Make sure to call the "base" functionality
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?