📄 step by step文档.txt
字号:
1,将 mapx.h mapx.cpp 拷贝到工程目录
------------------------------------------------
2,在 *view.h 中加入:
#include "mapx.h"
------------------------------------------------
3,在 *view.cpp 中:
public:
CMapX m_ctrlMapX;
定义一个控件
------------------------------------------------
4-1,要声明表示用于 MapX 的控件 ID 的常数:
转到“视图” > “资源符号”。
单击“新建”。
键入“IDC_MAP”来作为名称。
------------------------------------------------
4-2,或者在 Resource.h 中进行定义:
#define IDC_MAP 1001
------------------------------------------------
5, 增加 *view 类的 WM_CREATE 消息函数
- - - - - - - - - -
Rect 结构
Describes the width, height, and location of a rectangle.
命名空间: System.Windows
程序集: WindowsBase(在 windowsbase.dll 中)
- - - - - - - - - -
MFC Library Reference
COleControl::GetClientRect
Retrieves the size of the control's client area.
- - - - - - - - - -
GetClientRect Function
The GetClientRect function retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
- - - - - - - - - -
int CScGeoExpertView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
RECT windRect;
GetClientRect(& windRect);
if (!m_ctrlMapX.Create(NULL,WS_VISIBLE,windRect,this,IDC_MAP))
{
return -1;
}
return 0;
}
------------------------------------------------
5, 增加 *view 类的 WM_SIZE 消息函数
void CScGeoExpertView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
m_ctrlMapX.MoveWindow(0,0,cx,cy,true);
}
}
------------------------------------------------
6, 设置窗口的大小与屏幕一致
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.x = 0;
cs.y = 0;
cs.cx = GetSystemMetrics(SM_CXFULLSCREEN);
cs.cy = GetSystemMetrics(SM_CYFULLSCREEN)+50;
if (!CFrameWnd::PreCreateWindow(cs))
{
return FALSE;
}
return TRUE;
}
------------------------------------------------
7, 增加 *view 类的 WM_SETFOCUS 消息函数
void CScGeoExpertView::OnSetFocus(CWnd* pOldWnd)
{
CView::OnSetFocus(pOldWnd);
// TODO: Add your message handler code here
m_ctrlMapX.SetFocus();
}
------------------------------------------------
8, *view 类中, ID_FILE_OPEN 消息函数 Command
- - - - - - - - - -
首先在 *view类中定义一个存储路径的变量
public:
CString m_strFilePath;
- - - - - - - - - -
定义一个数组,用于判断 ManpInfo 的 gst 文件
[具体内函,我也不清楚]
static char BASED_CODE szTabFilter[]
= "MapInfo Map Files (*.gst)|*.gst|All Files (*.*)|*.*||";
void CScGeoExpertView::OnFileOpen()
- - - - - - - - - -
void CScGeoExpertView::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog dlgfile(TRUE, "*.gst", NULL, OFN_HIDEREADONLY, szTabFilter, this);
dlgfile.m_ofn.lpstrTitle = "Open MapInfo Map";
if (dlgfile.DoModal() == IDCANCEL)
return;
m_strFilePath = dlgfile.GetPathName();
try {
// Close the existing set of map layers and load the Canada map
TRACE0("Old Geoset: " + m_ctrlMapX.GetGeoSet());
m_ctrlMapX.SetGeoSet(m_strFilePath);
//m_ctrlMapX.SetTitleText("");
TRACE0("New Geoset: " + m_ctrlMapX.GetGeoSet());
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
}
------------------------------------------------
9, 导入工具条位图,此刻出现“image”菜单,点击 Toolbar Edit,OK,导入新工具条菜单
给新的工具条添加ID: IDR_TOOLBARMAP
- - - - - - - - - -
在MainFrame类中加入一个工具条的定义
public:
CToolBar m_wndMapToolBar;
- - - - - - - - - -
在菜单中添加新菜单项:“查看”->“地图操作栏”,ID_VIEW_MAPTOOLS
选择的类为: MainFrame
使用 ClassWizard,为ID_VIEW_MAPTOOLS 添加两个消息函数
Command
Update_Command_UI
- - - - - - - - - -
void CMainFrame::OnViewMaptools()
{
// TODO: Add your command handler code here
if (m_wndMapToolBar.IsVisible())
{
ShowControlBar(&m_wndMapToolBar, FALSE, FALSE);
}
else
{
ShowControlBar(&m_wndMapToolBar, TRUE, TRUE);
}
RecalcLayout();
}
- - - - - - - - - -
void CMainFrame::OnUpdateViewMaptools(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_wndMapToolBar.IsWindowVisible());
}
------------------------------------------------
10, 完成第9步后,运行程序会出错,因为还没有在程序初始化时,创建工具条
- - - - - - - - - -
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
······
创建新的工具条
if (!m_wndMapToolBar.Create(this) ||
!m_wndMapToolBar.LoadToolBar(IDR_TOOLBARMAP))
{
TRACE0("Failed to create toolbar\n");
return -1;
}
······
设定工具条的style
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndMapToolBar.SetBarStyle(m_wndMapToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
······
工具条停靠
EnableDocking(CBRS_ALIGN_ANY);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
m_wndMapToolBar.EnableDocking(CBRS_ALIGN_ANY);
//EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
······
新建工具条停靠在左侧
DockControlBar(&m_wndMapToolBar, AFX_IDW_DOCKBAR_LEFT);
}
------------------------------------------------
11, 单文档窗口,程序运行时初始化的大小问题
用 MainFrame 类的 PreCreateWindow 函数来完成这个功能
- - - - - - - - - -
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.x = 0;
cs.y = 0;
cs.cx = GetSystemMetrics(SM_CXFULLSCREEN);
//cs.cy = GetSystemMetrics(SM_CYFULLSCREEN)+50;
cs.cy = GetSystemMetrics(SM_CYFULLSCREEN);
if (!CFrameWnd::PreCreateWindow(cs))
{
return FALSE;
}
return TRUE;
}
------------------------------------------------
12, 给工具条添加ID号,以及 Command 和 Updata_Command_UI 消息函数
*view 类
- - - - - - - - - -
ID_MAP_TOOL_ARROW
MapX对象中的 miArrowTool
[鼠标焦点回到 鼠标指针]
void CScGeoExpertView::OnMapToolArrow()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miArrowTool);
}
void CScGeoExpertView::OnUpdateMapToolArrow(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miArrowTool);
}
- - - - - - - - - -
ID_MAP_TOOL_ARROW
MapX对象中的 miArrowTool
[鼠标焦点回到 鼠标指针]
- - - - - - - - - -
ID_MAP_TOOL_ZOOMIN
MapX对象中的 miZoomInTool
[放大]
- - - - - - - - - -
ID_MAP_TOOL_ZOOMOUT
MapX对象中的 miZoomOutTool
[缩小]
- - - - - - - - - -
ID_MAP_TOOL_PAN
MapX对象中的 miPanTool
[用掌形鼠标移动地图]
- - - - - - - - - -
ID_MAP_TOOL_SELECT
MapX对象中的miSelectTool
[选定]
- - - - - - - - - -
ID_MAP_TOOL_RECTANGLESELECT
MapX对象中的 miRectSelectTool
[矩形区域选定]
- - - - - - - - - -
ID_MAP_TOOL_RADIUSSELECT
MapX对象中的 miRadiusSelectTool
[圆形区域选定]
- - - - - - - - - -
ID_MAP_TOOL_CENTER
MapX对象中的 miCenterTool
[根据鼠标,设置地图中心]
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
ID_MAP_TOOL_ENTIRE
设定*.gst 文件夹的路径
*view 类中的 command 消息函数
void CScGeoExpertView::OnMapToolEntire()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetGeoSet(m_strFilePath);
}
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
ID_VIEW_LAYERCONTROL
使用MapX的内部对象管理图层
void CScGeoExpertView::OnViewLayercontrol()
{
// TODO: Add your command handler code here
try {
VARIANT vHelpFile, vHelpID; // mark as optional since we don't have a helpfile
vHelpFile.vt = VT_ERROR;
vHelpFile.scode = DISP_E_PARAMNOTFOUND;
vHelpID.vt = VT_ERROR;
vHelpID.scode = DISP_E_PARAMNOTFOUND;
CMapXLayers layers = m_ctrlMapX.GetLayers();
layers.LayersDlg(vHelpFile, vHelpID);
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
}
------------------------------------------------ Done!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -