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

📄 ew_menu.cpp

📁 基于Windml2.0的窗口系统原代码 , 以及和MFC CDC兼容的CDC
💻 CPP
字号:
#include "EW_Menu.h"
#include "EW_VxWorksAdaptor.h"
#include "EW_FileDialog.h"
#include "EW_List.h"
#include "cemgis.h"
#include "CGIS_LayerControlDialog.h"
//#include "IGIS_AbstractLayer.h"

/*
#define  ID_SHOWANNOTATION  1
#define  ID_SHOWCOMPASS  	2
#define  ID_SHOWSCALE  		3
#define  ID_EXIT  			4
*/

enum {ID_NONE , ID_OPENLAYER , ID_LAYERCONTROL , ID_SHOWANNOTATION , ID_SHOWCOMPASS , ID_SHOWSCALE ,ID_EXIT};


BEGIN_MESSAGE_MAP(EW_Menu, EW_Window)
    ON_DRAW         (EW_Menu::OnDraw)
    ON_KEYDOWN      (EW_Menu::OnKeyDown)
    ON_MOUSEMOVE	(EW_Menu::OnMouseMove)    
    ON_ID_NOTIFY    (FBN_PRESSED, ID_OPENLAYER, EW_Menu::OpenLayer)
    ON_ID_NOTIFY    (FBN_PRESSED, ID_LAYERCONTROL, EW_Menu::LayerControl)
    ON_ID_NOTIFY    (FBN_PRESSED, ID_SHOWANNOTATION, EW_Menu::ShowAnnotation)
    ON_ID_NOTIFY    (FBN_PRESSED, ID_SHOWCOMPASS , EW_Menu::ShowCompass)
    ON_ID_NOTIFY    (FBN_PRESSED, ID_SHOWSCALE , EW_Menu::ShowScale)
    ON_ID_NOTIFY    (FBN_PRESSED, ID_EXIT , EW_Menu::OnExit)    
END_MESSAGE_MAP()


struct 	{
		char strMenuText[1024];
		int menuId;	
		}
strMenuTexts [] =   {
					{ "打开图层" 	, ID_OPENLAYER 		} ,
					{ "图层控制" 	, ID_LAYERCONTROL	} ,
					{ "隐藏注记" 	, ID_SHOWANNOTATION } ,
					{ "隐藏指北针" 	, ID_SHOWCOMPASS 	} ,
					{ "隐藏比例尺"	, ID_SHOWSCALE 		} ,
					{ "退出系统" 	, ID_EXIT 			} ,
					{ NULL 			, ID_NONE			}		
					} ;

	
EW_Menu::EW_Menu(int xPos , int yPos , WORD wID):EW_Window("", wID, FS_NONE)
{
	m_nxPos = xPos;
	m_nyPos = yPos;
	
	int i =0;
	while(strMenuTexts[i].menuId > 0){
		AddMenuItem(strMenuTexts[i].strMenuText );		
		i++;
	}
	
	EW_Factory * pF = EW_GetFactory();
	i = 0;
	while(strMenuTexts[i].menuId > 0){
		EW_Rect rect(xPos , yPos + (m_nMenuItemHeight + 1)*i ,  xPos + m_nMenuWidth , yPos + (m_nMenuItemHeight + 1)*(i + 1));
		AddWidget(pF->CreateButton(strMenuTexts[i].strMenuText, rect, strMenuTexts[i].menuId));						
		i++;
	}	
}

EW_Menu::~EW_Menu()
{
	MENU_STRING_LIST::iterator it = m_menuStringList.begin();
	for(; it != m_menuStringList.end();it++)
	{
		delete[] *it;	
	}
	
	m_menuStringList.clear();		
}

void EW_Menu::OnDraw(EW_OSAdaptor * pAdaptor)
{	
	pAdaptor->BeginDraw(m_Rect);
	
	EW_Window::OnDraw(pAdaptor);
	
	//draw background
    pAdaptor->FillRect(m_Rect ,RGB(0 , 0 , 0) ,  RGB(168, 168, 168)); 
    
    //draw menu items
	MENU_STRING_LIST::iterator it = m_menuStringList.begin();
	int i = 0;
	for (; it < m_menuStringList.end(); it++,i++){
		if(m_nSelectedIndex == i){
		//选择项
			//画选择项的背景
			EW_Rect rect(m_Rect.wLeft + 1 , m_Rect.wTop + i * m_nMenuItemHeight + 2 ,
				m_Rect.wLeft + m_nMenuWidth - 2, m_Rect.wTop + (i + 1) * m_nMenuItemHeight + 1 );			
			pAdaptor->FillRect(rect, RGB(168, 168, 168) , RGB(0, 0, 168)); 
			
			//画选择项文字
			pAdaptor->DrawTextW(*it , m_Rect.wLeft + 2, m_Rect.wTop + i * m_nMenuItemHeight + 2 , RGB(255 , 255 , 255));		
		}else
		//非选择项
			pAdaptor->DrawTextW(*it , m_Rect.wLeft + 2, m_Rect.wTop + i * m_nMenuItemHeight + 2 , RGB(0 , 0 , 0));		
			
	}

    pAdaptor->EndDraw();              				
}

void EW_Menu::AddMenuItem(char *szMenuText , int index)
{	
	int length = strlen(szMenuText);
	char *menuText = new char[length + 1];
	strcpy(menuText , szMenuText);
	
	if(index == -1)
		m_menuStringList.push_back(menuText);		
	else{
		MENU_STRING_LIST::iterator it = m_menuStringList.begin();
		it += index;
		m_menuStringList.insert(it , menuText);		
	}    
	
	//调整窗口大小
	AdjustifySize();
}
	
void EW_Menu::DeleteItem(int index)
{
	MENU_STRING_LIST::iterator it = m_menuStringList.begin();
	it += index;
	delete[] *it;
	m_menuStringList.erase(it);
	
	//调整窗口大小
	AdjustifySize();
}


void EW_Menu::OnMouseMove(EW_Message * pMsg)
{
	int preSelectedIndex = m_nSelectedIndex;
	//更改选择项
	SetSelectedItem(pMsg->Point.x , pMsg->Point.y);
	
	//重新绘制菜单
	if(preSelectedIndex != m_nSelectedIndex)
		OnDraw(EW_GetAdaptor());
}

void EW_Menu::SetSelectedItem(int xPos , int yPos)
{
	if (xPos >= m_Rect.wLeft + 2 && yPos >= m_Rect.wTop && xPos < m_Rect.wLeft + m_nMenuWidth && yPos < m_Rect.wTop + m_nMenuHeight - 2)
    {
    	m_nSelectedIndex = (yPos - 2 - m_Rect.wTop) / m_nMenuItemHeight;
		if(m_nSelectedIndex >= m_menuStringList.size())
			m_nSelectedIndex = -1;
    }else
    	m_nSelectedIndex = -1;    	
}

void EW_Menu::AdjustifySize()
{
	//清除原来的值	
	m_nMenuWidth = 0;	
	m_nMenuHeight = 0;
	
	EW_VxWorksAdaptor *adaptor = (EW_VxWorksAdaptor *)EW_GetAdaptor();
	CDC *pDC = adaptor->GetDC();	
	
	//计算菜单宽高
	MENU_STRING_LIST::iterator it = m_menuStringList.begin();
	for (; it != m_menuStringList.end(); it++)
    {
    	int textWidth;
    	char *menuText = *it;
    	int length = strlen(menuText);
		CSize size = pDC->GetTextExtent(menuText , length);
		
		//菜单是中文字符
		textWidth = size.cy * length/2; 
		
		//菜单是英文字符
		//textWidth = size.cx; 
		
		m_nMenuItemHeight = size.cy;
			
	    if (textWidth > m_nMenuWidth)
	    	m_nMenuWidth = textWidth;
	
	   	m_nMenuHeight += m_nMenuItemHeight + 1;
    } 
    
    //稍微扩充菜单宽度
    m_nMenuWidth += 4;
        		
    //生成菜单窗口
	
    m_nSelectedIndex = -1;   
	m_Rect.Set( m_nxPos , m_nyPos , m_nxPos + m_nMenuWidth , m_nyPos + m_nMenuHeight);    	
}

void EW_Menu::MoveDown()
{
	if(m_nSelectedIndex == m_menuStringList.size() - 1)
		return ;
	
	//增加当前索引
	m_nSelectedIndex++;
		
	//重绘菜单
	OnDraw(EW_GetAdaptor());
}

void EW_Menu::MoveUp()
{
	if(m_nSelectedIndex == -1)
		return ;
	
	//增加当前索引
	m_nSelectedIndex--;
		
	//重绘菜单
	OnDraw(EW_GetAdaptor());
}

void EW_Menu::OnKeyDown(EW_Message * pMsg)
{
	UGL_WCHAR key = pMsg->lData;  
	switch(key)
	{
		case FVK_UP:
			MoveUp();
			break;	
		case FVK_DOWN:
			MoveDown();
			break;									
		case FVK_ESCAPE:
			EW_GetScreen()->RemoveWindow(this);
			break;	
		case FVK_ENTER:
			{
			EW_Message msg(EW_MSG_NOTIFY , FBN_PRESSED , strMenuTexts[m_nSelectedIndex].menuId);
			Message(&msg);			
			}
			break;				
		default:
			break;		
	}//end switch		
}
void EW_Menu::OpenLayer()
{
	EW_GetScreen()->AddWindow(new EW_FileDialog(100 ,100));
	EW_GetScreen()->RemoveWindow(this);	
}

void EW_Menu::LayerControl()
{			
	EW_GetScreen()->AddWindow(new CGIS_LayerControlDialog(EW_Rect(200 , 200 , 600 , 600)));	
	EW_GetScreen()->RemoveWindow(this);	
}

void EW_Menu::ShowAnnotation()
{
	CEMGIS *pEMGIS = GetCEMGIS();	
	int i;
	for(i = 0;i < pEMGIS->GetLayerCount(); i++)
	{
		pEMGIS->SetAnnotation(i , !pEMGIS->HasAnnotation(i));
	}
	
	if(pEMGIS->GetLayerCount() > 0){
		
		MENU_STRING_LIST::iterator it = m_menuStringList.begin() + ID_SHOWANNOTATION - 1;
		if(pEMGIS->HasAnnotation(0))
			strcpy(strMenuTexts[ID_SHOWANNOTATION - 1].strMenuText , "隐藏注记");
		else
			strcpy(strMenuTexts[ID_SHOWANNOTATION - 1].strMenuText , "显示注记");
	}
							
	EW_GetScreen()->RemoveWindow(this);	
	pEMGIS->RedrawAll(0);	
}

void EW_Menu::ShowCompass()
{
	CEMGIS *pEMGIS = GetCEMGIS();  
	pEMGIS->ShowCompass(!pEMGIS->IsShowCompass());	
	
	MENU_STRING_LIST::iterator it = m_menuStringList.begin() + ID_SHOWCOMPASS - 1;
	if(pEMGIS->IsShowCompass())
		strcpy(strMenuTexts[ID_SHOWCOMPASS - 1].strMenuText , "隐藏指北针");
	else{
		strcpy(strMenuTexts[ID_SHOWCOMPASS - 1].strMenuText , "显示指北针");
	}
			
	EW_GetScreen()->RemoveWindow(this);
}

void EW_Menu::ShowScale()
{
	CEMGIS *pEMGIS = GetCEMGIS(); 
	pEMGIS->ShowScale(!pEMGIS->IsShowScale());		 
		
	MENU_STRING_LIST::iterator it = m_menuStringList.begin() + ID_SHOWSCALE - 1;
	if(pEMGIS->IsShowScale())		
		strcpy(strMenuTexts[ID_SHOWSCALE - 1].strMenuText , "隐藏比例尺");
	else
		strcpy(strMenuTexts[ID_SHOWSCALE - 1].strMenuText , "显示比例尺");
		
		
	EW_GetScreen()->RemoveWindow(this);
}

void EW_Menu::OnExit()
{	
	EW_GetScreen()->RemoveWindow(this);
	EW_Message Msg(EW_MSG_KEY, FKS_DOWN);	
	Msg.lData = FVK_END;
	EW_GetAdaptor()->SendMsg(& Msg); 	
}

								

⌨️ 快捷键说明

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