VB下在PICTUREBOX上用API画图怎么定义坐标?
Public Declare Function MoveToEx Lib "gdi32" Alias "MoveToEx" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
Public Declare Function LineTo Lib "gdi32" Alias "LineTo" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
这些函数我都知道,但不会定位画,我现在能画出来,但想改变位置,感觉很不自由?有哪位高手知道吗?

解决方案 »

  1.   

    Option Explicit'This project needs:
    '- two picture boxes
    '- a button
    Private Type POINTAPI
        X As Long
        Y As Long
    End TypePrivate Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
    Private Declare Function PaintDesktop Lib "user32" (ByVal hdc As Long) As Long
    Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
    Private Declare Function GetBkColor Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As POINTAPI) As Long
    Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As LongConst ScrCopy = &HCC0020
    Const Yellow = &HFFFF&
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim Cnt1 As Byte, Cnt2 As Byte, Point As POINTAPI
        'Set the graphic mode to persistent
        Me.AutoRedraw = True
        'API uses pixels
        Me.ScaleMode = vbPixels
        Picture1.ScaleMode = vbPixels
        Picture2.ScaleMode = vbPixels
        'No borders
        Picture1.BorderStyle = 0: Picture2.BorderStyle = 0
        'Set the button's caption
        Command1.Caption = "Paint && Stretch"
        'Set the graphic mode to 'non persistent'
        Picture1.AutoRedraw = False: Picture2.AutoRedraw = False
        For Cnt1 = 0 To 100 Step 3
            For Cnt2 = 0 To 100 Step 3
                'Set the start-point's co鰎dinates
                Point.X = Cnt1: Point.Y = Cnt2
                'Move the active point
                MoveToEx Me.hdc, Cnt1, Cnt2, Point
                'Draw a line from the active point to the given point
                LineTo Me.hdc, 200, 200
            Next Cnt2
        Next Cnt1
        For Cnt1 = 0 To 100 Step 5
            For Cnt2 = 0 To 100 Step 5
                'Draw a pixel
                SetPixel Me.hdc, Cnt1, Cnt2, Yellow
            Next Cnt2
        Next Cnt1
    End Sub
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim XX As Long, YY As Long, A As Long
        XX = X: YY = Y
        'Set the picturebox' backcolor
        Picture2.BackColor = GetPixel(Picture1.hdc, XX, YY)
    End Sub
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = 1 Then
            Dim XX As Long, YY As Long, A As Long
            XX = X: YY = Y
            'Set the picturebox' backcolor
            Picture2.BackColor = GetPixel(Picture1.hdc, XX, YY)
        End If
    End Sub
    Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim XX As Long, YY As Long, A As Long
        XX = X: YY = Y
        'Set the picturebox' backcolor
        Picture2.BackColor = GetPixel(Picture1.hdc, XX, YY)
    End Sub
    Private Sub Command1_Click()
        'Set the width and height
        Picture2.Width = 100: Picture2.Height = 100
        Picture1.Width = 50: Picture1.Height = 50
        'No pictures
        Picture1.Picture = LoadPicture("")
        DoEvents
       ' Copy the desktop to our picturebox
       ' PaintDesktop Picture1.hdc
        'Stretch the picture
        StretchBlt Picture2.hdc, 0, 0, 100, 100, Picture1.hdc, 0, 0, 50, 50, ScrCopy
    End Sub
      

  2.   

    picturebox的BorderStyle为1的时候,它的每一边都占用2个像素。那么假设这个picturebox在屏幕上的位置为(100,100),那么它的可画区域的LeftTop(0,0)点就是(102,102),当其BorderStyle为0的时候,它的可画区域的LeftTop(0,0)点就是(100,100),另外,1 pixel = 15 twips ,用API画的时候都是以pixel为单位的。你可以用API函数GetWindowRect获得pictureBox在屏幕上的位置。
      

  3.   

    你们的回答不太全面,只说了画线可没说定义坐标,所以我决定给“ daisy8675(莫依)”40分,我没用过,怎么给你分告诉我吧。
      

  4.   

    楼上的几位都在干什么阿,人家楼主问个问题容易吗,你们回答的都是什么呀,和问题根本不相干阿。偶来直接问答:
    PictureBox的坐标是以左上角为坐标原点(0,0)向右为X正向,向下为Y正向。
    PictureBox可以设定很多单位,但是在画图的时候,只有把DrawMode设为3-pixel,才有实用意义。
    而且用API画图,它只会用Pixel单位来画。这个Pixel就是像素单位。
    不要用其他方法去换算PictureBox的坐标方向(很多人开始的时候不习惯这个Y向下为正的方法),因为以后你就会发现很多很多东西都是使用这样的坐标系,因此你最好还是适应它。比如你用了一个PICTUREBOX,设为像素单位,并把控件的长度和宽度都设为100 PIXEL
    此时画一条从左上角到右下角的线就是:
    MoveToEx Picture1.hdc,0,0,Null
    LineTo Picture1.hdc,100,100
      

  5.   

    WallesCai(算了下命,居然说我今年桃花遍地,紫溦星临头!)你的回答不错,可我想再问你一下,我画的线怎么能够按比例缩放?好象是有两个API函数可设置此比例。即逻辑坐标与设备坐标的(好象是这样),你能详细的说一下吗?
      

  6.   

    WallesCai(算了下命,居然说我今年桃花遍地,紫溦星临头!) 
    将scalemode设为3,非DrawMode吧
      
     
      

  7.   

    VB默认的坐标刻度是缇,可以用
    xp = xt / Screen.TwipsPerPixelX
    yp = yt / Screen.TwipsPerPixelY
    来得到以象素为单位的坐标
      

  8.   

    在form或PictureBox用line circle等函数直接画图,都是以缇twip为单位的,这是一个绝对单位,和屏幕的大小的分辨率无关,便于显示和打印~