Magnum357jhp
Community Member

Posts: 128
|
 |
« on: June 05, 2009, 05:10:53 PM » |
|
I've just started messing with a little programing with Windows API Functions. The following code is suppose to draw a simple line in a Windows Picture box with different colors...
Option Explicit
Public Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Sub Form_Load() Dim i As Integer Picture1.AutoRedraw = True For i = 1 To 100 SetPixel Picture1.hdc, i + 1, i + 1, 255 Next i
End Sub
The problem I'm having is that the line only shows up red when you run the program and I would like to add Blue and Green values so that I can change the lines color to anything I want. The reference book I'm using barely tells me anything about this API function. Does anyone know how I can get this line to display more then just the color red?
|
|
|
|
|
Logged
|
"There is no such thing as the ultimate programming language. Every language has its weaknesses."
|
|
|
|
SylvainTV
|
 |
« Reply #1 on: June 05, 2009, 05:28:39 PM » |
|
the color 255 is red. Use RGB(red, green, blue) to make another color (red, green and blue values are between 0 and 255)
|
|
|
|
|
Logged
|
|
|
|
Magnum357jhp
Community Member

Posts: 128
|
 |
« Reply #2 on: June 06, 2009, 02:04:01 AM » |
|
First, is "RGB()" a function in Windows API? I was hoping to stick with "SetPixel" because its kinda handy for a program I was working on (seting individual Pixels would be most useful with another project). If "RGB()" is a function, can you give me an example on how to set an individual pixel with it?
As for the previous code, can you give an example on how I can add in the Green and Blue values to this API function? If I set the last value of the function (ByVal crColor As Long) to "0" it just gives a black line. At 255, it gives red. Anything inbetween that just changes the line to a different shade of Red.
|
|
|
|
|
Logged
|
"There is no such thing as the ultimate programming language. Every language has its weaknesses."
|
|
|
jviper
Community Member

Posts: 2130
Discipline in training
|
 |
« Reply #3 on: June 06, 2009, 10:29:57 AM » |
|
Try number bigger than 255, like 65000, or 128000
|
|
|
|
« Last Edit: June 06, 2009, 10:35:32 AM by jviper »
|
Logged
|
JAbstract.....Don't just imagine, make it happen!
|
|
|
Magnum357jhp
Community Member

Posts: 128
|
 |
« Reply #4 on: June 06, 2009, 06:30:11 PM » |
|
I did, just gives me a black line like if it was set at 0. Should I add this "RGB()" inside the (ByVal crColor As Long) part of the function? Would it do any good?
|
|
|
|
|
Logged
|
"There is no such thing as the ultimate programming language. Every language has its weaknesses."
|
|
|
jviper
Community Member

Posts: 2130
Discipline in training
|
 |
« Reply #5 on: June 06, 2009, 08:40:42 PM » |
|
Not sure what RGB function they are talking about (if it is the RGB function I think is it), but the one you are looking for should be a system function (a function predefined) that takes in 3 arguments (Red, Green and Blue Color components), and returns a Long.
Problem is since you are using a "legacy" API function, the color could be OLE and not RGB component, in which case no RGB function would work. You would need to find a function to convert the OLE Color. If so, it may be better to find a different gdi API function, one that takes and Red, Green and Blue Component. Or there could be a predefined function.
Might I ask why you are not using TVScreen2DImmediate to draw 2D to the picture box? You understand that if you are using API functions to draw to a picture box, you cannot use TV3D to draw to that picture box, right? Drawing the the picture box using TV3D will erase what you draw using the API functions.
|
|
|
|
« Last Edit: June 06, 2009, 08:49:45 PM by jviper »
|
Logged
|
JAbstract.....Don't just imagine, make it happen!
|
|
|
Magnum357jhp
Community Member

Posts: 128
|
 |
« Reply #6 on: June 07, 2009, 02:05:03 AM » |
|
jviper, thanks for the advice about mixing Truevsion and API functions to a picture box. I did not know that you can't really mix the two together very well. I'll keep that in mind for the future. The good news about my problem is that I tried the "RGB()" function within in the (ByVal crColor As Long) part of the "GetPixel" function. It worked perfectly this time. I can get the line to change to different colors now.  In case anyone is interested, here is how the code looks now... Option Explicit 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 Sub Form_Load() Dim i As Integer Picture1.AutoRedraw = True For i = 1 To 100 SetPixel Picture1.hdc, i + 1, i + 1, RGB(100, 255, 100) Next i End Sub I'm not sure what you mean by "legacy" function, but I am using VB6 and I have not idea if VB.net uses the same API functions. This little project is sort of unrelated to Truevision (although it could be later if I think of something that might relate it). I just wanted to find a why so that I could customize pixel colors inside a Picture box, would come in handy for 2D type games or similar projects.
|
|
|
|
|
Logged
|
"There is no such thing as the ultimate programming language. Every language has its weaknesses."
|
|
|
|
scottpipper
|
 |
« Reply #7 on: June 08, 2009, 03:40:31 PM » |
|
Hi, the function RGB is a Visual Basic function, you have to set the color on the picturebox as a 16 bit color, so rgb function converts the bytes from r g b into a long value you can use so its
Dim H as long H = RGB(byte_red,Byte_green,Byte_blue)
then you set the value on the picturebox, using H Picture.PSet (x, y), H
|
|
|
|
« Last Edit: June 08, 2009, 03:45:56 PM by scottpipper »
|
Logged
|
|
|
|
Magnum357jhp
Community Member

Posts: 128
|
 |
« Reply #8 on: June 09, 2009, 03:48:20 PM » |
|
Ok, just out of curiosity, is this code better then the one I already used? Ill tryout your idea too, but just was wondering if there is any advantage to it.
|
|
|
|
|
Logged
|
"There is no such thing as the ultimate programming language. Every language has its weaknesses."
|
|
|
|
scottpipper
|
 |
« Reply #9 on: June 10, 2009, 07:45:39 AM » |
|
Not really, that's just the one I used, just wanted to explain the RGB function
|
|
|
|
|
Logged
|
|
|
|
Magnum357jhp
Community Member

Posts: 128
|
 |
« Reply #10 on: June 10, 2009, 04:54:24 PM » |
|
Ok, cool. One last question, how do I set the Picture box to a 16 bit pallet? Also, would this limit the values of the each color (ie. would it be lower then 255, say between 0 to 64 or something like that)?
|
|
|
|
|
Logged
|
"There is no such thing as the ultimate programming language. Every language has its weaknesses."
|
|
|
jviper
Community Member

Posts: 2130
Discipline in training
|
 |
« Reply #11 on: June 10, 2009, 07:53:56 PM » |
|
Why would you need to?
|
|
|
|
|
Logged
|
JAbstract.....Don't just imagine, make it happen!
|
|
|
|
scottpipper
|
 |
« Reply #12 on: June 11, 2009, 07:19:02 PM » |
|
If your working with VB then all your colors are 16 bit, if you use a function like savepicture on the picturebox, the the image is saved as 8 bit, anything different than that you will have to code.
|
|
|
|
|
Logged
|
|
|
|
|