Here's a bit of old code that might help, not so much with the generating of your geometries as this tool tracks multiple lines to the current mouse location, but the method of generating a geometry in your OnMouseMove then drawing it in a different event.
protected override void OnMouseMove(MouseEventArgs arg)
{
int X = arg.X; // screen coordinates, in pixels from upper left
int Y = arg.Y;
gMidPoint = gDispTran.ToMapPoint(X, Y); // get the screen point as a map point
// update the refesh envelope with how far the mouse has travelled - this object was created elsewhere on mouse down
if (gMidPoint.X < gRefreshEnvelope.XMin) gRefreshEnvelope.XMin = gMidPoint.X;
if (gMidPoint.X > gRefreshEnvelope.XMax) gRefreshEnvelope.XMax = gMidPoint.X;
if (gMidPoint.Y < gRefreshEnvelope.YMin) gRefreshEnvelope.YMin = gMidPoint.Y;
if (gMidPoint.Y > gRefreshEnvelope.YMax) gRefreshEnvelope.YMax = gMidPoint.Y;
IPointCollection pDrawPC;
IGeometryCollection pDrawGC = new PolylineClass();
// create a part for each tracked line
foreach (IPointCollection pThisPC in gLinePolyDrawList)
{
pDrawPC = new PathClass();
// add the first point and then the mouse location (snapped if applicable)
pDrawPC.AddPoint(pThisPC.get_Point(0), ref gMissing, ref gMissing);
pDrawPC.AddPoint(gMidPoint, ref gMissing, ref gMissing);
// if there's a second point (i.e. not an end) then add that
if (pThisPC.PointCount > 1) pDrawPC.AddPoint(pThisPC.get_Point(1), ref gMissing, ref gMissing);
// add the path to the polyline
pDrawGC.AddGeometry((IGeometry)pDrawPC, ref gMissing, ref gMissing);
}
// make it into a geometry and set the spatial reference... only
// lines with a spatial reference can be drawn.
gDrawGeometry = (IGeometry)pDrawGC;
gDrawGeometry.SpatialReference = gMap.SpatialReference;
// extend the envelope to include all of the tracked paths
gRefreshEnvelope.Union(gDrawGeometry.Envelope);
// do a partial refresh of just the area that has been (possibly) modified
gDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, gRefreshEnvelope);
Application.DoEvents();
}
void AfterDraw(IDisplay Display, esriViewDrawPhase phase)
{
// draws the lines that are created during the OnMouseMove
if (phase == esriViewDrawPhase.esriViewGeography)
{
if (!gSelectionStarted) return; // a boolean switch to say that there is a geometry
gScreenDis = gDoc.ActiveView.ScreenDisplay;
gScreenDis.SetSymbol(gSym);
gScreenDis.DrawPolyline(gDrawGeometry);
}
}
In the OnMouseMove event a geometry is created and then the envelope of the affected area is updated with partial refresh (quicker than refresh) which fires the event AfterDraw (wired on startup with ((IActiveViewEvents_Event)gDoc.FocusMap).AfterDraw += new IActiveViewEvents_AfterDrawEventHandler(AfterDraw);
.. you can only draw to the screen between the phases of IDisplay.StartDrawing and FinishDrawing except when the view is already being refreshed. gSym
is a line symbol; note you can only set one each of line, point and polygon symbols as current draw symbols, using SetSymbol again with an ISymbol of a previously set type will overwrite the existing setting.