我在picturebox画了一个任意的封闭图形,我现在要做,如果在这个封闭图形上点击就用颜色来填充,如何做?谢谢

解决方案 »

  1.   

    在MouseDown事件里有两个参数时表示鼠标点击时的坐标,根据这个坐标就可以判断是不是在你那个封闭图形内了。(封闭图形也是有一个坐标范围的)
      

  2.   

    '********************************************************************************
    '*   功能  描述:判断一点是否在框内
    '*   参数  说明:
    '*         输入:X,Y,Top,Left,Width,Height
    '*         输出:None
    '*   返回值说明:Boolean
    '*   作      者:阿九
    '*   更      新:
    '*   创建  日期:2003/8/6
    '*   更新  日期:
    '********************************************************************************
    Public Function PointIsRectangle(ByVal X As Single, ByVal Y As Single, ByVal Top As Single, _
                                     ByVal Left As Single, ByVal Width As Single, ByVal Height As Single) As Boolean
                
        Dim bFlag       As Boolean
        
        bFlag = True
        If X < Left Or X > Left + Width Then bFlag = False
        If Y < Top Or Y > Top + Height Then bFlag = False
        PointIsRectangle = bFlag
    End Function
      

  3.   

    调用:
    我是在ListItem中判断是否点中一个区域的
    Private Sub lvwDetail_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
       If PointIsRectangle(X, Y, mItem.Top, mItem.Left, mItem.Width, mItem.Height) Then
       '相应处理
    End Sub
      

  4.   

    我的封闭区域是不能用WIDTH和HEIGHT来指定的。比如我区域的是一个扇形,我希望我在点击不是扇形区域的时候不会在任何地方填充颜色,而在扇形区域点击的时候只会填充扇形。
      

  5.   

    API PtInRect
    PtInRegion
      

  6.   

    //我在picturebox画了一个任意的封闭图形,我现在要做,如果在这个封闭图形上点击就用颜色来填充,如何做?谢谢要实现填充的话,可以用api函数FloodFill:【VB声明】
      Private Declare Function FloodFill Lib "gdi32" Alias "FloodFill" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long【说明】
      用当前选定的刷子在指定的设备场景中填充一个区域。区域是由颜色crColor定义的 【返回值】
      Long,非零表示成功,零表示失败。会设置GetLastError 【备注】
      点x,y绝对不能有颜色crColor,而且必须在剪切区域内。这个函数只对光栅设备有效,请参考ExtFloodFill的注解【参数表】
      hdc ------------  Long,设备场景的句柄  x,y ------------  Long,开始填充的那个点,用逻辑坐标表示  crColor --------  Long,欲使用的边界颜色。由这个颜色包围的表面会被填充//如何判断鼠标是否在某一区域,100分求助
    用apiPtInRect或PtInRegion都可以,下面函数的例子:Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function PtInRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function CreateEllipticRgnIndirect Lib "gdi32" (lpRect As RECT) As Long
    Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
    Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Private Sub Form_Load()
        Dim mRGN As Long, R As RECT, x As Long, y As Long
        'Set the graphical mode to persistent
        Me.AutoRedraw = True
        'Set the rectangle's values
        SetRect R, 0, 0, 50, 50
        'Create an elliptical region
        mRGN = CreateEllipticRgnIndirect(R)
        For x = R.Left To R.Right
            For y = R.Top To R.Bottom
                'If the point is in the region, draw a green pixel
                If PtInRegion(mRGN, x, y) <> 0 Then
                    'Draw a green pixel
                    SetPixelV Me.hdc, x, y, vbGreen
                ElseIf PtInRect(R, x, y) <> 0 Then
                    'Draw a red pixel
                    SetPixelV Me.hdc, x, y, vbRed
                End If
            Next y
        Next x
        'delete our region
        DeleteObject mRGN
    End Sub