How to draw a line
Hi,
I have been trying to draw a line based on some mouse clicks. Here is what I have done using the provided sample_app as a template in Visual Studio 2005 (64-bit OS).
CGMDLL_TesterDlg::OnLoadLayer()
...
GM_Projection_t theCurProj;
GM_Error_t32 theErr = GM_GetProjection( &theCurProj );
// customVectorLayer is a protected member of this Dlg
customVectorLayer=GM_CreateCustomVectorLayer("Custom Vector Layer",&theCurProj);
mMapWindow.AddLayer(customVectorLayer);
...
CGMDLL_TesterDlg::OnLButtonUp()
...
GM_LineStyle_t theDrwLineStyle;
theDrwLineStyle.mPenColor = RGB(255,0,0);
theDrwLineStyle.mFont.mWeight = FW_BOLD;
GM_SetLineFeatureDrawStyle(customVectorLayer,0,&theDrwLineStyle);
GM_LineFeature_t theDrwLineFeature;
// Cur_Points_Num is a member of the Dlg
theDrwLineFeature.mNumPoints= Cur_Points_Num;
theDrwLineFeature.mLineStyle= theDrwLineStyle;
// Cur_Points is a member of the Dlg: an array of GM_Point_t
theDrwLineFeature.mPointList= Cur_Points;
GM_AddLineToVectorLayer(customVectorLayer,&theDrwLineFeature,TRUE);
...
The program gave an "Unhandler exception ..." at the last line above.
Could you help me on this?
Thanks,
Qiao
I have been trying to draw a line based on some mouse clicks. Here is what I have done using the provided sample_app as a template in Visual Studio 2005 (64-bit OS).
CGMDLL_TesterDlg::OnLoadLayer()
...
GM_Projection_t theCurProj;
GM_Error_t32 theErr = GM_GetProjection( &theCurProj );
// customVectorLayer is a protected member of this Dlg
customVectorLayer=GM_CreateCustomVectorLayer("Custom Vector Layer",&theCurProj);
mMapWindow.AddLayer(customVectorLayer);
...
CGMDLL_TesterDlg::OnLButtonUp()
...
GM_LineStyle_t theDrwLineStyle;
theDrwLineStyle.mPenColor = RGB(255,0,0);
theDrwLineStyle.mFont.mWeight = FW_BOLD;
GM_SetLineFeatureDrawStyle(customVectorLayer,0,&theDrwLineStyle);
GM_LineFeature_t theDrwLineFeature;
// Cur_Points_Num is a member of the Dlg
theDrwLineFeature.mNumPoints= Cur_Points_Num;
theDrwLineFeature.mLineStyle= theDrwLineStyle;
// Cur_Points is a member of the Dlg: an array of GM_Point_t
theDrwLineFeature.mPointList= Cur_Points;
GM_AddLineToVectorLayer(customVectorLayer,&theDrwLineFeature,TRUE);
...
The program gave an "Unhandler exception ..." at the last line above.
Could you help me on this?
Thanks,
Qiao
Comments
-
Qiao,
Where is your theDrwLineFeature structure initialized? Make sure that you are zeroing out the structure with the following call to make sure you aren't getting some garbage in members that you aren't explicitly initializing:
memset( &theDrwLineFeature, 0, sizeof theDrwLineFeature );
Let me know if I can be of further assistance.
Thanks,
Mike
Global Mapper Support
support@globalmapper.com -
Hi Mike,
Thanks, yes the error goes away after I added that line.
Now I still can not get the lines shown in the map. Here are some lines afterwards in CGMDLL_TesterDlg::OnLButtonUp().
GM_DrawFlags_t32 aDrawFlags=GM_DrawFlags_SeparateVectorLayers ; // 0x00001000 ;
CRect mDrwRect= mMapWindow.GetDrawWndRect();
CPaintDC thePaintDC( this );
GM_DrawLayerList(thePaintDC,&customVectorLayer,1,aDrawFlags,&(mMapWindow.GetCurrentViewRect)),mDrwRect.left,mDrwRect.top,mDrwRect.Width(),mDrwRect.Height());
Do I need to add something under ::OnPaint()?
Thanks,
Qiao -
Qiao,
You don't want to do your drawing in your left button handler. Instead just call Invalidate() in your OnLButtonUp handler which will trigger your map window to redraw. Since it contains your custom vector layer it will be redrawn as well.
Let me know if I can be of further assistance.
Thanks,
Mike
Global Mapper Support
support@globalmapper.com -
Hi Mike,
Thanks again. I added that line after GM_DrawLayerList(). The lines are still not shown. Do you see any problems in my GM_DrawLayerList() call? I am not sure whether the hDC parameter has been correctly set.
I checked the mPointList of the "theDrwLineFeature". It seems correctly assigned.
Thanks,
Qiao -
Qiao,
You need to move all of your drawing code to OnPaint and just call Invalidate from the OnLButton handler. The DC in the OnPaint call should be the CPaintDC that is already available there.
Thanks,
Mike
Global Mapper Support
support@globalmapper.com -
Great, I can see the lines now. Only the color is not the one I am askinf for. It shows gray level instead of red. Here is the block I put under OnPait() before "isDrawing=false".
if(Cur_Points_Num>1)
{
GM_LineStyle_t theDrwLineStyle;
theDrwLineStyle.mPenColor = RGB(255,0,0);
theDrwLineStyle.mFont.mWeight = FW_BOLD;
GM_SetLineFeatureDrawStyle(customVectorLayer,0,&theDrwLineStyle);
GM_LineFeature_t theDrwLineFeature;
memset( &theDrwLineFeature, 0, sizeof theDrwLineFeature );
theDrwLineFeature.mNumPoints= Cur_Points_Num;
theDrwLineFeature.mLineStyle= theDrwLineStyle;
theDrwLineFeature.mPointList= Cur_Points;
GM_AddLineToVectorLayer(customVectorLayer,&theDrwLineFeature,TRUE);
GM_DrawFlags_t32 aDrawFlags=GM_DrawFlags_SeparateVectorLayers ; // 0x00001000 ;
CRect mDrwRect= mMapWindow.GetDrawWndRect();
GM_DrawLayerList(thePaintDC,&customVectorLayer,1,aDrawFlags,&(mMapWindow.GetCurrentViewRect()),mDrwRect.left,mDrwRect.top,mDrwRect.Width(),mDrwRect.Height());
}
Thanks,
Qiao -
You need to pass FALSE for the 3rd parameter to GM_AddLineToVectorLayer otherwise the default style for the type will be used.
Note that I would still add the line to the vector layer in your OnLButton handler, just only do the drawing part in OnPaint.
Thanks,
Mike
Global Mapper Support
support@globalmapper.com -
Thanks for your suggestions. I moved the line-to-layer part under LButtonUp().
However, the color is still not rgiht. Please see the attached for the quick watch screenshot of the defined LineStyle. -
Qiao,
Looking at your code, you need to be initializing the mDrawStyle member of the GM_LineFeature_t structure, not setting up a separate structure. You also wouldn't call the GM_SetLineFeatureDrawStyle function for a new feature since the style is provided when you add the line.
Let me know if I can be of further assistance.
Thanks,
Mike
Global Mapper Support
support@globalmapper.com
Categories
- 13K All Categories
- 5.8K Features Discussion
- 350 Downloading Imagery
- 1.3K Elevation Data
- 385 Georeferencing Imagery Discussion
- 652 GM Script Language
- 56 User Scripts
- 115 GPS Features
- 421 Projection Questions
- 837 Raster Data
- 1.4K Vector Data
- 6.7K Support
- 182 Announcement and News
- 943 Bug Report
- 562 SDK
- 1.2K Suggestion Box
- 3.8K Technical Support
- 581 Other Discussion
- 132 GIS Data Sources
- 27 Global Mapper Showcase
- 245 How I use Global Mapper
- 111 Global Mapper Forum Website
