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 );//
}