Create a MapX custom tool (ranging tool) using vc6.0)

Source: Internet
Author: User
Statement:

I have found many examples on the Internet. This is a complete example. After debugging, I posted it for your reference, so that you can avoid detours (attach a map first ).

Reference blog: http://blog.csdn.net/fengrx/archive/2009/05/09/4163187.aspx

The first part of the following article is the author of the original text, and the latter part is added in combination with the specific details, which is also a perfect example of the original text.

 

Creating a custom tool is a very important part of MapX programming. The steps are relatively simple, but many people can easily ignore some of the steps, making this function difficult to implement. You can perform the following steps. It takes a lot of time to do this function, because most articles about this function ignore the third step, making it difficult for beginners to implement it successfully.

Therefore, the steps are further detailed based on the previous improvements. Other custom tools can follow similar steps.

// 1. Define Constants
# Define mytool_distance 100 // 1 ~ 999

// 2. Create a tool
M_ctrlmap.createcustomtool (mytool_distance, mitooltypepoly, microsscursor );

// 3. Create an event slot
In view. H, add a line of code under the declare_message_map () Row: declare_eventsink_map ()

In view. cpp, add

Begin_eventsink_map (cpatrolcheckview, cview)
On_event (cpatrolcheckview, idc_map,
11/* mapx_dispid_polytoolused */, onpolytoolusedmap,
Vts_i2 vts_i4 vts_dispatch vts_bool vts_pbool)
End_eventsink_map ()

 
// 4. Respond to the event
Add void onpolytoolusedmap (short toolnum, long flags, lpdispatch points, bool in view. h.

Bshift, bool bctrl, bool far * enabledefault );
Add the implementation code to view. cpp.
Void cmapxsampleview: onpolytoolusedmap (short toolnum, long flags, lpdispatch points,
Bool bshift, bool bctrl, bool far * enabledefault)
{
// Calculate the distance
If (flags = mipolympus toolinprogress) {// This sentence is optional
If (toolnum = mytool_distance)
{
Cmapxpoints PTS;
Long N;
Long I;
Try {
PTS. attachdispatch (points, false); // get points object
Double dddistancetot = 0.0;
N = pts. getcount ();
M_ctrlmapx.setmapunit (miunitkilometer );
For (I = 1; I <n; I ++)
{
Cmapxpoint pt1 = pts. item (I );
Cmapxpoint pt2 = pts. Item (I + 1 );
Double D = m_ctrlmapx.distance (pt1.getx (), pt1.gety (), pt2.getx (),
Pt2.gety ());
Ddistancetot + = D;
}
Cstring STR;
Str. Format ("total distance: % F", ddistancetot );
// Output distance in the status bar
(Cmainframe *) afxgetmainwnd ()-> m_wndstatusbar.setpanetext (0, STR); // set m_wndstatusbar first

Public
}
Catch (coledispatchexception * E ){
E-> reporterror ();
E-> Delete ();
}
Catch (coleexception * E ){
E-> reporterror ();
E-> Delete ();
}
}
} // If (flags = mipolympus toolinprogress)
}

// 5. Call the tool
Void cmapxsampleview: onmaptooldistancetool ()
{
M_ctrlmapx.setcurrenttool (mytool_distance );//

}

Note: on_event (cpatrolcheckview, idc_map, 11/* mapx_dispid_polytoolused */, onpolytoolusedmap, where the mark is red. The judgment of specific tool types is carried out in onpolytoolusedmap.

The above part of the reference: csdn blog, reproduced please indicate the source: http://blog.csdn.net/fengrx/archive/2009/05/09/4163187.aspx

The following is my complete code:

// TestTView.h : interface of the CTestTView class///////////////////////////////////////////////////////////////////////////////#if !defined(AFX_TESTTVIEW_H__605D4332_7071_4CE3_9EFE_73D15BB50061__INCLUDED_)#define AFX_TESTTVIEW_H__605D4332_7071_4CE3_9EFE_73D15BB50061__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "MapX.h"#include "MainFrm.h"class CTestTView : public CView{protected: // create from serialization onlyCTestTView();DECLARE_DYNCREATE(CTestTView)// Attributespublic:CTestTDoc* GetDocument();// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CTestTView)public:virtual void OnDraw(CDC* pDC);  // overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);//}}AFX_VIRTUAL// Implementationpublic:BOOL OnToolTest();void OnPolyToolUsedMap(short ToolNum, long Flags, LPDISPATCH Points,BOOL bShift,BOOL bCtrl,BOOL FAR* EnableDefault);virtual ~CTestTView();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:private:CMapX m_ctrlMapX;// Generated message map functionsprotected://{{AFX_MSG(CTestTView)afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);afx_msg void OnTest();afx_msg void OnSize(UINT nType, int cx, int cy);//}}AFX_MSGDECLARE_MESSAGE_MAP()DECLARE_EVENTSINK_MAP()};#ifndef _DEBUG  // debug version in TestTView.cppinline CTestTDoc* CTestTView::GetDocument()   { return (CTestTDoc*)m_pDocument; }#endif///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_TESTTVIEW_H__605D4332_7071_4CE3_9EFE_73D15BB50061__INCLUDED_)
// Testtview. CPP: Implementation of the ctesttview class // # include "stdafx. H "# include" testt. H "# include" testtdoc. H "# include" testtview. H "# ifdef _ debug # define new debug_new # UNDEF this_filestatic char this_file [] = _ file __; # endif ////////////////////////////////////// //////////////////////////////////////// /ctesttviewimplement_dyncreate (ctesttview, cview) begin_message_map (ctesttview, cview) // {afx _ Msg_map (ctesttview) on_wm_create () on_command (id_test, ontest) Evaluate () //} afx_msg_map // standard printing commandson_command (id_file_print, cview: onfileprint) on_command (response, response, cview: onfileprint) on_command (outputs, cview: onfileprintpreview) end_message_map () outputs (ctesttview, cview) on_event (ctesttview, idc_map, outputs, onpolytoolusedmap, V Ts_i2 vts_i4 vts_dispatch vts_bool limit) limit () // ctesttview construction/Direction: ctesttview () {// todo: Add construction code here} ctesttview ::~ Ctesttview () {} bool ctesttview: precreatewindow (createstruct & CS) {// todo: Modify the window class or styles here by modifying // The createstruct csreturn cview :: precreatewindow (CS );} //////////////////////////////////////// /// // ctesttview drawingvoid ctesttview:: ondraw (CDC * PDC) {ctesttdoc * pdoc = getdocument (); assert_valid (pdoc); // todo: Add draw code for native da Ta here }///////////////////////////////////// //////////////////////////////////////// // ctesttview printingbool ctesttview:: onprepareprinting (cprintinfo * pinfo) {// default preparationreturn doprepareprinting (pinfo);} void ctesttview: onbeginprinting (CDC */* PDC */, cprintinfo */* pinfo */) {// todo: add extra initialization before printing} void ctesttview: onendprinting (CDC */* PDC */, cprintinfo */* pinfo */) {// Todo: add cleanup after printing }/////////////////////////////////// //////////////////////////////////////// /// ctesttview diagnostics # ifdef _ debugvoid ctesttview:: assertvalid () const {cview: assertvalid ();} void ctesttview: dump (cdumpcontext & DC) const {cview: dump (DC);} ctesttdoc * ctesttview :: getdocument () // non-debug version is inline {assert (m_pdocument-> iskindof (runtime_class (ctesttdoc ))); Return (ctesttdoc *) m_pdocument ;} # endif/_ debug ////////////////////////////////// //////////////////////////////////////// //// ctesttview message handlersint ctesttview:: oncreate (maid) {If (! M_ctrlmapx.create (null, ws_visible, crect (100,100,), this, idc_map) Return-1; m_ctrlmapx.setgeoset ("China. GST "); created (" "); // create a custom tool named tranquility (drag_tool, mitooltypepoly, microsscursor); // todo: add your specialized creation code here misizeallcursorif (cview :: oncreate (lpcreatestruct) =-1) Return-1; return 0;} void ctesttview: onpolytoolusedmap (short toolnum, long flag S, lpdispatch points, bool bshift, bool bctrl, bool far * enabledefault) {(cmainframe *) afxgetmainwnd ()-> m_wndstatusbar.setpanetext (0, "Start computing "); // calculate the distance // If (flags = mipolympus toolinprogress) {// This sentence can have no if (toolnum = drag_tool) {cmapxpoints PTS; long n; long I; try {PTS. attachdispatch (points, false); // get points objectdouble ddistancetot = 0.0; n = PTS. getcount (); m_ctrlmapx.setmapunit (miunitkilometer); for (I = 1; I <n; I ++ ){ Cmapxpoint pt1 = PTS. item (I); cmapxpoint pt2 = PTS. item (I + 1); double D = m_ctrlmapx.distance (pt1.getx (), pt1.gety (), pt2.getx (), pt2.gety (); ddistancetot + = D;} cstring STR; Str. format ("total distance: % F", ddistancetot); // The output distance in the status bar (cmainframe *) afxgetmainwnd ()-> m_wndstatusbar.setpanetext (0, STR ); // set m_wndstatusbar to public} catch (coledispatchexception * E) {e-> reporterror (); e-> Delete ();} catch (coleexception * E) {e-> rep Orterror (); e-> Delete () ;}/// if (flags = mipolympus toolinprogress)} void ctesttview: ontest () {// todo: add your command handler code example (drag_tool); (cmainframe *) afxgetmainwnd ()-> example (0, "gooooooooood");} void ctesttview: onsize (uint ntype, int CX, int CY) {cview: onsize (ntype, CX, CY); // todo: add your message handler code hereif (CX! = 0 & Cy! = 0) {m_ctrlmapx.movewindow (0, 0, CX, Cy, true) ;}} bool ctesttview: ontooltest () {MessageBox ("goood test"); Return true ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.