mainfrm.cpp

来自「共享内存的源代码」· C++ 代码 · 共 906 行 · 第 1/2 页

CPP
906
字号
void CMainFrame::OnDestroy()
{
	CDataManView* pView = (CDataManView*)GetActiveView();

	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);
    //上升提示框保存
	AfxGetApp()->WriteProfileInt(s_profileHeading, s_profileTips_Show , m_Tips_Show_MAIN);
	AfxGetApp()->WriteProfileInt(s_profileHeading, s_profileOpt_time , m_optimiz_time_int_main);
    SaveBarState(AfxGetApp()->m_pszProfileName);



	//OPTION保存
//    pView->OnToolsOptionsRegSave(pView->MirexFilePath , pView->MirexFilePathsize , pView->m_IfAutoResetList , pView->m_IfAutoSelMicrexF);
    //MessageBox("save Success");

    CFrameWnd::OnDestroy();

}
void CMainFrame::ActivateFrame(int nCmdShow)
{
	CString strText;
    BOOL bIconic, bMaximized;
    UINT flags;
    WINDOWPLACEMENT wndpl;
    CRect rect;

	//注册表读取关于上升对话框的配置信息
	m_Tips_Show_MAIN = AfxGetApp()->GetProfileInt(s_profileHeading, s_profileTips_Show, 0);
	m_optimiz_time_int_main = AfxGetApp()->GetProfileInt(s_profileHeading, s_profileOpt_time , 0);

	//生成应用程序类的指针
    CDataManApp *pDataManApp = (CDataManApp *)AfxGetApp();
    //保证首次不显示
	if(pDataManApp->IfCMSOpen)
    {
		OnFileClosewindow();
		//pDataManApp->IfCMSOpen = false ;
		return ;
	}
	else
	{
		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 minimized/maximized status
			BOOL bRet = SetWindowPlacement(&wndpl);
		}

		CFrameWnd::ActivateFrame(nCmdShow);
	}

}

void CMainFrame::OnFileClosewindow()
{
	NOTIFYICONDATA nd;
	nd.cbSize	= sizeof (NOTIFYICONDATA);
	nd.hWnd	= m_hWnd;
	nd.uID	= IDI_TRAYICON;
	nd.uFlags	= NIF_ICON|NIF_MESSAGE;//|NIF_INFO | NIF_TIP;
	nd.uCallbackMessage = WM_TRAYMESSAGE;
	nd.hIcon = m_ilTray.ExtractIcon(0);
	/*nd.dwInfoFlags = NIIF_INFO;
	nd.uTimeout = 10000;

	CString strTip, strInfo, strInfoT;
	strTip.LoadString(AFX_IDS_APP_TITLE);
	strInfoT.LoadString(AFX_IDS_APP_TITLE);
	strInfo.LoadString(IDS_TRAYINFO);

	strcpy(nd.szTip, strTip);
	strcpy(nd.szInfoTitle, strInfoT);
	strcpy(nd.szInfo, strInfo);*/
	
	Shell_NotifyIcon(NIM_ADD, &nd);

	m_fTrayed = TRUE;
	ShowWindow(SW_HIDE);

}

void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
{
	if (nID == SC_MINIMIZE || nID == SC_CLOSE)
	{
		CMainFrame::OnFileClosewindow();
	}
	else
	{
		CWnd::OnSysCommand(nID, lParam);
	}
}

LRESULT CMainFrame::OnShowWndByPrev(WPARAM wParam, LPARAM lParam)
{
	
	//if (m_fTrayed)
	//{
		//OnTrayOpenDataMan();
	//}
	//else
	//{
	//	CWnd* pWndChild = GetLastActivePopup();
	//	pWndChild->SetForegroundWindow();
	//}
	//非首次,自动托盘最小化程序
	if(lParam != 0)
	{
		OnFileClosewindow();
	}
	return 0L;
}

LRESULT CMainFrame::OnTrayMessage(WPARAM wParam, LPARAM lParam)
{
	UINT uID = (UINT)wParam;
	UINT uMouseMsg = (UINT)lParam;
	POINT pt;

	if(uID == IDI_TRAYICON)
	{
		if (uMouseMsg == WM_LBUTTONDBLCLK )
		{
			//双击后显示视图
            CDataManApp *pDataManApp = (CDataManApp *)AfxGetApp();
            pDataManApp->IfCMSOpen = false ;
			AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW);

			//delete system tray
			NOTIFYICONDATA nd;
			nd.cbSize	= sizeof (NOTIFYICONDATA);
			nd.hWnd	= m_hWnd;
			nd.uID	= IDI_TRAYICON;
			Shell_NotifyIcon(NIM_DELETE, &nd);
			m_fTrayed = FALSE;
		}
		else if (uMouseMsg == WM_RBUTTONUP )
		{
			CMenu menu;
			menu.LoadMenu("IDR_TRAYMENU");
			CMenu  *pContextMenu = menu.GetSubMenu(0);
			pContextMenu->SetDefaultItem(ID_TRAY_OPENDataMan, FALSE);
			GetCursorPos(&pt);
			SetForegroundWindow();
			pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x,pt.y, AfxGetMainWnd());
		}
	}
	return 0L;
}

//////////////////////////////////////////////////////////////////////////////////////////////////
//wParam = 0 Map, = 1 unmap
//lParam COM port number
LRESULT CMainFrame::OnCMSSendEvent(WPARAM wParam, LPARAM lParam)
{
	//CString strMap, strComPort;
	//strMap = ( wParam == 0) ? _T("COM port MAP required, "):_T("COM port UNMAP required, ");
	//strComPort.Format(_T("YC%d"),(DWORD)lParam);
	//AfxMessageBox(strMap + strComPort,MB_ICONINFORMATION);YC ID required?
	
	CDataManView* pView = (CDataManView*)GetActiveView();
    pView->OnRefreshItemAllWithChange(lParam , wParam);
	return 0L;
}

void CMainFrame::OpenMicrexF(void)
{
	return;
}

void CMainFrame::LoadSoftware(CString strClassName, CString strWndTitle, CString strExeName)
{
	if(AfxMessageBox(IDS_QUERYUTILITIES, MB_ICONQUESTION | MB_YESNO) == IDNO)
		return;

	CWnd *pWndPrev, *pWndChild;
	
	// Determine if a window with the class name exists...
	pWndPrev = CWnd::FindWindow(strClassName,strWndTitle);
	
    if (pWndPrev != NULL)	//Find Micrex-F 
    {
		//does it have any popups?
		pWndChild = pWndPrev->GetLastActivePopup();

		// If iconic, restore the main window
		if (pWndPrev->IsIconic())
			pWndPrev->ShowWindow(SW_RESTORE);
			
		// Bring the main window or its popup to the foreground
		pWndChild->SetForegroundWindow();
	}
	else
	{
		STARTUPINFO si;
		::ZeroMemory(&si, sizeof(STARTUPINFO));
		si.cb = sizeof(STARTUPINFO);
		PROCESS_INFORMATION pi;
		
		if(::CreateProcess(NULL,(LPTSTR)(LPCTSTR)strExeName,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi))
		{
			::CloseHandle(pi.hThread);
			::WaitForInputIdle(pi.hProcess,INFINITE);
			::CloseHandle(pi.hProcess);
		}
		else
			AfxMessageBox(IDS_CANNOTLOADSOFTWARE, MB_ICONEXCLAMATION | MB_OK);
	}
}
void CMainFrame::OnToolsAdvanced()
{
	//LoadSoftware(	_T(""),
	//				_T("NPort Administrator-Configuration"),
	//				_T("C:\\Program Files\\NPortAdminSuite\\bin\npadmer.exe"));
}
void CMainFrame::OnTrayOpenDataMan()
{
	//通过托盘图标菜单显示视图
	CDataManApp *pDataManApp = (CDataManApp *)AfxGetApp();
    pDataManApp->IfCMSOpen = false ;
	AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW);

	//delete system tray
	NOTIFYICONDATA nd;
	nd.cbSize	= sizeof (NOTIFYICONDATA);
	nd.hWnd	= m_hWnd;
	nd.uID	= IDI_TRAYICON;
	Shell_NotifyIcon(NIM_DELETE, &nd);
	m_fTrayed = FALSE;
}

void CMainFrame::OnAppExit()
{
	if (AfxMessageBox(IDS_QUERYEXIT, MB_YESNO | MB_ICONQUESTION ) == IDYES)
	{
		//delete system tray
		NOTIFYICONDATA nd;
		nd.cbSize	= sizeof (NOTIFYICONDATA);
		nd.hWnd	= m_hWnd;
		nd.uID	= IDI_TRAYICON;
		Shell_NotifyIcon(NIM_DELETE, &nd);
		
		CFrameWnd::OnClose();
	}
}
//右下角 按钮设置
void CMainFrame::OnTrayOptimization()
{
	//获取共享内存块数据
	CSFMClient SFMC;

	//叠加变量名
	char l_Global[50] = "Global\\YC_share_mem";
	strcat(l_Global,"Optimization");

	SFMC.Open(FILE_MAP_READ|FILE_MAP_WRITE, l_Global);
	char *p = (char*)SFMC.GetBuffer();
	if (p)
	{
		int int_p = atoi(p);
		CDataManApp *pDataManApp = (CDataManApp *)AfxGetApp();//生成应用程序类的指针
		if(int_p == 1)
		{
			pDataManApp->IfOptimization(0);
			//设置菜单
			Is_Optimization = 0;

			m_wndOptimizationButton.SetWindowText("× Optimize");

			//view刷新一次
			CDataManView* pView = (CDataManView*)GetActiveView();
			pView->OnRefreshItemAll();

			//CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATIC);
			//pStatic->EnableWindow(true);
			//CWnd*   pWnd = 
			//	m_Dlgbar.GetDlgItem(IDC_STATIC_OPTSTA)->SetDlgItemText("dsadsa");   
            //CDC   pDC=pWnd->GetDC();   
			//pDC->SetTextColor(RGB(0,0,0),RGB(1,1,1),RGB(2,2,2));
		}
		else
		{
            pDataManApp->IfOptimization(1);
			Is_Optimization = 1;
			
			m_wndOptimizationButton.SetWindowText("√ Optimize");

			//view刷新一次
			CDataManView* pView = (CDataManView*)GetActiveView();
			pView->OnRefreshItemAll();
		}
	}

	//AfxMessageBox("efdefger");
}

void CMainFrame::OnUpdateTrayOptimization(CCmdUI *pCmdUI)
{  
	//CFont   myfont;   
 // CClientDC     dc(&m_wndOptimizationButton);   
 // myfont.CreatePointFont(0,"Arial",&dc);   
 // m_wndOptimizationButton.SetFont(&myfont,true);   
 // m_wndOptimizationButton.UpdateData(true);   
 // m_wndOptimizationButton.SetWindowText("× Optimize");
	// TODO: Add your command update UI handler code here
	if  (Is_Optimization)
	{
		pCmdUI->SetCheck(1);   
	}
	else   
	{
		pCmdUI->SetCheck(0);  
	}
}
void CMainFrame::OnViewStatusBar(void)
{
	// call base class handler
	OnBarCheck(AFX_IDW_STATUS_BAR);

	// now make button visible state same as status bar's
	BOOL bShow = m_wndStatusBar.GetStyle() & WS_VISIBLE;
	m_wndOptimizationButton.SetWindowPos(NULL, 0, 0, 0, 0,
		SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
		(bShow ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
}
//定时刷新部分
void CMainFrame::OnTimer(UINT nIDEvent) 
{
	switch(nIDEvent) 
	{
	case 1:  
		{ 
			KillTimer(1);
            
			CDataManView* pView = (CDataManView*)GetActiveView();
			pView->OnRefreshItemAllWithChange(1,1); //OnViewRefresh();

			SetTimer(1 , 1000 * (m_optimiz_time_int_main==0 ? 0.5 : m_optimiz_time_int_main) , NULL); 
	    }
			break;
	}
}
//定时刷新菜单
void CMainFrame::OnRefreshOnTime()
{
	if(m_IfRefreshOnTime)
	{		
		KillTimer(1);
		m_IfRefreshOnTime = false ;
	}
	else
	{
		SetTimer(1 , 1000 * (m_optimiz_time_int_main==0 ? 0.5 : m_optimiz_time_int_main) , NULL); 
		m_IfRefreshOnTime = true ;
	}
}
void CMainFrame::OnRefreshOnTime_UI(CCmdUI *pCmdUI)
{
	if  (m_IfRefreshOnTime)
	{
		pCmdUI->SetCheck(1);   
	}
	else   
	{
		pCmdUI->SetCheck(0);  
	}
}

⌨️ 快捷键说明

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