WindowManager.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include "stdafx.h"
  2. #include "resource.h"
  3. #include "WindowManager.h"
  4. #include "ViewManager.h"
  5. //#include "PopupMenu.h"
  6. #include "MainFrm.h" // TODO: include your main window frame header file here.
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // Globals
  14. // Please change this to a more useful logo text for your application
  15. static TCHAR szLogoString[] = _T("");
  16. // Helpers for saving/restoring window state
  17. static TCHAR szSection[] = _T("Settings");
  18. static TCHAR szWindowPos[] = _T("WindowPos");
  19. static TCHAR szFormat[] = _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");
  20. static BOOL ReadWindowPlacement(LPWINDOWPLACEMENT pwp)
  21. {
  22. CString strBuffer = AfxGetApp()->GetProfileString(szSection, szWindowPos);
  23. if (strBuffer.IsEmpty())
  24. return FALSE;
  25. WINDOWPLACEMENT wp;
  26. int nRead = _stscanf(strBuffer, szFormat,
  27. &wp.flags, &wp.showCmd,
  28. &wp.ptMinPosition.x, &wp.ptMinPosition.y,
  29. &wp.ptMaxPosition.x, &wp.ptMaxPosition.y,
  30. &wp.rcNormalPosition.left, &wp.rcNormalPosition.top,
  31. &wp.rcNormalPosition.right, &wp.rcNormalPosition.bottom);
  32. if (nRead != 10)
  33. return FALSE;
  34. wp.length = sizeof wp;
  35. *pwp = wp;
  36. return TRUE;
  37. }
  38. static void WriteWindowPlacement(LPWINDOWPLACEMENT pwp)
  39. // write a window placement to settings section of app's ini file
  40. {
  41. TCHAR szBuffer[sizeof("-32767")*8 + sizeof("65535")*2];
  42. wsprintf(szBuffer, szFormat,
  43. pwp->flags, pwp->showCmd,
  44. pwp->ptMinPosition.x, pwp->ptMinPosition.y,
  45. pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
  46. pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
  47. pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);
  48. AfxGetApp()->WriteProfileString(szSection, szWindowPos, szBuffer);
  49. }
  50. ///////////////////////////////////////////////////////////
  51. // CDocumentList: class implementation
  52. CDocumentList::CDocumentList()
  53. {
  54. CWinApp* pApp = AfxGetApp();
  55. ASSERT_VALID(pApp);
  56. // Get the first document template position and iterate through the document template
  57. POSITION posDocTemplate = pApp->GetFirstDocTemplatePosition();
  58. while (posDocTemplate != NULL)
  59. {
  60. // For each document template object found ...
  61. CDocTemplate* pDocTemplate = pApp->GetNextDocTemplate(posDocTemplate);
  62. ASSERT_VALID(pDocTemplate);
  63. ASSERT_KINDOF(CDocTemplate, pDocTemplate);
  64. // ...iterate through the template's document list
  65. POSITION posDocument = pDocTemplate->GetFirstDocPosition();
  66. while (posDocument != NULL)
  67. {
  68. // And for each document object found...
  69. CDocument* pDoc = pDocTemplate->GetNextDoc(posDocument);
  70. ASSERT_VALID(pDoc);
  71. ASSERT_KINDOF(CDocument, pDoc);
  72. // ...add the document pointer to the list
  73. AddTail(pDoc);
  74. }
  75. }
  76. // Finally, set the position of the first list member as the current
  77. m_CurPosInDocList = GetHeadPosition();
  78. }
  79. CDocumentList::~CDocumentList()
  80. {
  81. // Out of scope or deleted, remove all document templates...
  82. RemoveAll();
  83. //...set the current position to NULL.
  84. m_CurPosInDocList = NULL;
  85. }
  86. CDocument* CDocumentList::GetNextDocument()
  87. {
  88. if (m_CurPosInDocList == NULL)
  89. return NULL;
  90. CDocument* pDoc = GetNext(m_CurPosInDocList);
  91. ASSERT_VALID(pDoc);
  92. ASSERT_KINDOF(CDocument, pDoc);
  93. return pDoc;
  94. }
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CWindowDlg dialog
  97. CWindowDlg::CWindowDlg(CMDIFrameWnd * pMDIFrame)
  98. : CDialog(CWindowDlg::IDD, pMDIFrame)
  99. {
  100. m_pMDIFrame = pMDIFrame;
  101. //{{AFX_DATA_INIT(CWindowDlg)
  102. // NOTE: the ClassWizard will add member initialization here
  103. //}}AFX_DATA_INIT
  104. }
  105. void CWindowDlg::DoDataExchange(CDataExchange* pDX)
  106. {
  107. CDialog::DoDataExchange(pDX);
  108. //{{AFX_DATA_MAP(CWindowDlg)
  109. DDX_Control(pDX, IDC_WINDOWLIST_LIST, m_wndList);
  110. //}}AFX_DATA_MAP
  111. }
  112. BEGIN_MESSAGE_MAP(CWindowDlg, CDialog)
  113. //{{AFX_MSG_MAP(CWindowDlg)
  114. ON_BN_CLICKED(IDC_WINDOWLIST_CLOSE, OnClose)
  115. ON_LBN_SELCHANGE(IDC_WINDOWLIST_LIST, OnSelChange)
  116. ON_BN_CLICKED(IDC_WINDOWLIST_SAVE, OnSave)
  117. ON_BN_CLICKED(IDC_WINDOWLIST_ACTIVATE, OnActivate)
  118. ON_WM_DRAWITEM()
  119. ON_BN_CLICKED(IDC_WINDOWLIST_TILEHORZ, OnTileHorz)
  120. ON_BN_CLICKED(IDC_WINDOWLIST_MINIMIZE, OnMinimize)
  121. ON_BN_CLICKED(IDC_WINDOWLIST_TILEVERT, OnTileVert)
  122. ON_BN_CLICKED(IDC_WINDOWLIST_CASCADE, OnCascade)
  123. ON_LBN_DBLCLK(IDC_WINDOWLIST_LIST, OnActivate)
  124. //}}AFX_MSG_MAP
  125. END_MESSAGE_MAP()
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CWindowDlg message handlers
  128. BOOL CWindowDlg::OnInitDialog()
  129. {
  130. CDialog::OnInitDialog();
  131. FillWindowList();
  132. SelActive();
  133. UpdateButtons();
  134. return TRUE; // return TRUE unless you set the focus to a control
  135. // EXCEPTION: OCX Property Pages should return FALSE
  136. }
  137. void CWindowDlg::OnClose()
  138. {
  139. int nItems = m_wndList.GetCount();
  140. if (nItems != LB_ERR && nItems > 0)
  141. {
  142. HWND hMDIClient = m_pMDIFrame->m_hWndMDIClient;
  143. m_wndList.SetRedraw(FALSE);
  144. for (int i = nItems - 1; i >= 0; i--)
  145. {
  146. if (m_wndList.GetSel(i) > 0)
  147. {
  148. HWND hWnd = reinterpret_cast<HWND>(m_wndList.GetItemData(i));
  149. ::SendMessage(hWnd, WM_CLOSE, static_cast<WPARAM>(0), static_cast<LPARAM>(0));
  150. if(::GetParent(hWnd) == hMDIClient)
  151. break;
  152. }
  153. }
  154. m_wndList.SetRedraw(TRUE);
  155. }
  156. FillWindowList();
  157. SelActive();
  158. UpdateButtons();
  159. }
  160. void CWindowDlg::OnSelChange()
  161. {
  162. UpdateButtons();
  163. }
  164. // Enables/Disables states of buttons
  165. void CWindowDlg::UpdateButtons()
  166. {
  167. int nSel=m_wndList.GetSelCount();
  168. GetDlgItem(IDC_WINDOWLIST_CLOSE)->EnableWindow(nSel > 0);
  169. GetDlgItem(IDC_WINDOWLIST_SAVE)->EnableWindow(nSel > 0);
  170. GetDlgItem(IDC_WINDOWLIST_TILEHORZ)->EnableWindow(nSel >= 2);
  171. GetDlgItem(IDC_WINDOWLIST_TILEVERT)->EnableWindow(nSel >= 2);
  172. GetDlgItem(IDC_WINDOWLIST_CASCADE)->EnableWindow(nSel >= 2);
  173. GetDlgItem(IDC_WINDOWLIST_MINIMIZE)->EnableWindow(nSel > 0);
  174. GetDlgItem(IDC_WINDOWLIST_ACTIVATE)->EnableWindow(nSel == 1);
  175. }
  176. // Selects currently active window in listbox
  177. void CWindowDlg::SelActive()
  178. {
  179. int nItems = m_wndList.GetCount();
  180. if (nItems != LB_ERR && nItems > 0)
  181. {
  182. m_wndList.SetRedraw(FALSE);
  183. m_wndList.SelItemRange(FALSE, 0, nItems - 1);
  184. HWND hwndActive = reinterpret_cast<HWND>(::SendMessage(m_pMDIFrame->m_hWndMDIClient,
  185. WM_MDIGETACTIVE, 0, 0));
  186. for (int i = 0; i < nItems; i++)
  187. {
  188. if ((HWND) m_wndList.GetItemData(i) == hwndActive)
  189. {
  190. m_wndList.SetSel(i);
  191. break;
  192. }
  193. }
  194. m_wndList.SetRedraw(TRUE);
  195. }
  196. }
  197. // Saves selected documents
  198. void CWindowDlg::OnSave()
  199. {
  200. int nItems = m_wndList.GetCount();
  201. if (nItems != LB_ERR && nItems > 0)
  202. {
  203. for (int i = 0; i < nItems; i++)
  204. {
  205. if (m_wndList.GetSel(i) > 0)
  206. {
  207. HWND hWnd = reinterpret_cast<HWND>(m_wndList.GetItemData(i));
  208. CWnd* pWnd = CWnd::FromHandle(hWnd);
  209. CFrameWnd* pFrame = DYNAMIC_DOWNCAST(CFrameWnd, pWnd);
  210. if (pFrame != NULL)
  211. {
  212. CDocument* pDoc = pFrame->GetActiveDocument();
  213. if (pDoc != NULL)
  214. pDoc->SaveModified();
  215. }
  216. }
  217. }
  218. }
  219. FillWindowList();
  220. SelActive();
  221. UpdateButtons();
  222. }
  223. void CWindowDlg::OnActivate()
  224. {
  225. if (m_wndList.GetSelCount() == 1)
  226. {
  227. int index;
  228. if (m_wndList.GetSelItems(1, &index) == 1)
  229. {
  230. DWORD dw = m_wndList.GetItemData(index);
  231. if (dw != LB_ERR)
  232. {
  233. WINDOWPLACEMENT wndpl;
  234. ::GetWindowPlacement(reinterpret_cast<HWND>(dw), &wndpl);
  235. if (wndpl.showCmd == SW_SHOWMINIMIZED)
  236. ::ShowWindow(reinterpret_cast<HWND>(dw), SW_RESTORE);
  237. ::SendMessage(m_pMDIFrame->m_hWndMDIClient, WM_MDIACTIVATE,
  238. static_cast<WPARAM>(dw), 0);
  239. EndDialog(IDOK);
  240. }
  241. }
  242. }
  243. }
  244. // Refresh windows list
  245. void CWindowDlg::FillWindowList(void)
  246. {
  247. m_wndList.SetRedraw(FALSE);
  248. m_wndList.ResetContent();
  249. HWND hwndT;
  250. hwndT = ::GetWindow(m_pMDIFrame->m_hWndMDIClient, GW_CHILD);
  251. while (hwndT != NULL)
  252. {
  253. TCHAR szWndTitle[_MAX_PATH];
  254. ::GetWindowText(hwndT, szWndTitle, sizeof(szWndTitle)/sizeof(szWndTitle[0]));
  255. int index=m_wndList.AddString(szWndTitle);
  256. m_wndList.SetItemData(index, reinterpret_cast<DWORD>(hwndT));
  257. hwndT = ::GetWindow(hwndT, GW_HWNDNEXT);
  258. }
  259. m_wndList.SetRedraw(TRUE);
  260. }
  261. // Draws listbox item
  262. void CWindowDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS)
  263. {
  264. if (nIDCtl == IDC_WINDOWLIST_LIST)
  265. {
  266. if (lpDIS->itemID == LB_ERR)
  267. return;
  268. HBRUSH brBackground;
  269. RECT rcTemp = lpDIS->rcItem;
  270. HDC hDC = lpDIS->hDC;
  271. COLORREF clText;
  272. if (lpDIS->itemState & ODS_SELECTED)
  273. {
  274. brBackground = GetSysColorBrush (COLOR_HIGHLIGHT);
  275. clText = GetSysColor(COLOR_HIGHLIGHTTEXT);
  276. }
  277. else
  278. {
  279. brBackground = GetSysColorBrush (COLOR_WINDOW);
  280. clText = GetSysColor(COLOR_WINDOWTEXT);
  281. }
  282. if (lpDIS->itemAction & (ODA_DRAWENTIRE | ODA_SELECT))
  283. FillRect(hDC, &rcTemp, brBackground);
  284. int OldBkMode = ::SetBkMode(hDC, TRANSPARENT);
  285. COLORREF clOldText = ::SetTextColor(hDC, clText);
  286. TCHAR szBuf[1024];
  287. ::SendMessage(lpDIS->hwndItem, LB_GETTEXT, static_cast<WPARAM>(lpDIS->itemID),
  288. reinterpret_cast<LPARAM>(szBuf));
  289. int h = rcTemp.bottom - rcTemp.top;
  290. rcTemp.left += h + 4;
  291. DrawText(hDC, szBuf, -1, &rcTemp, DT_LEFT | DT_VCENTER |
  292. DT_NOPREFIX | DT_SINGLELINE);
  293. HICON hIcon = reinterpret_cast<HICON>
  294. (::GetClassLong(reinterpret_cast<HWND>(lpDIS->itemData), GCL_HICONSM));
  295. /*AfxGetApp()->LoadStandardIcon(IDI_HAND);*/
  296. //(HICON) ::SendMessage((HWND) lpDIS->itemData,WM_GETICON,(WPARAM)ICON_BIG,(LPARAM) 0);
  297. rcTemp.left = lpDIS->rcItem.left;
  298. ::DrawIconEx(hDC, rcTemp.left + 2, rcTemp.top, hIcon, h, h, 0, 0, DI_NORMAL);
  299. ::SetTextColor(hDC, clOldText);
  300. ::SetBkMode(hDC, OldBkMode);
  301. if (lpDIS->itemAction & ODA_FOCUS)
  302. DrawFocusRect(hDC, &lpDIS->rcItem);
  303. return;
  304. }
  305. CDialog::OnDrawItem(nIDCtl, lpDIS);
  306. }
  307. void CWindowDlg::MDIMessage(UINT uMsg, WPARAM flag)
  308. {
  309. int nItems = m_wndList.GetCount();
  310. if (nItems != LB_ERR && nItems > 0)
  311. {
  312. HWND hMDIClient = m_pMDIFrame->m_hWndMDIClient;
  313. ::LockWindowUpdate(hMDIClient);
  314. for (int i = nItems - 1; i >= 0; i--)
  315. {
  316. HWND hWnd = reinterpret_cast<HWND>(m_wndList.GetItemData(i));
  317. if (m_wndList.GetSel(i) > 0)
  318. ::ShowWindow(hWnd, SW_RESTORE);
  319. else
  320. ::ShowWindow(hWnd, SW_MINIMIZE);
  321. }
  322. ::SendMessage(hMDIClient, uMsg, flag, 0);
  323. ::LockWindowUpdate(NULL);
  324. }
  325. }
  326. void CWindowDlg::OnTileHorz()
  327. {
  328. MDIMessage(WM_MDITILE, MDITILE_HORIZONTAL);
  329. }
  330. void CWindowDlg::OnTileVert()
  331. {
  332. MDIMessage(WM_MDITILE, MDITILE_VERTICAL);
  333. }
  334. void CWindowDlg::OnMinimize()
  335. {
  336. int nItems = m_wndList.GetCount();
  337. if (nItems != LB_ERR && nItems > 0)
  338. {
  339. m_wndList.SetRedraw(FALSE);
  340. for (int i = nItems - 1; i >= 0; i--)
  341. {
  342. if (m_wndList.GetSel(i) > 0)
  343. {
  344. HWND hWnd = reinterpret_cast<HWND>(m_wndList.GetItemData(i));
  345. ::ShowWindow(hWnd, SW_MINIMIZE);
  346. }
  347. }
  348. m_wndList.SetRedraw(TRUE);
  349. }
  350. FillWindowList();
  351. SelActive();
  352. UpdateButtons();
  353. }
  354. void CWindowDlg::OnCascade()
  355. {
  356. MDIMessage(WM_MDICASCADE, 0);
  357. }
  358. /////////////////////////////////////////////////////////////////////////////
  359. // CMDIClient
  360. CMDIClient::CMDIClient() : m_sizeClient(0, 0), m_bFullScreen(FALSE), m_bFirstTime(TRUE)
  361. {
  362. m_hMenuWindow = 0;
  363. m_strWindows.LoadString(IDS_WINDOW_MANAGE);
  364. m_pMDIFrame = NULL;
  365. m_crBkColor = GetSysColor(WHITE_BRUSH);
  366. m_bToolBarVisible = FALSE;
  367. m_bStatusBarVisible = FALSE;
  368. m_bMaxChild = FALSE;
  369. m_pwndFullScrnToolBar = NULL;
  370. m_bMaxChild = AfxGetApp()->GetProfileInt(_T("Settings"), _T("TVChildWinState"), m_bMaxChild);
  371. }
  372. CMDIClient::~CMDIClient()
  373. {
  374. }
  375. BEGIN_MESSAGE_MAP(CMDIClient, CWnd)
  376. //{{AFX_MSG_MAP(CMDIClient)
  377. ON_MESSAGE(WM_MDIREFRESHMENU,OnRefreshMenu)
  378. ON_MESSAGE(WM_MDISETMENU,OnSetMenu)
  379. ON_WM_ERASEBKGND()
  380. ON_WM_SIZE()
  381. ON_WM_RBUTTONDOWN()
  382. ON_COMMAND(ID_WINDOW_MANAGE, OnWindowManage)
  383. ON_COMMAND(ID_WINDOW_NEXT, OnWindowNext)
  384. ON_COMMAND(ID_WINDOW_PREVIOUS, OnWindowPrevious)
  385. ON_COMMAND(ID_VIEW_VIEWTAB, OnTabView)
  386. ON_UPDATE_COMMAND_UI(ID_VIEW_VIEWTAB, OnUpdateTabView)
  387. ON_UPDATE_COMMAND_UI(ID_WINDOW_MANAGE, OnUpdateWindowManage)
  388. ON_MESSAGE(WM_MDICREATE, OnMDICreate)
  389. ON_MESSAGE(WM_MDIDESTROY, OnMDIDestroy)
  390. //}}AFX_MSG_MAP
  391. ON_UPDATE_COMMAND_UI(ID_WINDOW_NEXT, OnUpdateWindowManage) // Just cheating!!!
  392. ON_UPDATE_COMMAND_UI(ID_WINDOW_PREVIOUS, OnUpdateWindowManage)
  393. ON_UPDATE_COMMAND_UI(ID_WINDOW_SAVE_ALL, OnUpdateWindowManage)
  394. ON_UPDATE_COMMAND_UI(ID_WINDOW_CLOSE_ALL, OnUpdateWindowManage)
  395. END_MESSAGE_MAP()
  396. BOOL CMDIClient::SubclassMDIClient(CMDIFrameWnd* pMDIFrameWnd,
  397. CViewManager* pViewManager, UINT uID)
  398. {
  399. // Subclass the MDI client window
  400. VERIFY(this->SubclassWindow(pMDIFrameWnd->m_hWndMDIClient));
  401. pViewManager->CreateViewManager(pMDIFrameWnd, uID);
  402. m_pViewManager = pViewManager;
  403. m_pMDIFrame = reinterpret_cast<CMyMainFrameClass*>(pMDIFrameWnd); //TODO
  404. return TRUE;
  405. }
  406. /////////////////////////////////////////////////////////////////////////////
  407. // CMDIClient message handlers
  408. void CMDIClient::OnTabView()
  409. {
  410. ASSERT(m_pMDIFrame != NULL);
  411. ASSERT(m_pViewManager != NULL);
  412. m_pMDIFrame->ShowControlBar(m_pViewManager,
  413. (m_pViewManager->GetStyle() & WS_VISIBLE) == 0, FALSE);
  414. }
  415. void CMDIClient::OnUpdateTabView(CCmdUI* pCmdUI)
  416. {
  417. ASSERT(m_pMDIFrame != NULL);
  418. ASSERT(m_pViewManager != NULL);
  419. pCmdUI->SetCheck((m_pViewManager->GetStyle() & WS_VISIBLE) != 0);
  420. }
  421. LRESULT CMDIClient::OnSetMenu(WPARAM wParam, LPARAM lParam)
  422. {
  423. #if 0
  424. // LRESULT lResult = Default(); // This does not prevent level 4 warning...
  425. LRESULT lResult = DefWindowProc(WM_MDISETMENU, wParam, lParam);
  426. // Remember Window submenu handle
  427. m_hMenuWindow = reinterpret_cast<HMENU>(lParam);
  428. // Refresh window submenu
  429. SendMessage(WM_MDIREFRESHMENU);
  430. return lResult;
  431. #endif
  432. return 0;
  433. }
  434. LRESULT CMDIClient::OnRefreshMenu(WPARAM wParam, LPARAM lParam)
  435. {
  436. #if 0
  437. // LRESULT lResult = Default(); // This does not prevent level 4 warning...
  438. LRESULT lResult = DefWindowProc(WM_MDIREFRESHMENU, wParam, lParam);
  439. HMENU hMenu = m_hMenuWindow;
  440. if (hMenu != NULL)
  441. {
  442. UINT uState = ::GetMenuState(hMenu, AFX_IDM_FIRST_MDICHILD + 9, MF_BYCOMMAND);
  443. if (uState == 0xFFFFFFFF)
  444. uState = 0;
  445. // Remove old MDI 'Windows...' command
  446. while (::RemoveMenu(hMenu, AFX_IDM_FIRST_MDICHILD + 9, MF_BYCOMMAND))
  447. ;
  448. // Remove 'Windows' command
  449. while (::RemoveMenu(hMenu, ID_WINDOW_MANAGE, MF_BYCOMMAND))
  450. ;
  451. ::AppendMenu(hMenu, MF_STRING | uState, ID_WINDOW_MANAGE, m_strWindows);
  452. }
  453. return lResult;
  454. #endif
  455. return 0;
  456. }
  457. void CMDIClient::OnWindowManage()
  458. {
  459. ManageWindows(m_pMDIFrame);
  460. }
  461. void CMDIClient::OnUpdateWindowManage(CCmdUI* pCmdUI)
  462. {
  463. pCmdUI->Enable(m_pViewManager->GetWindowNum());
  464. }
  465. void CMDIClient::ManageWindows(CMDIFrameWnd* pMDIFrame)
  466. {
  467. CWindowDlg dlg(pMDIFrame);
  468. dlg.DoModal();
  469. }
  470. BOOL CMDIClient::OnEraseBkgnd(CDC* pDC)
  471. {
  472. CRect rect;
  473. GetClientRect(&rect);
  474. // Paint the background color
  475. if (m_crBkColor != 0)
  476. {
  477. CBrush NewBrush(m_crBkColor);
  478. pDC->SetBrushOrg(0, 0);
  479. CBrush* pOldBrush = static_cast<CBrush*>(pDC->SelectObject(&NewBrush));
  480. pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  481. pDC->SelectObject(pOldBrush);
  482. NewBrush.DeleteObject();
  483. }
  484. CRect rcDataBox;
  485. //CString strLogo = MAKEINTRESOURCE( IDS_COMPANY_NAME );
  486. CString strLogo = "";
  487. CFont fontLogo;
  488. TEXTMETRIC tm;
  489. int fontSize = -MulDiv(18, pDC->GetDeviceCaps(LOGPIXELSY), 72);
  490. // Could be any TrueType font, but Times New Roman is most common
  491. fontLogo.CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
  492. ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  493. FIXED_PITCH | FF_ROMAN, _T("Times New Roman"));
  494. pDC->SetBkMode(OPAQUE);
  495. CFont* oldFont = pDC->SelectObject(&fontLogo);
  496. CRect st(0, 0, 0, 0);
  497. CSize sz = pDC->GetTextExtent(strLogo, strLogo.GetLength());
  498. // GetTextExtent calculates the size of the displayed logo
  499. // which depends on the device context....
  500. pDC->GetTextMetrics(&tm);
  501. // Calculate the box size by subtracting the text width and height from the
  502. // window size. Also subtract 20% of the average character size to keep the
  503. // logo from printing into the borders...
  504. GetClientRect(&rcDataBox);
  505. rcDataBox.left = rcDataBox.right - sz.cx - tm.tmAveCharWidth / 2;
  506. rcDataBox.top = rcDataBox.bottom - sz.cy - st.bottom - tm.tmHeight / 5;
  507. CRect rcSave = rcDataBox;
  508. pDC->SetBkMode(TRANSPARENT);
  509. rcSave = rcDataBox;
  510. // shift logo box right, and print black...
  511. rcDataBox.left += tm.tmAveCharWidth / 5;
  512. COLORREF oldColor = pDC->SetTextColor(RGB(0, 0, 0));
  513. pDC->DrawText(strLogo, strLogo.GetLength(), &rcDataBox,
  514. DT_VCENTER | DT_SINGLELINE | DT_CENTER);
  515. rcDataBox = rcSave;
  516. // shift logo box left and print white
  517. rcDataBox.left -= tm.tmAveCharWidth / 5;
  518. pDC->SetTextColor(RGB(255, 255, 255));
  519. pDC->DrawText(strLogo, strLogo.GetLength(), &rcDataBox,
  520. DT_VCENTER | DT_SINGLELINE | DT_CENTER);
  521. // Restore original location and print in the button face color
  522. rcDataBox = rcSave;
  523. pDC->SetTextColor(GetSysColor(COLOR_BTNFACE));
  524. pDC->DrawText(strLogo, strLogo.GetLength(), &rcDataBox,
  525. DT_VCENTER | DT_SINGLELINE | DT_CENTER);
  526. // restore the original properties and release resources...
  527. pDC->SelectObject(oldFont);
  528. pDC->SetTextColor(oldColor);
  529. pDC->SetBkMode(OPAQUE);
  530. fontLogo.DeleteObject();
  531. ReleaseDC(pDC);
  532. return TRUE;
  533. }
  534. void CMDIClient::OnSize(UINT nType, int cx, int cy)
  535. {
  536. CWnd::OnSize(nType, cx, cy);
  537. // if the app is just starting up, save the window
  538. // dimensions and get out
  539. if ((m_sizeClient.cx == 0) && (m_sizeClient.cy == 0))
  540. {
  541. m_sizeClient.cx = cx;
  542. m_sizeClient.cy = cy;
  543. return ;
  544. }
  545. // if the size hasn't changed, break and pass to default
  546. if ((m_sizeClient.cx == cx) && ( m_sizeClient.cy == cy))
  547. {
  548. return;
  549. }
  550. // window size has changed so save new dimensions and force
  551. // entire background to redraw, including icon backgrounds
  552. m_sizeClient.cx = cx;
  553. m_sizeClient.cy = cy;
  554. RedrawWindow(NULL, NULL,
  555. RDW_INVALIDATE | RDW_ERASE | RDW_ERASENOW | RDW_ALLCHILDREN);
  556. return;
  557. }
  558. void CMDIClient::OnRButtonDown(UINT nFlags, CPoint point)
  559. {
  560. // TODO: Add your message handler code here and/or call default
  561. // A simple demo...
  562. POINT ptScreen = point;
  563. // Convert the mouse point to screen coordinates since that is what is used by
  564. // the TrackPopupMenu() function.
  565. ClientToScreen(&ptScreen);
  566. // NOTE: This is an application menu, do not destroy it here!!!
  567. CWnd::OnRButtonDown(nFlags, point);
  568. }
  569. LRESULT CMDIClient::OnMDICreate(WPARAM wParam, LPARAM lParam)
  570. {
  571. HWND hWnd = reinterpret_cast<HWND>(DefWindowProc(WM_MDICREATE, wParam, lParam));
  572. CMDIChildWnd *pChild = static_cast<CMDIChildWnd*>(FromHandle(hWnd));
  573. if (m_bFirstTime && m_bMaxChild)
  574. m_pMDIFrame->MDIMaximize(pChild);
  575. else if (m_bMaxChild && m_pViewManager->GetWindowNum() == 0)
  576. m_pMDIFrame->MDIMaximize(pChild);
  577. // Thank you Andrew Lazarus, he provided the cool tip ...
  578. // pChild->GetDlgItem(AFX_IDW_PANE_FIRST)) to obtain the view just created.
  579. m_pViewManager->AddView(_T(""),
  580. static_cast<CView *>(pChild->GetDlgItem(AFX_IDW_PANE_FIRST)));
  581. return (LRESULT) hWnd;
  582. }
  583. LRESULT CMDIClient::OnMDIDestroy(WPARAM wParam, LPARAM lParam)
  584. {
  585. CMDIChildWnd* pChild =
  586. static_cast<CMDIChildWnd*>(FromHandle(reinterpret_cast<HWND>(wParam)));
  587. // It will be useful to save the state of the last child window closed and
  588. // use this for displaying the next window...
  589. if (m_pViewManager->GetWindowNum() == 1)
  590. {
  591. DWORD dwStyle = ::GetWindowLong(pChild->m_hWnd, GWL_STYLE);
  592. // Save the necessary options for later restoration process
  593. m_bMaxChild = (dwStyle & WS_MAXIMIZE) ? TRUE : FALSE;
  594. AfxGetApp()->WriteProfileInt(_T("Settings"), _T("TVChildWinState"), m_bMaxChild);
  595. }
  596. m_pViewManager->RemoveView(static_cast<CView*>(pChild->GetDlgItem(AFX_IDW_PANE_FIRST)));
  597. //return TRUE;
  598. return DefWindowProc(WM_MDIDESTROY, wParam, lParam);
  599. }
  600. void CMDIClient::OnWindowNext()
  601. {
  602. m_pMDIFrame->MDINext();
  603. }
  604. void CMDIClient::OnWindowPrevious()
  605. {
  606. ASSERT(::IsWindow(m_hWnd));
  607. ::SendMessage(m_pMDIFrame->m_hWndMDIClient, WM_MDINEXT, 0, 1);
  608. }
  609. BOOL CMDIClient::PreTranslateMessage(MSG* pMsg)
  610. {
  611. // When there is an escape key toggle the full screen mode
  612. return CWnd::PreTranslateMessage(pMsg);
  613. }