// MainFrm.cpp // // (c)1998-2025 Codejock Software, All Rights Reserved. // // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN // CONSENT OF CODEJOCK SOFTWARE. // // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A // SINGLE COMPUTER. // // CONTACT INFORMATION: // support@codejock.com // http://www.codejock.com // ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "TrayIconDemo.h" #include "MainFrm.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() ON_COMMAND(ID_VIEW_THEME_OFFICEXP, OnViewThemeOfficeXP) ON_UPDATE_COMMAND_UI(ID_VIEW_THEME_OFFICEXP, OnUpdateViewThemeOfficeXP) ON_COMMAND(IDR_RESTORE, OnRestore) ON_COMMAND(ID_PROPERTIES, OnProperties) ON_COMMAND(ID_APP_EXIT, OnAppExit) //}}AFX_MSG_MAP #ifdef _XTP_INCLUDE_COMMANDBARS ON_WM_CLOSE() ON_COMMAND(XTP_ID_CUSTOMIZE, OnCustomize) #endif ON_MESSAGE(TIN_XTP_TRAYICON, OnTrayIconNotify) END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; static UINT icoArray[] = { IDI_ICON2, IDI_ICON3, IDI_ICON4, IDI_ICON5 }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // initialize themes. m_iTheme = m_regMgr.GetProfileInt(_T("Settings"), _T("Theme"), xtpControlThemeOfficeXP); } CMainFrame::~CMainFrame() { m_regMgr.WriteProfileInt(_T("Settings"), _T("Theme"), m_iTheme); } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; #ifdef _XTP_INCLUDE_COMMANDBARS if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators) / sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } if (!InitCommandBars()) return -1; CXTPCommandBars* pCommandBars = GetCommandBars(); pCommandBars->SetMenu(_T("Menu Bar"), IDR_MAINFRAME); CXTPToolBar* pCommandBar = (CXTPToolBar*)pCommandBars->Add(_T("Standard"), xtpBarTop); if (!pCommandBar || !pCommandBar->LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; } ((CXTPControlButton*)pCommandBar->GetControls()->GetAt(8))->SetStyle(xtpButtonIconAndCaption); // Load the previous state for command bars. LoadCommandBars(_T("CommandBars")); #else # ifdef _XTP_INCLUDE_CONTROLBARS if (!m_wndMenuBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndMenuBar.LoadMenuBar(IDR_MAINFRAME)) { TRACE0("Failed to create menubar\n"); return -1; // fail to create } if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } # else if (!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } // TODO: Remove this if you don't want tool tips or a resizeable toolbar m_wndToolBar.SetWindowText(_T("Address Bar")); m_wndToolBar.ModifyStyle(0, TBSTYLE_FLAT); m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); # endif if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators) / sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable # ifdef _XTP_INCLUDE_CONTROLBARS m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY); # endif m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); # ifdef _XTP_INCLUDE_CONTROLBARS DockControlBar(&m_wndMenuBar); # endif DockControlBar(&m_wndToolBar); # ifdef _XTP_INCLUDE_CONTROLBARS // TODO: Remove this line if you don't want cool menus. InstallCoolMenus(IDR_MAINFRAME); # endif #endif SetTheme(m_iTheme); // Create the tray icon. if (!m_TrayIcon.Create(_T("Power Tools for MFC!"), // Toolktip text this, // Parent window IDI_TRAYICON, // Icon resource ID IDR_POPUP_TRAY, // Resource ID of popup menu IDR_RESTORE, // Default menu item for popup menu false)) // True if default menu item is located by position { TRACE0("Failed to create tray icon\n"); return -1; } m_TrayIcon.SetAnimationIcons(icoArray, _countof(icoArray)); // Display the properties dialog. ::PostMessage(m_hWnd, WM_COMMAND, ID_PROPERTIES, 0); return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if (!CFrameWnd::PreCreateWindow(cs)) return FALSE; cs.lpszClass = _T("XTPMainFrame"); CXTPDrawHelpers::RegisterWndClass(AfxGetInstanceHandle(), cs.lpszClass, CS_DBLCLKS, AfxGetApp()->LoadIcon(IDR_MAINFRAME)); return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers void CMainFrame::OnRestore() { m_propDlg.MinMaxWindow(); } void CMainFrame::SetTheme(int iTheme) { m_iTheme = iTheme; #ifdef _XTP_INCLUDE_COMMANDBARS XTPPaintManager()->SetTheme(m_iTheme == xtpControlThemeOfficeXP ? xtpThemeOfficeXP : xtpThemeNativeWinXP); GetCommandBars()->GetPaintManager()->m_bAutoResizeIcons = TRUE; GetCommandBars()->RedrawCommandBars(); #endif RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE | RDW_ALLCHILDREN); RecalcLayout(); } void CMainFrame::OnViewThemeOfficeXP() { SetTheme((m_iTheme == xtpControlThemeOfficeXP) ? xtpControlThemeOffice2000 : xtpControlThemeOfficeXP); } void CMainFrame::OnUpdateViewThemeOfficeXP(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_iTheme == xtpControlThemeOfficeXP); } LRESULT CMainFrame::OnTrayIconNotify(WPARAM /*wParam*/, LPARAM lParam) { // UINT uID = (UINT)wParam; // resource ID of the tray icon. UINT uMouseMsg = (UINT)lParam; // mouse message that was sent. // We can let the tray icon control handle our context menu and // mouse double click events, but we want handle our balloon tip // notifications, so we will return 1 to let the tray icon control // know that we have handled these messages already... switch (uMouseMsg) { // Sent when the balloon is shown (balloons are queued). case NIN_BALLOONSHOW: { } return 1; // Sent when the balloon disappears-for example, when the // icon is deleted. This message is not sent if the balloon // is dismissed because of a timeout or a mouse click. case NIN_BALLOONHIDE: { } return 1; // Sent when the balloon is dismissed because of a timeout. case NIN_BALLOONTIMEOUT: { } return 1; // Sent when the balloon is dismissed because of a mouse click. case NIN_BALLOONUSERCLICK: { CXTPHyperLink hyperlink; hyperlink.GotoURL(_T("http://www.codejock.com/")); } return 1; #ifdef _XTP_INCLUDE_COMMANDBARS case WM_RBUTTONUP: { CMenu menu; if (!menu.LoadMenu(IDR_POPUP_TRAY)) { return 0; } CMenu* pSubMenu = menu.GetSubMenu(0); if (pSubMenu == NULL) { return 0; } ::SetMenuDefaultItem(pSubMenu->m_hMenu, IDR_RESTORE, FALSE); // Display the menu at the current mouse location. There's a "bug" // (Microsoft calls it a feature) in Windows 95 that requires calling // SetForegroundWindow. To find out more, search for Q135788 in MSDN. // CPoint pos; GetCursorPos(&pos); ::SetForegroundWindow(m_hWnd); CXTPCommandBars::TrackPopupMenu(pSubMenu, 0, pos.x, pos.y, this); ::PostMessage(m_hWnd, WM_NULL, 0, 0); menu.DestroyMenu(); } return 1; #endif } return 0; } void CMainFrame::OnProperties() { // Display the properties dialog. m_propDlg.DoModal(this); } void CMainFrame::OnAppExit() { // if the properties dialog is open, close it... if (::IsWindow(m_propDlg.m_hWnd)) { ::SendMessage(m_propDlg.m_hWnd, WM_COMMAND, IDOK, 0); } // post a message to ourself to close ::PostMessage(m_hWnd, WM_CLOSE, 0, 0); } #ifdef _XTP_INCLUDE_COMMANDBARS void CMainFrame::OnClose() { // Save the current state for command bars. SaveCommandBars(_T("CommandBars")); CFrameWnd::OnClose(); } void CMainFrame::OnCustomize() { // get a pointer to the command bars object. CXTPCommandBars* pCommandBars = GetCommandBars(); if (pCommandBars == NULL) return; // instanciate the customize dialog CXTPCustomizeSheet dlg(pCommandBars); // add the options page to the customize dialog. CXTPCustomizeOptionsPage pageOptions(&dlg); dlg.AddPage(&pageOptions); // add the commands page to the customize dialog. CXTPCustomizeCommandsPage* pPageCommands = dlg.GetCommandsPage(); pPageCommands->AddCategories(IDR_MAINFRAME); // initialize the commands page page. pPageCommands->InsertAllCommandsCategory(); pPageCommands->InsertBuiltInMenus(IDR_MAINFRAME); pPageCommands->InsertNewMenuCategory(); // display the customize dialog. dlg.DoModal(); } #endif