📄 windowmanager.cpp
字号:
#include "stdafx.h"
#include "resource.h"
#include "resrc1.h"
#include "WindowManager.h"
#include "ViewManager.h"
//#include "PopupMenu.h"
#include "MainFrm.h" // TODO: include your main window frame header file here.
#include ".\common\FrameWndEx.h"
#include "generaldata.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Globals
// Please change this to a more useful logo text for your application
static TCHAR szLogoString[] = _T("benBen Browser");
// Helpers for saving/restoring window state
static TCHAR szSection[] = _T("Settings");
static TCHAR szChildWinPos[] = _T("TVChildWinState");
//static TCHAR szLogoFont[] = _T("TVLogoFont");
//static TCHAR szLogoColor[] = _T("TVLogoColor");
static TCHAR szWindowPos[] = _T("TVWindowPos");
//static TCHAR szDispType[] = _T("TVDispType");
//static TCHAR szFileName[] = _T("TVFileName");
//static TCHAR szBackColor[] = _T("TVBackColor");
//static TCHAR szBkBitmap[] = _T("TVBkBitmap");
static TCHAR szFormat[] = _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");
static BOOL ReadWindowPlacement(LPWINDOWPLACEMENT pwp)
{
CString strBuffer = AfxGetApp()->GetProfileString(szSection, szWindowPos);
if (strBuffer.IsEmpty())
return FALSE;
WINDOWPLACEMENT wp;
int nRead = _stscanf(strBuffer, szFormat,
&wp.flags, &wp.showCmd,
&wp.ptMinPosition.x, &wp.ptMinPosition.y,
&wp.ptMaxPosition.x, &wp.ptMaxPosition.y,
&wp.rcNormalPosition.left, &wp.rcNormalPosition.top,
&wp.rcNormalPosition.right, &wp.rcNormalPosition.bottom);
if (nRead != 10)
return FALSE;
wp.length = sizeof(WINDOWPLACEMENT);
*pwp = wp;
return TRUE;
}
static void WriteWindowPlacement(LPWINDOWPLACEMENT pwp)
// write a window placement to settings section of app's ini file
{
TCHAR szBuffer[sizeof("-32767")*8 + sizeof("65535")*2];
wsprintf(szBuffer, szFormat,
pwp->flags, pwp->showCmd,
pwp->ptMinPosition.x, pwp->ptMinPosition.y,
pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);
AfxGetApp()->WriteProfileString(szSection, szWindowPos, szBuffer);
}
///////////////////////////////////////////////////////////
// CDocumentList: class implementation
CDocumentList::CDocumentList()
{
CWinApp* pApp = AfxGetApp();
ASSERT_VALID(pApp);
// Get the first document template position and iterate through the document template
POSITION posDocTemplate = pApp->GetFirstDocTemplatePosition();
while (posDocTemplate != NULL)
{
// For each document template object found ...
CDocTemplate* pDocTemplate = pApp->GetNextDocTemplate(posDocTemplate);
ASSERT_VALID(pDocTemplate);
ASSERT_KINDOF(CDocTemplate, pDocTemplate);
// ...iterate through the template's document list
POSITION posDocument = pDocTemplate->GetFirstDocPosition();
while (posDocument != NULL)
{
// And for each document object found...
CDocument* pDoc = pDocTemplate->GetNextDoc(posDocument);
ASSERT_VALID(pDoc);
ASSERT_KINDOF(CDocument, pDoc);
// ...add the document pointer to the list
AddTail(pDoc);
}
}
// Finally, set the position of the first list member as the current
m_CurPosInDocList = GetHeadPosition();
}
CDocumentList::~CDocumentList()
{
// Out of scope or deleted, remove all document templates...
RemoveAll();
//...set the current position to NULL.
m_CurPosInDocList = NULL;
}
CDocument* CDocumentList::GetNextDocument()
{
if (m_CurPosInDocList == NULL)
return NULL;
CDocument* pDoc = GetNext(m_CurPosInDocList);
ASSERT_VALID(pDoc);
ASSERT_KINDOF(CDocument, pDoc);
return pDoc;
}
/////////////////////////////////////////////////////////////////////////////
// CWindowDlg dialog
CWindowDlg::CWindowDlg(CMDIFrameWnd * pMDIFrame)
: CDialog(CWindowDlg::IDD, pMDIFrame)
{
m_pMDIFrame = pMDIFrame;
//{{AFX_DATA_INIT(CWindowDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CWindowDlg::CWindowDlg(CMDIFrameWnd* pMDIFrame, CWnd* pParentWnd)
: CDialog(CWindowDlg::IDD, pParentWnd)
{
m_pMDIFrame = pMDIFrame;
//{{AFX_DATA_INIT(CWindowDlg)
//}}AFX_DATA_INIT
}
void CWindowDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWindowDlg)
DDX_Control(pDX, IDC_WINDOWLIST_LIST, m_wndList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWindowDlg, CDialog)
//{{AFX_MSG_MAP(CWindowDlg)
ON_BN_CLICKED(IDC_WINDOWLIST_CLOSE, OnClose)
ON_LBN_SELCHANGE(IDC_WINDOWLIST_LIST, OnSelChange)
// ON_BN_CLICKED(IDC_WINDOWLIST_SAVE, OnSave)
ON_BN_CLICKED(IDC_WINDOWLIST_ACTIVATE, OnActivate)
ON_WM_DRAWITEM()
// ON_BN_CLICKED(IDC_WINDOWLIST_TILEHORZ, OnTileHorz)
// ON_BN_CLICKED(IDC_WINDOWLIST_MINIMIZE, OnMinimize)
// ON_BN_CLICKED(IDC_WINDOWLIST_TILEVERT, OnTileVert)
// ON_BN_CLICKED(IDC_WINDOWLIST_CASCADE, OnCascade)
ON_LBN_DBLCLK(IDC_WINDOWLIST_LIST, OnActivate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWindowDlg message handlers
BOOL CWindowDlg::OnInitDialog()
{
CDialog::OnInitDialog();
FillWindowList();
SelActive();
UpdateButtons();
CString wintextsrc;
CString wintextnew;
GetWindowText(wintextsrc);
GetText(wintextsrc,wintextnew,global_item_text);
SetWindowText(wintextnew);
CWnd *wnd=GetDlgItem(IDOK);
if(wnd)
{
wnd->GetWindowText(wintextsrc);
GetText(wintextsrc,wintextnew,global_item_text);
wnd->SetWindowText(wintextnew);
}
wnd=GetDlgItem(IDC_WINDOWLIST_ACTIVATE);
if(wnd)
{
wnd->GetWindowText(wintextsrc);
GetText(wintextsrc,wintextnew,global_item_text);
wnd->SetWindowText(wintextnew);
}
wnd=GetDlgItem(IDC_WINDOWLIST_CLOSE);
if(wnd)
{
wnd->GetWindowText(wintextsrc);
GetText(wintextsrc,wintextnew,global_item_text);
wnd->SetWindowText(wintextnew);
}
wnd=GetDlgItem(IDC_STATIC_SELECT);
if(wnd)
{
wnd->GetWindowText(wintextsrc);
GetText(wintextsrc,wintextnew,global_item_text);
wnd->SetWindowText(wintextnew);
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CWindowDlg::OnClose()
{
int nItems = m_wndList.GetCount();
if (nItems != LB_ERR && nItems > 0)
{
HWND hMDIClient = m_pMDIFrame->m_hWndMDIClient;
m_wndList.SetRedraw(FALSE);
for (int i = nItems - 1; i >= 0; i--)
{
if (m_wndList.GetSel(i) > 0)
{
HWND hWnd = reinterpret_cast < HWND>(m_wndList.GetItemData(i));
::SendMessage(hWnd,WM_COMMAND , static_cast < WPARAM>(ID_FILE_CLOSE),
static_cast < LPARAM>(0));
if (::GetParent(hWnd) == hMDIClient)
break;
}
}
m_wndList.SetRedraw(TRUE);
}
FillWindowList();
SelActive();
UpdateButtons();
}
void CWindowDlg::OnSelChange()
{
UpdateButtons();
}
// Enables/Disables states of buttons
void CWindowDlg::UpdateButtons()
{
int nSel = m_wndList.GetSelCount();
GetDlgItem(IDC_WINDOWLIST_CLOSE)->EnableWindow(nSel > 0);
// GetDlgItem(IDC_WINDOWLIST_SAVE)->EnableWindow(nSel > 0);
// GetDlgItem(IDC_WINDOWLIST_TILEHORZ)->EnableWindow(nSel >= 2);
// GetDlgItem(IDC_WINDOWLIST_TILEVERT)->EnableWindow(nSel >= 2);
// GetDlgItem(IDC_WINDOWLIST_CASCADE)->EnableWindow(nSel >= 2);
// GetDlgItem(IDC_WINDOWLIST_MINIMIZE)->EnableWindow(nSel > 0);
GetDlgItem(IDC_WINDOWLIST_ACTIVATE)->EnableWindow(nSel == 1);
}
// Selects currently active window in listbox
void CWindowDlg::SelActive()
{
int nItems = m_wndList.GetCount();
if (nItems != LB_ERR && nItems > 0)
{
m_wndList.SetRedraw(FALSE);
m_wndList.SelItemRange(FALSE, 0, nItems - 1);
HWND hwndActive = reinterpret_cast < HWND>(::SendMessage(m_pMDIFrame->m_hWndMDIClient,
WM_MDIGETACTIVE, 0, 0));
for (int i = 0; i < nItems; i++)
{
if ((HWND) m_wndList.GetItemData(i) == hwndActive)
{
m_wndList.SetSel(i);
break;
}
}
m_wndList.SetRedraw(TRUE);
}
}
// Saves selected documents
/*void CWindowDlg::OnSave()
{
int nItems = m_wndList.GetCount();
if (nItems != LB_ERR && nItems > 0)
{
for (int i = 0; i < nItems; i++)
{
if (m_wndList.GetSel(i) > 0)
{
HWND hWnd = reinterpret_cast < HWND>(m_wndList.GetItemData(i));
CWnd* pWnd = CWnd::FromHandle(hWnd);
CFrameWnd* pFrame = DYNAMIC_DOWNCAST(CFrameWnd, pWnd);
if (pFrame != NULL)
{
CDocument* pDoc = pFrame->GetActiveDocument();
if (pDoc != NULL)
pDoc->SaveModified();
}
}
}
}
FillWindowList();
SelActive();
UpdateButtons();
}
*/
void CWindowDlg::OnActivate()
{
if (m_wndList.GetSelCount() == 1)
{
int index;
if (m_wndList.GetSelItems(1, &index) == 1)
{
DWORD dw = m_wndList.GetItemData(index);
if (dw != LB_ERR)
{
WINDOWPLACEMENT wndpl;
wndpl.length = sizeof(WINDOWPLACEMENT);
::GetWindowPlacement(reinterpret_cast < HWND>(dw), &wndpl);
if (wndpl.showCmd == SW_SHOWMINIMIZED)
::ShowWindow(reinterpret_cast < HWND>(dw), SW_RESTORE);
::SendMessage(m_pMDIFrame->m_hWndMDIClient, WM_MDIACTIVATE,
static_cast < WPARAM>(dw), 0);
EndDialog(IDOK);
}
}
}
}
// Refresh windows list
void CWindowDlg::FillWindowList(void)
{
m_wndList.SetRedraw(FALSE);
m_wndList.ResetContent();
HWND hwndT;
hwndT = ::GetWindow(m_pMDIFrame->m_hWndMDIClient, GW_CHILD);
while (hwndT != NULL)
{
TCHAR szWndTitle[_MAX_PATH];
::GetWindowText(hwndT, szWndTitle, sizeof(szWndTitle)/sizeof(szWndTitle[0]));
int index = m_wndList.AddString(szWndTitle);
m_wndList.SetItemData(index, reinterpret_cast < DWORD>(hwndT));
hwndT = ::GetWindow(hwndT, GW_HWNDNEXT);
}
m_wndList.SetRedraw(TRUE);
}
// Draws listbox item
void CWindowDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS)
{
if (nIDCtl == IDC_WINDOWLIST_LIST)
{
if (lpDIS->itemID == LB_ERR)
return;
HBRUSH brBackground;
RECT rcTemp = lpDIS->rcItem;
HDC hDC = lpDIS->hDC;
COLORREF clText;
if (lpDIS->itemState & ODS_SELECTED)
{
brBackground = GetSysColorBrush(COLOR_HIGHLIGHT);
clText = GetSysColor(COLOR_HIGHLIGHTTEXT);
}
else
{
brBackground = GetSysColorBrush(COLOR_WINDOW);
clText = GetSysColor(COLOR_WINDOWTEXT);
}
if (lpDIS->itemAction &(ODA_DRAWENTIRE | ODA_SELECT))
FillRect(hDC, &rcTemp, brBackground);
int OldBkMode = ::SetBkMode(hDC, TRANSPARENT);
COLORREF clOldText = ::SetTextColor(hDC, clText);
TCHAR szBuf[1024];
::SendMessage(lpDIS->hwndItem, LB_GETTEXT, static_cast < WPARAM>(lpDIS->itemID),
reinterpret_cast < LPARAM>(szBuf));
int h = rcTemp.bottom - rcTemp.top;
rcTemp.left += h + 4;
DrawText(hDC, szBuf, -1, &rcTemp, DT_LEFT | DT_VCENTER |
DT_NOPREFIX | DT_SINGLELINE);
HICON hIcon = reinterpret_cast < HICON>
(::GetClassLong(reinterpret_cast < HWND>(lpDIS->itemData), GCL_HICONSM));
//AfxGetApp()->LoadStandardIcon(IDI_HAND);
//(HICON) ::SendMessage((HWND) lpDIS->itemData,WM_GETICON,(WPARAM)ICON_BIG,(LPARAM) 0);
rcTemp.left = lpDIS->rcItem.left;
::DrawIconEx(hDC, rcTemp.left + 2, rcTemp.top, hIcon, h, h, 0, 0, DI_NORMAL);
::SetTextColor(hDC, clOldText);
::SetBkMode(hDC, OldBkMode);
if (lpDIS->itemAction & ODA_FOCUS)
DrawFocusRect(hDC, &lpDIS->rcItem);
return;
}
CDialog::OnDrawItem(nIDCtl, lpDIS);
}
void CWindowDlg::MDIMessage(UINT uMsg, WPARAM flag)
{
int nItems = m_wndList.GetCount();
if (nItems != LB_ERR && nItems > 0)
{
HWND hMDIClient = m_pMDIFrame->m_hWndMDIClient;
::LockWindowUpdate(hMDIClient);
for (int i = nItems - 1; i >= 0; i--)
{
HWND hWnd = reinterpret_cast < HWND>(m_wndList.GetItemData(i));
if (m_wndList.GetSel(i) > 0)
::ShowWindow(hWnd, SW_RESTORE);
else
::ShowWindow(hWnd, SW_MINIMIZE);
}
::SendMessage(hMDIClient, uMsg, flag, 0);
::LockWindowUpdate(NULL);
}
}
/*
void CWindowDlg::OnTileHorz()
{
MDIMessage(WM_MDITILE, MDITILE_HORIZONTAL);
}
void CWindowDlg::OnTileVert()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -