策略为王源代码分析-主程序及各视图界面对按键的处理

Published

按键的定义及对应的事件

G:\stock\TskingVS2019\src\Client\StkUI\StkUI.rc

/////////////////////////////////////////////////////////////////////////////
//
// Accelerator
//热键、快捷键定义   by freeman

IDR_MAINFRAME ACCELERATORS
BEGIN
    "A",            ID_EDIT_SELECT_ALL,     VIRTKEY, CONTROL, NOINVERT
    "B",            ID_VIEW_RESUMEDRUP,     VIRTKEY, CONTROL, NOINVERT
    "C",            ID_EDIT_COPY,           VIRTKEY, CONTROL, NOINVERT
    "F",            ID_EDIT_FIND,           VIRTKEY, CONTROL, NOINVERT
    "P",            ID_FILE_PRINT,          VIRTKEY, CONTROL, NOINVERT
    "V",            ID_VIEW_RESUMEDRDOWN,   VIRTKEY, CONTROL, NOINVERT
    VK_BACK,        ID_EDIT_UNDO,           VIRTKEY, ALT, NOINVERT
    VK_DELETE,      ID_EDIT_CUT,            VIRTKEY, SHIFT, NOINVERT
    VK_F1,          ID_REALTIME_REPORTDETAIL, VIRTKEY, NOINVERT
    VK_F10,         ID_VIEW_F10,            VIRTKEY, NOINVERT
    VK_F11,         ID_VIEW_SIMU,           VIRTKEY, NOINVERT
    VK_F12,         ID_TOOL_TRADER,         VIRTKEY, NOINVERT
    VK_F2,          ID_VIEW_SLIST,          VIRTKEY, NOINVERT
    VK_F3,          ID_REALTIME_VIEWSZZS,   VIRTKEY, NOINVERT
    VK_F4,          ID_REALTIME_VIEWSZNCZ,  VIRTKEY, NOINVERT
    VK_F5,          ID_VIEW_GRAPH,          VIRTKEY, NOINVERT
    VK_F6,          ID_SLIST_GROUPSELF,     VIRTKEY, NOINVERT
    VK_F7,          ID_VIEW_MULTISORT,      VIRTKEY, NOINVERT
    VK_INSERT,      ID_EDIT_COPY,           VIRTKEY, CONTROL, NOINVERT
    VK_INSERT,      ID_EDIT_PASTE,          VIRTKEY, SHIFT, NOINVERT
    "X",            ID_EDIT_CUT,            VIRTKEY, CONTROL, NOINVERT
    "Z",            ID_EDIT_UNDO,           VIRTKEY, CONTROL, NOINVERT
END

策略为王对按键的处理包括

1.全局按键的处理

如:F3等

2.单个视图里面对按键的进行响应处理

如:日K线 界面对按下ESCAPE

3.视图中的控件对按键的处理

如:股票列表视图的列表框控件、日K线画图控件

4.键盘精灵的处理

 

第一部分 主程序对按键的处理

主程序对按键的消息处理,只判断键盘精灵是否需要支持。如果检测到按下的键是0-9,a-z,则判断哪些视图需要弹出键盘精灵窗口,

G:\stock\TskingVS2019\src\Client\StkUI\MainFrm.cpp

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	/* ProfUIS Using Code
	if (m_wndMenuBar.TranslateMainFrameMessage(pMsg))
		return TRUE;
	*/

	if (WM_KEYDOWN == pMsg->message)
	{
		int	nVirtKey = (int)pMsg->wParam;
		if (VK_ESCAPE == nVirtKey && m_fullScreen.bFullScreen)
			OnViewFullscreen();

		if (m_SearchBox.IsWantChar(pMsg->wParam))
		{
			CView* pView = AfxGetStaticDoc()->GetActiveView();
			if (!pView ||
				(pView->IsKindOf(RUNTIME_CLASS(CWizardView))
					|| pView->IsKindOf(RUNTIME_CLASS(CGraphView))
					|| pView->IsKindOf(RUNTIME_CLASS(CRealTimeView))
					|| pView->IsKindOf(RUNTIME_CLASS(CMultiSortView))
					|| pView->IsKindOf(RUNTIME_CLASS(CSListView))
					|| pView->IsKindOf(RUNTIME_CLASS(CBaseView))
					|| pView->IsKindOf(RUNTIME_CLASS(CSelectorView))))
			{
				CRect	rect;
				GetClientRect(&rect);
				m_SearchBox.SetWindowPos(NULL, rect.right - FRM_SEARCH_WIDTH - 18, rect.bottom - FRM_SEARCH_HEIGHT - 22,
					0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
				m_SearchBox.OnChangeStatus(pMsg->wParam, pMsg->lParam, FALSE);
			}
		}
		else if (/*::IsWindow(m_wndWorkspBar.GetSafeHwnd())
			 && */!m_SearchBox.GetDroppedState()/* && !m_wndWorkspBar.IsWindowVisible()*/)
		{
			CView* pView = AfxGetStaticDoc()->GetActiveView();
			if (pView &&
				(pView->IsKindOf(RUNTIME_CLASS(CGraphView))
					|| pView->IsKindOf(RUNTIME_CLASS(CRealTimeView))
					|| pView->IsKindOf(RUNTIME_CLASS(CMultiSortView))
					|| pView->IsKindOf(RUNTIME_CLASS(CSListView))))
				pView->SetFocus();
		}
	}

	return CTskMainFrame::PreTranslateMessage(pMsg);
}

第二部分 视图界面对按键的处理

(一)对按键的处理分两部分:

1.在系统消息预处理中处理按键

2.自定义消息及处理函数

(一)\StkUI\View\RealTimeView.cpp

(二)在系统消息预处理中处理按键

G:\stock\TskingVS2019\src\Client\StkUI\View\GraphView.cpp

定义F5

//处理按键,按F5 切换到 分时线图 by freeman
BOOL CGraphView::PreTranslateMessage(MSG* pMsg) 
{
	if (WM_KEYDOWN == pMsg->message)
	{
		switch(pMsg->wParam)
		{
		case VK_F5:  //K线图 F5热键、快捷键定义   by freeman
			{
				AfxSwitchToStaticView(RUNTIME_CLASS(CRealTimeView));
				CRealTimeView * pView = AfxGetRealTimeView();
				if (pView)
					pView->ChangeDrawMode(CRealTime::modePriceLine);
			}
			return TRUE;
		
		}
		
	}
	
	return CView::PreTranslateMessage(pMsg);
}

(三)自定义消息及处理函数

1.定义按键消息

BEGIN_MESSAGE_MAP(CRealTimeView, CView)
	//{{AFX_MSG_MAP(CRealTimeView)
	ON_WM_CREATE()
	ON_WM_WINDOWPOSCHANGED()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_KEYDOWN()
	ON_COMMAND(ID_REALTIME_PREV, OnRealtimePrev)
	ON_COMMAND(ID_REALTIME_NEXT, OnRealtimeNext)
	ON_COMMAND(ID_REALTIME_LEFT, OnRealtimeLeft)
	ON_COMMAND(ID_REALTIME_RIGHT, OnRealtimeRight)
	ON_COMMAND(ID_REALTIME_HOME, OnRealtimeHome)
	ON_COMMAND(ID_REALTIME_END, OnRealtimeEnd)
	ON_NOTIFY(TCN_SELCHANGE, IDC_REALTIMEVIEW_REPORTTAB, OnSelchangeReporttab)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
	ON_MESSAGE(WM_USER_GETVIEWTITLE, OnGetViewTitle)
	ON_MESSAGE(WM_USER_GETVIEWCMDID, OnGetViewCmdid)
	ON_MESSAGE(WM_USER_COLORCHANGE, OnColorChange)
	ON_MESSAGE(WM_APP_STKRECEIVER_DATA, OnStkReceiverData)
END_MESSAGE_MAP()

2.处理函数

void CRealTimeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	switch( nChar )
	{
	case VK_LEFT:
		OnRealtimeLeft();
		break;
	case VK_RIGHT:
		OnRealtimeRight();
		break;
	case VK_UP:
		OnRealtimeUp();
		break;
	case VK_DOWN:
		OnRealtimeDown();
		break;
	case VK_HOME:
		OnRealtimeHome();
		break;
	case VK_END:
		OnRealtimeEnd();
		break;
	case VK_PRIOR:	//	Page Up
		OnRealtimePrev( );
		break;
	case VK_NEXT:	//	Page Down
		OnRealtimeNext( );
		break;
	case VK_ESCAPE:
		for( int i=0; i<m_nMultiStockCount && i<RTV_MAX_MULTICOUNT; i++ )
			m_realtime[i].ResetIndexCurrent( );
		Invalidate( );
		break;
	}

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}