如何用VC实现图元响应鼠标事件?比如,我定义一了一个CLline对象,然后定义一个 line 实例;
在界面上画 一条线,然后我用鼠标 点一下这条线,线马上被选中,而且还可以以高亮,或者变色显示等等;

解决方案 »

  1.   

    可以根据直线的两点式得到直线的两点方程如:(x1,y1),(x2,y2):y = y1+((y2-y1)(x-x1))/(x2-x1),然后获得鼠标点击处的坐标,带入该方程用if判断if (y == y1+((y2-y1)(x-x1))/(x2-x1)),为真就表示点击了该线段,然后你就可以做你的处理了,注意Windows坐标系正负值与标准坐标系的区别。
      

  2.   

    明白了lz的意思,ls的也可以处理直线的方法,不过估计LZ的意思不是单独对于直线,
    用CRgn类,lz可以查看下msdn里面的帮助。先创建一个区域,然后判断你点击的点是不是在那个区域内,是的话,执行你要的操作。
    都直接有函数的,看你怎么转化了。
      

  3.   

    如果是直线的话就可以用这个了CRgn::CreatePolygonRgn 
    BOOL CreatePolygonRgn( LPPOINT lpPoints, int nCount, int nMode );Return ValueNonzero if the operation succeeded; otherwise 0.ParameterslpPointsPoints to an array of POINT structures or an array of CPoint objects. Each structure specifies the x-coordinate and y-coordinate of one vertex of the polygon. The POINT structure has the following form:typedef struct tagPOINT {
       int x;
       int y;
    } POINT;nCountSpecifies the number of POINT structures or CPoint objects in the array pointed to by lpPoints.nModeSpecifies the filling mode for the region. This parameter may be either ALTERNATE or WINDING.ResCreates a polygonal region. The system closes the polygon automatically, if necessary, by drawing a line from the last vertex to the first. The resulting region is stored in the CRgn object. The size of a region is limited to 32,767 by 32,767 logical units or 64K of memory, whichever is smaller.When the polygon-filling mode is ALTERNATE, the system fills the area between odd-numbered and even-numbered polygon sides on each scan line. That is, the system fills the area between the first and second side, between the third and fourth side, and so on. When the polygon-filling mode is WINDING, the system uses the direction in which a figure was drawn to determine whether to fill an area. Each line segment in a polygon is drawn in either a clockwise or a counterclockwise direction. Whenever an imaginary line drawn from an enclosed area to the outside of a figure passes through a clockwise line segment, a count is incremented. When the line passes through a counterclockwise line segment, the count is decremented. The area is filled if the count is nonzero when the line reaches the outside of the figure.When an application has finished using a region created with the CreatePolygonRgn function, it should select the region out of the device context and use the DeleteObject function to remove it.ExampleCRgn   rgnA, rgnB;CPoint ptVertex[5];ptVertex[0].x = 180;
    ptVertex[0].y = 80;
    ptVertex[1].x = 100;
    ptVertex[1].y = 160;
    ptVertex[2].x = 120;
    ptVertex[2].y = 260;
    ptVertex[3].x = 240;
    ptVertex[3].y = 260;
    ptVertex[4].x = 260;
    ptVertex[4].y = 160;VERIFY(rgnA.CreatePolygonRgn( ptVertex, 5, ALTERNATE));CRect rectRgnBox;
    int nRgnBoxResult = rgnA.GetRgnBox( &rectRgnBox );
    ASSERT( nRgnBoxResult != ERROR || nRgnBoxResult != NULLREGION );CBrush brA, brB;
    VERIFY(brA.CreateSolidBrush( RGB(255, 0, 0) ));  // rgnA Red
    VERIFY(pDC->FrameRgn( &rgnA, &brA, 2, 2 ));
    VERIFY(brB.CreateSolidBrush( RGB(0, 0, 255) ));  // Blue
    rectRgnBox.InflateRect(3,3);
    pDC->FrameRect( &rectRgnBox, &brB );
      

  4.   

    我达个船,LZ请不要介意:)当我们显示一副图像在窗口理,鼠标 点击图片后,周围有个能放大,缩小的焦点,就像FIREWORKES那样,请问这个是怎么实现的?
      

  5.   

    MSDN里面有一个例子,叫Draw***的吧,我忘了,你可以看看
      

  6.   

    根据这条线扩展成一个rgn, LButtonDown时做PtInRegion判断。