?? 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 + -