⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwnd.cpp

📁 VisualC OPC Client Example C 程序开发
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// **************************************************************************
// mainwnd.cpp
//
// Description:
//	Implements a CFrameWnd derived class.  This the main window of our GUI
//	which contains the group view, item view, and event view panes - the view
//	parts of our MFC SDI document/view architecture.
//
// DISCLAIMER:
//	This programming example is provided "AS IS".  As such Kepware, Inc.
//	makes no claims to the worthiness of the code and does not warranty
//	the code to be error free.  It is provided freely and can be used in
//	your own projects.  If you do find this code useful, place a little
//	marketing plug for Kepware in your code.  While we would love to help
//	every one who is trying to write a great OPC client application, the 
//	uniqueness of every project and the limited number of hours in a day 
//	simply prevents us from doing so.  If you really find yourself in a
//	bind, please contact Kepware's technical support.  We will not be able
//	to assist you with server related problems unless you are using KepServer
//	or KepServerEx.
// **************************************************************************


#include "stdafx.h"
#include "opctestclient.h"
#include "mainwnd.h"
#include "document.h"
#include "groupview.h"
#include "itemview.h"
#include "eventview.h"
#include "server.h"
#include "group.h"

// Registry entry names:
static LPCTSTR lpszMainWindow	= _T("Main Window");
static LPCTSTR lpszX			= _T("x");
static LPCTSTR lpszY			= _T("y");
static LPCTSTR lpszCX			= _T("cx");
static LPCTSTR lpszCY			= _T("cy");
static LPCTSTR lpszState		= _T("State");
static LPCTSTR lpszGroups		= _T("Groups");
static LPCTSTR lpszEvents		= _T("Events");
static LPCTSTR lpszToolbar		= _T("Toolbar");
static LPCTSTR lpszStatusbar	= _T("Status Bar");

// Status bar string resources:
#define ID_HELPTEXT		0
#define ID_ITEMCOUNT	1

static UINT auIDs[] = 
	{
	ID_HELPTEXT,
	ID_ITEMCOUNT,
	};

// For window update timer:
#define STATUSUPDATETIMER		1
#define STATUSUPDATEINTERVAL	250


/////////////////////////////////////////////////////////////////////////////
// CKMainWnd
/////////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE (CKMainWnd, CFrameWnd)

// **************************************************************************
BEGIN_MESSAGE_MAP (CKMainWnd, CFrameWnd)
	//{{AFX_MSG_MAP(CKMainWnd)
	ON_WM_CLOSE ()
	ON_WM_ENDSESSION ()
	ON_UPDATE_COMMAND_UI (ID_APP_ABOUT, OnUpdateCmdUI)
	ON_WM_TIMER ()
	ON_UPDATE_COMMAND_UI (ID_APP_EXIT, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_EDIT_COPY, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_EDIT_CUT, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_EDIT_PASTE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_EDIT_DELETE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_NEW, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_OPEN, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_SAVE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_SAVE_AS, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE1, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE2, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE3, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE4, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE5, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE6, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE7, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE8, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE9, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE10, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE11, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE12, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE13, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE14, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE15, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_FILE_MRU_FILE16, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_VIEW_CLEAR, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_VIEW_ERRORONLY, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_SYNC_WRITE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC10_WRITE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC20_WRITE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_SET_ACTIVE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_SET_INACTIVE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_SYNC_READ_CACHE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_SYNC_READ_DEVICE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC10_READ_CACHE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC10_READ_DEVICE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC20_READ_DEVICE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC10_REFRESH_CACHE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC10_REFRESH_DEVICE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC20_REFRESH_CACHE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ASYNC20_REFRESH_DEVICE, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_GET_ERROR_STRING, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_CLONE_GROUP, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_GET_GROUP_BY_NAME, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_ENUMERATE_GROUPS, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_CONNECT, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_DISCONNECT, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_RECONNECT, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_EXPORT_CSV, OnUpdateCmdUI)
	ON_UPDATE_COMMAND_UI (ID_TOOLS_IMPORT_CSV, OnUpdateCmdUI)
	ON_WM_DESTROY ()
	//}}AFX_MSG_MAP
	ON_UPDATE_COMMAND_UI_RANGE (ID_EDIT_NEWSERVER, ID_EDIT_DELETE, OnUpdateCmdUI)
	
	ON_MESSAGE (UM_SERVER_SHUTDOWN, OnServerShutdownRequest)
	ON_MESSAGE (UM_SELECT_GROUP, OnSelectGroup)
	ON_MESSAGE (UM_REFRESH_ITEMVIEW, OnRefreshItemView)
	ON_MESSAGE (UM_ITEM_READD, OnItemReAdd)
	ON_MESSAGE (UM_CHANGEVIEW, OnChangeView)
END_MESSAGE_MAP ()


/////////////////////////////////////////////////////////////////////////////
// CKMainWnd construction/destruction
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// CKMainWnd ()
//
// Description:
//	Constructor.
//
// Parameters:
//  none
//
// Returns:
//  none
// **************************************************************************
CKMainWnd::CKMainWnd ()
	{
	// Initialize member variables:
	m_bLoaded = false;
	m_pEventLog = NULL;
	m_cnItems = 0;

	m_strIdleStatusText.LoadString (AFX_IDS_IDLEMESSAGE); 
	m_strStatusText = m_strIdleStatusText;
	m_bForceStatusText = false;
	}

// **************************************************************************
// ~CKMainWnd ()
//
// Description:
//	Destructor.
//
// Parameters:
//  none
//
// Returns:
//  none
// **************************************************************************
CKMainWnd::~CKMainWnd ()
	{
	}

// **************************************************************************
// LoadSettings ()
//
// Description:
//	Called when the application's window settings should be loaded from the
//	registry, or set to the defaults.  Settings saved at end of each session.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKMainWnd::LoadSettings ()
	{
	// Get pointer to application root object:
	CWinApp *pApp =  AfxGetApp ();
	ASSERT (pApp != NULL);

	// Define "Main Window" registry section string:
	CString strSection = lpszMainWindow;
	CString strKey;

	// Get height and witdh of screen (in pixels) to use as default if 
	// registry settings not found:
	int cx = GetSystemMetrics (SM_CXSCREEN);
	int cy = GetSystemMetrics (SM_CYSCREEN);

	// Get position of main window:
	int x = pApp->GetProfileInt (strSection, lpszX, 10);
	int y = pApp->GetProfileInt (strSection, lpszY, 10);

	// Get width and height of main window:
	int nWidth = pApp->GetProfileInt (strSection, lpszCX, cx / 2);
	int nHeight = pApp->GetProfileInt (strSection, lpszCY, cy / 2);

	// Get the state of main window when it was last visible (Default to normal):
	int nState = pApp->GetProfileInt (strSection, lpszState, 0);
	
	// Get the dimensions of the splitter panes:
	m_cDataView.SetColumnInfo (0, pApp->GetProfileInt (strSection, lpszGroups, nWidth / 3), 10);
	m_cEventView.SetRowInfo (0, pApp->GetProfileInt (strSection, lpszEvents, nHeight / 2), 10);

	// Get tool bar state and show if true:
	if (pApp->GetProfileInt (strSection, lpszToolbar, true))
		ShowControlBar (&m_cToolBar, TRUE, TRUE);

	// Load status bar state and show if true:
	if (pApp->GetProfileInt (strSection, lpszStatusbar, true))
		m_cStatusBar.ShowWindow (SW_SHOW);

	// Resize the main window using position, width and height jsut 
	// retrieved from registry.
	MoveWindow (x, y, nWidth, nHeight);

	// Minimize window if state retrieved from registry is -1,
	if (nState == -1)
		ShowWindow (SW_SHOWMINIMIZED);
	// or maximize if state is 1,
	else if (nState == 1)
		ShowWindow (SW_SHOWMAXIMIZED);
	// or show as normal otherwise.
	else
		ShowWindow (SW_SHOWNORMAL);
	}

// **************************************************************************
// SaveSettings ()
//
// Description:
//	Called when the application's window settings should be saved to the
//	registry.  These settings will be used the next time the application
//	starts up.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKMainWnd::SaveSettings ()
	{
	// Get pointer to application root object:
	CWinApp *pApp = AfxGetApp ();
	ASSERT (pApp != NULL);

	// Define "Main Window" registry section string:
	CString strSection = lpszMainWindow;
	CString strKey;

	// Save the size/position/state of the main window:
	if (IsIconic ())
		{
		// State is -1 if minimized.  Doesn't make sense to overwrite
		// any existing window size and position settings now.
		pApp->WriteProfileInt (strSection, lpszState, -1);
		}
	else if (IsZoomed ())
		{
		// State is 1 if maximized.  Doesn't make sense to overwrite
		// any existing window size and position settings now.
		pApp->WriteProfileInt (strSection, lpszState, 1);
		}
	else
		{
		// State is 0 otherwise.  Save window size and position settings
		// too.
		RECT rc;
		GetWindowRect (&rc);

		pApp->WriteProfileInt (strSection, lpszX, rc.left);
		pApp->WriteProfileInt (strSection, lpszY, rc.top);

		pApp->WriteProfileInt (strSection, lpszCX, rc.right - rc.left);
		pApp->WriteProfileInt (strSection, lpszCY, rc.bottom - rc.top);

		pApp->WriteProfileInt (strSection, lpszState, 0);
		}

	// Save the splitter pane widths:
	
	// Save group view pane width:
	int cxCol;
	int cxColMin;
	m_cDataView.GetColumnInfo (0, cxCol, cxColMin);
	pApp->WriteProfileInt (strSection, lpszGroups, cxCol);

	// Save event view pane width:
	int cyRow;
	int cyRowMin;
	m_cEventView.GetRowInfo (0, cyRow, cyRowMin);
	pApp->WriteProfileInt (strSection, lpszEvents, cyRow);

	// Save tool bar state (true shown, false hidden):
	pApp->WriteProfileInt (strSection, lpszToolbar, m_cToolBar.IsWindowVisible ());

	// Save status bar state (true shown, false hidden):
	pApp->WriteProfileInt (strSection, lpszStatusbar, m_cStatusBar.IsWindowVisible ());
	}

// **************************************************************************
// PreCreateWindow ()
//
// Description:
//	Called before the creation of the window. Override to modify the window 
//	style.
//
// Parameters:
//  none
//
// Returns:
//  BOOL - TRUE if success.
// **************************************************************************
BOOL CKMainWnd::PreCreateWindow (CREATESTRUCT &cs)
	{
	// Perform default processing.  Return FALSE if fail:
	if (!CFrameWnd::PreCreateWindow (cs))
		return (FALSE);

	// Modify main window style (do not show document name before application
	// name in title bar, and window not initially visible).
	cs.style &= ~(FWS_PREFIXTITLE | WS_VISIBLE);
	return (TRUE);
	}


/////////////////////////////////////////////////////////////////////////////
// CKMainWnd diagnostics
/////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
void CKMainWnd::AssertValid () const
	{
	CFrameWnd::AssertValid ();
	}

void CKMainWnd::Dump (CDumpContext &dc) const
	{
	CFrameWnd::Dump (dc);
	}

#endif //_DEBUG


/////////////////////////////////////////////////////////////////////////////
// CKMainWnd message handlers
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// OnCreateClient ()
//
// Description:
//	Create our splitter window with views.
//
// Parameters:
//  LPCREATESTRUCT	lpcs		The CREATESTRUCT structure defines the 
//								  initialization parameters passed to the 
//								  window procedure of an application.	
//	CCreateContext	*pContext	A CCreateContext structure contains pointers
//								  to the document, the frame window, the view, 
//								  and the document template.
//
// Returns:
//  BOOL - TRUE if success.
// **************************************************************************
BOOL CKMainWnd::OnCreateClient (LPCREATESTRUCT lpcs, CCreateContext *pContext) 
	{
	// Set up tool bar:

	// First determine whether or not to use flat toolbars.  Proper support 
	// for flat toolbars begins with the COMCTL32.DLL installed with 
	// IE4 - V4.71.
	CVersionInfo cVer (_T("COMCTL32.DLL"));
	DWORD dwVersion = MAKELONG (cVer.GetMinorVersion (), cVer.GetMajorVersion ());
	TRACE (_T("Common controls version %u.%02u detected\n"), cVer.GetMajorVersion (), cVer.GetMinorVersion ()); 

	// If DLL version supports flat tool bars,
	if (dwVersion >= MAKELONG (71, 4))
		{
		// Create tool bar with flat style.  (Other style properties set are:
		// child of this window, show on top of frame window, message text is
		// to be updated at same time as tool tips, tool tips are displayed.)
		// Return FALSE if fail:
		if (!m_cToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | CBRS_TOP | CBRS_FLYBY | CBRS_TOOLTIPS, CRect (0, 0, 0, 0), ID_VIEW_TOOLBAR))
			{
			TRACE (_T("OTC: CKMainWnd::OnCreateClient () -> Flat tool bar creation failed\n"));
			return (FALSE);
			}
		}
	
	// else it does not:
	else
		{
		// Create tool bar with default style.  (Other style properties set are:
		// child of this window, show on top of frame window, message text is
		// to be updated at same time as tool tips, tool tips are displayed.)
		// Return FALSE if fail:
		if (!m_cToolBar.Create (this, WS_CHILD | CBRS_TOP | CBRS_FLYBY | CBRS_TOOLTIPS, ID_VIEW_TOOLBAR))
			{
			TRACE (_T("OTC: CKMainWnd::OnCreateClient () -> Tool bar creation failed\n"));
			return (FALSE);
			}
		}

	// Load the tool bar:
	m_cToolBar.LoadToolBar (IDR_MAINFRAME);

	// Set up status bar:

	// Create with "this" window as parent:
	m_cStatusBar.Create (this);

	// Set indicator IDs:
	m_cStatusBar.SetIndicators (auIDs, sizeof (auIDs) / sizeof (auIDs [0]));

	// Set indicator ID, style, and width for each index:
	m_cStatusBar.SetPaneInfo (ID_HELPTEXT, auIDs[ID_HELPTEXT], SBPS_NOBORDERS | SBPS_STRETCH, 0);
	m_cStatusBar.SetPaneInfo (ID_ITEMCOUNT, auIDs[ID_ITEMCOUNT], SBPS_NORMAL, 64);

	// Split this window into to two panes, one on top of the other.  Top
	// pane will be "data view", and bottowm will be "event view".  Return

⌨️ 快捷键说明

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