Is there a way to get a program to draw a line between two points?
Any simple tutorials on the web would be very useful.
Printable View
Is there a way to get a program to draw a line between two points?
Any simple tutorials on the web would be very useful.
here's one - wether or not this is what you are looking for
http://www.cprogramming.com/tutorial/tut3.html
Anyone else?
Well you could start with which OS / Compiler / graphics library you're using.
Windows XP / Dev-C++ / none
So far I have this:
http://www.adrianxw.dk/SoftwareSite/...Consoles3.html
Here's a little example using LineTo (must use windows 2000 or later):
Code:#define _WIN32_WINNT 0x0500 //specifies the windows version is windows 2000 (to use GetConsoleWindow)
#include <windows.h>
#include <iostream>
int main()
{
HWND hwnd = GetConsoleWindow();
if (!hwnd)
std::cout<<"Error getting console window handle!";
HPEN hPen = CreatePen(PS_SOLID,5,RGB(255,255,255)); //create a solid white pen of width 5
HDC hdc = GetDC(hwnd); //get the device context
SelectObject(hdc,hPen); //select white pen
MoveToEx(hdc,0,0,NULL); //move to 0,0
LineTo(hdc,100,100); //draw from 0,0 to 100,100
ReleaseDC(hwnd,hdc); //release the device context
DeleteObject(hPen); //delete the pen
}
When tryign to compile that i get:
implicit declaration of function `int GetConsoleWindow(...)'
initialization to `HWND__ *' from `int' lacks a cast
What do those errors mean?
> implicit declaration of function `int GetConsoleWindow(...)'
Either you spelt it wrong, or you're not including the correct header files.
Well I just copied that code into a new source file.
Go to the menu...
File>new>project>windows application>
Type in a name for your file. Then copy and paste your code in there.
Does it need to be put in with all the stuff about making a window?
No try jawib's code on its own.
Why doesn't it work then?
By not work you mean?
Are you getting any compiler errors?
Yes which I posted earlier...both on the line:
Code:HWND hwnd = GetConsoleWindow();
I don't know then.
I will try a different compiler.
Good lord. Just install a simple graphics lib, Like Allegro or SDL and draw some lines.
http://www.libsdl.org/index.php
http://www.talula.demon.co.uk/allegro/
Well, the code posted is exactly what I need but it doesn't work and I don't know why.
I there any other code that I can use?
You might try using GetForegroundWindow() instead of GetConsoleWindow(). I don't know why the code I posted doesn't work for you (it compiles fine with visual studio, so it must be an issue with your compiler's headers)
Any reason why it wouldn't work with Dev-C++
I compile it with foreground window and I think there is a linking error...it compiles but won't run...
Anybody know what I would need to do to get the program working?
I went ahead and tried this with the latest Dev-C++ version and it works fine. You just need to link libgdi32.a and add a pause at the end so that the program doesn't terminate automatically.
How do I link the library?
Go to project options->parameters then under "Linker" click the "Add Library or Object" button, go to the lib directory and find the lib file.
Erm, I am using version 4 of Dev-C++
I honestly don't know what the differences between the versions of Dev-C++ are. Either way, make sure you actually created a project (if you just open a .cpp file you won't be able to change the linker options). There has to be a setting somewhere once you've done that...
I have found where to add the library and I have tried adding it the way it tells me and it still doesnt work.