Creating a Line in Paint... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Creating a Line in Paint...

In MS - Paint, creating a straight line is simply done by pressing shift and moving the mouse to the required length. I wish to create a similar function in my ASCII Paint Program. I thought of the following... 0) Read the Starting cursor position when the user presses the SHIFT Key and the Mouse Button. 1) If the user presses the left Shift, keep the Y coordinate constant. 2) Paint the line keeping X const or Y const. But in this approach, the X is read on each movement. So, how to achieve my goal?

5th Aug 2017, 11:42 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
20 Answers
+ 7
@Kinshuk. You will have to create a WndProc function to handle messages. You might find some tips here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/ef5a76cd-966b-4cf3-a05e-7b809918cedc/message-handling-in-a-console-app?forum=vcgeneral And here: https://stackoverflow.com/questions/41311474/how-to-register-wndproc-in-console-app But the important part is the drawing and you will have to use GDI to draw a line which you can find some info on here: https://www.daniweb.com/programming/software-development/code/216430/add-a-little-graphics-to-your-console
11th Aug 2017, 3:34 AM
Karl T.
Karl T. - avatar
+ 8
@Karl T. I tried a similar approach without the DrawLine Function. I first read the coord of the place where the mouse was first pressed and simultaneously shift was pressed. Then I stored that index and used it to get a coordpos for the CHAR_INFO array for updating. But the problem I faced was that the stored coord was getting updated on each movement of the mouse with the left button pressed. Maybe my. approach wasn't able to distinguish between a starting click and holding of a button... Ill try again...
7th Aug 2017, 4:08 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 8
Yeah make sure you store the clicked coordinates separately from the current mouse movement coordinates.
7th Aug 2017, 7:34 AM
Karl T.
Karl T. - avatar
+ 7
You don't even need to use a shift key. You just need to detect a mouse down message and get the original clicked x y coordinates. Then you detect a mouse move message with current coords x2 y2 and update the line drawing using DrawLine(x,y,x2,y2) from a HDC (from GDI lib) I suggest you get acquainted with common Win32 functions and message handling first...such as mouse clicks. https://msdn.microsoft.com/en-us/library/windows/desktop/gg153548(v=vs.85).aspx
6th Aug 2017, 4:51 PM
Karl T.
Karl T. - avatar
+ 6
https://code.sololearn.com/cAlNxwpPFqST/?ref=app Here I use FinalY to save the Y coordinate when Shift is pressed... This will be line 577... But It changes when I move the mouse, and continue keeping shift pressed. I want that to remain const...
5th Aug 2017, 12:53 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
Why don't you use device context line drawing functions?
6th Aug 2017, 4:23 PM
Karl T.
Karl T. - avatar
+ 6
@Karl T. Ill try implementing that. Thank You!
7th Aug 2017, 4:02 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
@Jay Not yet... I am still trying to find a way without using translatemessage, dispatchmessage, etc... I wish to rely only on INPUT_RECORD, but as I move on, it seems that this will not be possible...
10th Aug 2017, 4:43 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
Supercool idea. Cant wait to see the results. I was only doing mine as an image editor for an ascii game. I gave up on that idea and decided just to do in sdl 😴
11th Aug 2017, 9:26 AM
jay
jay - avatar
+ 5
it should be possible.. . i made a program almost exactly the same but didnt bother with lines. i will revisit it and see if i can do it. its 3am here atm.. otherwise i would look now. in my version i kept the pixels in memory (1D array) at all times so working with screen data is really straight forward.
10th Aug 2017, 5:13 PM
jay
jay - avatar
+ 5
@jay All I need is a way to forcefully fix the Y coordinate and Im done. But WM_MOUSEMOVE is a Message, and currently I don't read messages in my code. Now, Ill either redefine the code completely for reading messages as cases or write seperate code for the Line Drawing part.
11th Aug 2017, 12:35 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
@Karl T. Thank You for the info. I want ascii lines in this version of paint, and so will create a GDI version seperately. That will also offer fill, ellipses, circles, rectangles, etc...
11th Aug 2017, 9:18 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
you should use WinAPI functions
5th Aug 2017, 11:50 AM
Andrés04_ve
Andrés04_ve - avatar
+ 4
No, unfortunately not.
5th Aug 2017, 12:45 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
did you solve this?
10th Aug 2017, 4:27 PM
jay
jay - avatar
+ 3
Which one? Im currently using Buf.Event.MouseEvent.dwMousePosition.Y for the position and GetKeyState for the key event .
5th Aug 2017, 11:52 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Is that working?
5th Aug 2017, 12:21 PM
Andrés04_ve
Andrés04_ve - avatar
+ 3
could i see your code?
5th Aug 2017, 12:46 PM
Andrés04_ve
Andrés04_ve - avatar
+ 3
I also need this program 😅 But I'll be making it in Java
6th Aug 2017, 4:00 PM
Limitless
Limitless - avatar
+ 2
I'd love to have this program! I'm working on a program that would compliment this
11th Aug 2017, 9:55 AM
Limitless
Limitless - avatar