Open a screen , draw a line with mouse

Hi,

What code do I need to open a blank screen ?
(On that screen, I want to draw, when I will move my mouse. )

you need a library for your operating system's graphics hardware to do that.
C++ does not understand 'mouse', 'screen', 'graphics', 'sound', 'windows', etc. All that stuff comes from libraries for your particular operating system and hardware.
which one do i need?
I have windows 10 on my laptop
and I write by MS visual studio
The library/libraries you need are already "built into" the OS. The Win 32 API. Using C/C++, the GDI system.

Or GDI+ system, also a part of Windows.

Both GDI and GDI+ are considered deprecated by Microsoft, DirectX is now the preferred drawing system.

Direct2D is used for basic 2D graphic rendering.
so ok what must I do then? I dont understand your last reply ) just beginner.... am i ok when i write on ms visual studio? or what else must install or use or do? do I install directx from somewhere? or what do i do with it? all i am doing is starting to write on ms visual studio.

i want to make my own drafting program . my goal is both to learn C++ and to have a simple drafting program on my own

Using the OS directly is always a pain.

What exactly are you trying to do? A clear direction is the difference between graphical toolkits.

For picture graphics (including lines), I blithely recommend SDL2. Very easy to install and use.

For GUI applications you would want something else.

If you tell use what you want this for, perhaps a better option is available.
Using the OS directly is always a pain.

Absolutely. Doubly so, really, since it's also not portable.
So I would also recommend SDL2:
https://lazyfoo.net/tutorials/SDL/
Here is what I am trying to do. I have 2 goals:

1- I am engineer. I want to make my own CAD drafting program, so that I will not have to use those cheap low quality programs, which are frustrating to use, or pay thousands of dollars subscription to good expensive programs. Drawing lines and dimensions accurately are enough. I am not looking to make game graphics or cartoons etc....

2- Learn C++ the right way, so that I can use it for other things in the future, for making other computer programs or even for hobby robotics,

3-I have time and desire, I can learn. I want to start in right direction

Here is my situation now:

1- I am very beginner in C++, but I had written some simple computer programs before such as php or fortran years ago

2- I have a laptop

3- On this laptop I have windows 10 installed

4- I saw on my computer I also have MS visual studio installed so I started writing on it... But in no way I am bound to this...

So please tell me exactly, where must I write C++, using what, installing what? Please tell me as simple as possible...
Last edited on
1) when you get a job, the company will pay for the cad program (meaning, you need to know how to use them). If you consult, its a business expense just like a MSDN subscription might be for me. If you can DIY, that is great, but you are trying to reinvent a 747 after taking shop class. Those cad programs do an awful lot of stuff.

That aside, there should be a visual studio 'paint' program example and tutorial on the web. Start there... but realize that drawing a line on the screen and keeping said line are different. Most paint type programs you draw and its forgotten. Cad programs track every line as part of an actual 3-d object, saving the points etc. Even the not-cad drawing programs for graphics programs do an insane amount of things.


consider https://www.bing.com/videos/search?q=visual+studio+example+paint&view=detail&mid=AEAA50D99EC0DC0E9A29AEAA50D99EC0DC0E9A29&FORM=VIRE

or http://www.code-home.com/Visual_c_drawing_tutorial_drawline_drawarc.html
Last edited on
I do not think you have any idea how deep this particular rabbit hole goes. CAD is a mind-bogglingly complex piece of software.


AutoCAD (what you should want to learn and use anyway) is not that expensive. As of this moment, you can use it for about US$200/month, which is a very good price.

NanoCAD is the Russian version of AutoCAD. I have no experience with it whatsoever, but it appears to be very good, and you can get the basic version for free.


If undaunted, SDL2 or DirectX should be your choice of graphics library.

Good luck!
I have 2 goals:
then you list 3 topics. So I conclude, you are an engineer not able to count up to 3.

Next, the subject of this thread was misleading for me. By reading "Open a screen , draw a line with mouse" my immediate idea was, you may open a window and have look at the chem- or contrails, leave the mouse alone.

A good CAD system is strictly based on analytic geometry - this has nothing to do with "drawing lines on a screen". You complain about "those cheap low quality programs, which are frustrating to use" -- because they focus on easy mouse input and display on screen and forget about the basics.

To make your own CAD system -- yes, it is possible, a colleague did so in his spare time at work -- the tools you list (laptop, Win10, ...) are of minor importance (Ada Lovelace wrote the first computer program with no real computer at all -- https://en.wikipedia.org/wiki/Ada_Lovelace#First_computer_program ), first consider what elements your system should handle, points, lines, circles too? Circles may be simulated by many smal chords... In case you tend to this idea -- RED NEON!! -- forget it, this kind of approximation is only for display but not for a tool for analytic geometry.

After you defined the "flock" of elements you need, take some decisions: how to tag them, how to store them (I don't need to tell an engineer about the advantages of vectors), how to file them, what attributes should be possible (linetype, linewidth, colour, show/noshow, layer, ...), if 2D or 3D, make your wish list.

Next, "interactions" of the elements: define new elements based on existing, points from intersections line-line, line-circle, circle-circle, tangents point-circle, circle-circle (four different possibe at best), younameit.

Next, "mainipulation" of elements, group, copy, move, rotate, stretch, change a. m. attributes, organize, merge files. And discard.

Up to now your system still lacks input and output. For the later consider the targeted devices, plotter, printer or screen only? Once upon a time HP-GL was a standard for every CAD system, and for a simple display on screen you may use the free HPGLview from CERN:
http://service-hpglview.web.cern.ch/service-hpglview/download_index.html

Input? Well, first by programing. Either from a language of your choice or a macro interface you add to it. As icing, when all else works as desired, you may consider an interacive interface which allows you to select elements displayed on screen for manipulation or input of new elements, for example dimensioning (consisting of extension lines, dimension line with arrows (consisiting of lines) or else, value as annotation).

Make a catalogue of all aspects you like or need, add a column for time it will probably take you or the costs if you buy it, sum it and ponder if it is worth it. Then go, do it. Or find someone who pays you for it. Or sell your engineering service for more hrs as it takes you, as my colleague did (without interactive input). What you plan to do is a spare time hobby.

At last one more hint, in case you go for 3D and use a pen plotter as output device, do not forget hidden line removal. Without it could be jeopardizing not only the pens and the paper (for a certainty) but also the plotter.
Hey ketanco.

Just curious. Were you able to get anywhere with either of those CAD programs? Were you able to get SDL2 or DirectX (and a C++ compiler) installed and make anything happen yet?
Last edited on
Create some simple SVG graphics.

View the output file (plot.svg) in a web browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;


class Svg
{
   ofstream out;

public:
   Svg( string filename, int width = 1000, int height = 1000, string background = "#FFFFFF" )
   {
      out.open( filename );
      out << "<?xml version=\"1.0\" standalone=\"no\"?>\n"
          << "<svg "
          << "xmlns=\"http://www.w3.org/2000/svg\" "
          << "width=\"" << width << "\" height=\"" << height << "\" "
          << "style=\"background: " << background << "\" "
          << ">\n";
   }


   ~Svg()
   {
      out << "</svg>";
      out.close();
   }


   void circle( double r, double cx, double cy )
   {
      out << "<circle r=\"" << r << "\" cx=\"" << cx << "\" cy=\"" << cy << "\" />\n";
   }
};


int main()
{
   double PI = 3.14159;
   string filename = "plot.svg";
   double width = 800, height = 800;

   double r0 = 10, dr = 5;
   double theta0 = 0, dtheta = 20;
   int npoints = 70;

   Svg svg( filename, width, height );
   for ( int i = 0; i < npoints; i++ )
   {
      double r = r0 + i * dr;
      double theta = theta0 + i * dtheta;
      double x = 0.5 * width  + r * cos( theta * PI /180.0 );
      double y = 0.5 * height - r * sin( theta * PI /180.0 );
      double radius = 0.03 * r;
      svg.circle( radius, x, y );
   }
}



Last edited on
@lastchance
O -- nice :)
Now... as an exercise only, please select the left most circle and change colour to yellow. Then connect the two circles to the bottom (r="10.35" and r="10.5") with all possible tangents, cut their length to the orthogonal projection of the next near circles' center. Keep one of the projection lines as dash-dotted line with linewidth 0.35, the tangens draw 0.5 black <grin>
Ah, no, sorry, interactive is the icing when all else works (including parametrics). The goal is
"so that I will not have to use those cheap low quality programs, which are frustrating to use"
http://www.cplusplus.com/forum/beginner/253401/#msg1114793
Topic archived. No new replies allowed.