Solved

Chart and Curve in VBA API

Posted on 2011-09-13
4
698 Views
Last Modified: 2012-06-21
Dear Expert,
I got one question on VBA,  I am learning how to plot curve in vba and
find some example  in goole seach and find following example and VBA code
But I am not familar with VBA userform1 usage , just want to know how to
let the form_paint() run to draw a curve. I try to run it in form_paint() but
it report "Me" word  is not using correctly.

So the question is
What is "Me" means in the code, how to use it ?
Where the curve should be displayed, is it on userform window or Excel window ? how to make
it happen,  Any good example for beginner ? Do I need to create userform1 first so then
call form_point() function, but how ? any simple code me to start ?

Pleae advise

Duncan
Private Type POINTAPI
        x As Long
        y As Long
End Type
Private Declare Function PolyBezier Lib "gdi32.dll" (ByVal hdc As Long, lppt As POINTAPI, ByVal cPoints As Long) As Long
Private Declare Function PolyBezierTo Lib "gdi32.dll" (ByVal hdc As Long, lppt As POINTAPI, ByVal cCount As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Any) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function PolyPolygon Lib "gdi32.dll" (ByVal hdc As Long, lpPoint As POINTAPI, lpPolyCounts As Long, ByVal nCount As Long) As Long

Private Sub Form_Paint()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim pts(0 To 6) As POINTAPI
    Dim numpoints(0 To 1) As Long
    'set the co¿¿dinates
    pts(0).x = 0: pts(0).y = 100
    pts(1).x = 125: pts(1).y = 75
    pts(2).x = 255: pts(2).y = 148
    pts(3).x = 219: pts(3).y = 199
    pts(4).x = 315: pts(4).y = 203
    pts(5).x = 236: pts(5).y = 16
    pts(6).x = 122: pts(6).y = 123
    'set the forecolor to green
    Me.ForeColor = vbGreen
    'draw the b¿¿ier
    PolyBezier Me.hdc, pts(0), 7
    'set the forecolor to red
    Me.ForeColor = vbRed
    'move the 'active' point
    MoveToEx Me.hdc, 200, 25, ByVal 0&
    'set the co¿¿dinates
    pts(0).x = 125: pts(0).y = 50
    pts(1).x = 123: pts(1).y = 200
    pts(2).x = 102: pts(2).y = 100
    pts(3).x = 102: pts(3).y = 100
    pts(4).x = 312: pts(4).y = 75
    pts(5).x = 289: pts(5).y = 125
    'draw the b¿¿ier
    PolyBezierTo Me.hdc, pts(0), 6
    'set the forecolor to blue
    Me.ForeColor = vbBlue
    'set the points belonging to the rectangle
    pts(0).x = 20: pts(0).y = 10
    pts(1).x = 200: pts(1).y = 10
    pts(2).x = 200: pts(2).y = 190
    pts(3).x = 20: pts(3).y = 190
    numpoints(0) = 4
    'set the points belonging to the triangle
    pts(4).x = 100: pts(4).y = 0
    pts(5).x = 50: pts(5).y = 100
    pts(6).x = 150: pts(6).y = 100
    numpoints(1) = 3
    'draw the polygons
    PolyPolygon Me.hdc, pts(0), numpoints(0), 2
End Sub

Open in new window

0
Comment
Question by:duncanb7
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36528154
I think the code you have downloaded was probably created for VB6, and you'll find it difficult to get it working in Excel VBA.  This code is intended to draw the shapes on a userform, which is not easy in VBA. Are you trying to draw shapes onto an Excel worksheet?  If so there are various methods of the Shapes collection that you can look up online, for example AddPolyLine, AddCurve which allow you to do this without using Windows apis.
0
 
LVL 13

Author Comment

by:duncanb7
ID: 36528172
ANd now I create an userform1 and add one command button to call Form_Paint() and
I replace the name of Form_paint() by Commandbutton1_Click(), now it won't report "Me"
error, porbably, ME is refer to userform1. But now report error on Me.hdc  in which hdc is
not member. What is hdc, How can I find correct Me.hdc ?

Thanks

Duncan
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 36528217
That's why I suggested you do it another way.  It's complicated and not easy to maintain and won't be guaranteed to work in future Office versions.  But if you want to go down that route, take a look at this:  http://www.cpearson.com/excel/formcontrol.aspx
0
 
LVL 13

Author Closing Comment

by:duncanb7
ID: 36528412
If I set hdc instead me.hdc and put
hdc=Getforegroundwindow(), and it works better

Thanks for your reply

Duncan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to open excel in 2016 is slow 4 21
DBF to ... Converter 5 43
sql server query from excel 3 57
Excel 2010 - Populate a formula in multiple cells 6 26
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now