dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Open a blank screen and draw with mouse on it

  1. #1
    Join Date
    Mar 2017
    Posts
    12

    Open a blank screen and draw with mouse on it

    Hello,
    I want to make my drawing program, with C++.
    I am very beginner.
    how can I open a blank screen so that I can draw on it when I move my mouse?

    I use microsoft visual studio 2017

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,348

    Re: Open a blank screen and draw with mouse on it

    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,671

    Re: Open a blank screen and draw with mouse on it

    C++ does not have integral support for graphics/windows programming. This has to be done using additional libraries with C++. For Windows there is the option of using WIN32 API's which comes as standard with VS. This allows programming of windows using a set of c functions which are part of the Windows OS. If you wat to purse this option, then I'd suggest reading 'Programming Windows' by Petzold 5th edition. https://www.amazon.co.uk/Programming...s=books&sr=1-1

    However, most c++ programmers today use 3rd party libraries for graphical programming - of which several are available.

    I would suggest, as you are a beginner, getting the book 'Programming: Principles and Practices Using C++' in which Stroustrup uses the FLTK graphical library to produce graphical c++ programs https://www.amazon.co.uk/Programming...s%2C159&sr=1-3
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.0.4)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,671

    Re: Open a blank screen and draw with mouse on it

    Quote Originally Posted by VictorN View Post
    Note that this uses MFC - which is a Microsoft c++ class library which is a thin OO wrapper around the Win32 API's.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.0.4)

  5. #5
    Join Date
    Mar 2017
    Posts
    12

    Re: Open a blank screen and draw with mouse on it

    Quote Originally Posted by VictorN View Post
    SOrry but itdirects me to github where there are dozens of diles which I dont know nothing to do

    All I ask is what code to write to i can draw with mouse on a blank screen

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,671

    Re: Open a blank screen and draw with mouse on it

    To do that, you have to either understand the WIN32 API's or understand a c++ add-on framework. As I mentioned in my post #3, c++ does NOT have in-built support for graphics. First you learn c++ using the console (which is supported as part of c++), then you come on to using graphics. That is why I mentioned the Stroustrup book in post #3. This teaches you c++ first using the console and then introduces graphics programming using the FLTK graphics library.

    C++ is not Java or Vb.net etc with standard graphics libraries that can be used early on with learning those languages. With C++ you have to crawl before you can walk, and walk before you can run.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.0.4)

  7. #7
    Join Date
    Mar 2017
    Posts
    12

    Re: Open a blank screen and draw with mouse on it

    Thanks for answer but no I will not buy any books when there are trillion gigabytes of information and tutorials about C++ on the web and I want to learn by hands on programming myself. Is buying that book a requirement for asking further questions here?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,671

    Re: Open a blank screen and draw with mouse on it

    The code below is the basis of what you want:

    Code:
    #include <Windows.h>
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
    
    int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR line, int show)
    {
    	WNDCLASSEX wc{};
    
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.hInstance = hinst;
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor = LoadCursor(NULL, IDI_APPLICATION);
    	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
    	wc.lpszClassName = TEXT("tmouse");
    	wc.lpfnWndProc = WndProc;
    	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
    	RegisterClassEx(&wc);
    	auto hwnd = CreateWindowEx(NULL, TEXT("tmouse"), TEXT("Trace the mouse"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 790, 450, NULL, NULL, hinst, NULL);
    
    	ShowWindow(hwnd, show);
    	UpdateWindow(hwnd);
    
    	MSG msg;
    
    	while (GetMessage(&msg, NULL, 0, 0)) {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	static WORD oldx{ 9999 }, oldy{ 9999 }, xpos, ypos;
    	static const HDC hdc{ GetDC(hwnd) };
    
    	switch (msg)
    	{
    		case WM_MOUSEMOVE:
    			xpos = LOWORD(lParam);
    			ypos = HIWORD(lParam);
    
    			if (xpos != oldx || ypos != oldy) {
    				if (oldx != 9999) {
    					MoveToEx(hdc, oldx, oldy, NULL);
    					LineTo(hdc, xpos, ypos);
    				}
    				oldx = xpos;
    				oldy = ypos;
    			}
    			break;
    
    		case WM_DESTROY:
    			ReleaseDC(hwnd, hdc);
    			PostQuitMessage(0);
    			break;
    
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    			break;
    	}
    
    	return 0;
    }
    You need to compile it as a windows program, and not as a console. It has some deficiencies (try minimising and the restoring the window), but it gives an idea.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.0.4)

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,671

    Re: Open a blank screen and draw with mouse on it

    Quote Originally Posted by Ketanco View Post
    Thanks for answer but no I will not buy any books when there are trillion gigabytes of information and tutorials about C++ on the web and I want to learn by hands on programming myself. Is buying that book a requirement for asking further questions here?
    No - but it's recommended that you do get an up-to-date book on c++. For a web resource on learning c++, have a look at https://www.learncpp.com/ - but as this is generic c++ it doesn't cover windows programming.

    As you state, there is a lot of info about c++ on the web - some of it good, some of it bad, some out of date. For learning C++ from scratch I do recommend that you get an up-to-date book on c++. For a web resource on learning c++, have a look at https://www.learncpp.com/ - but as this is generic c++ it doesn't cover windows programming.

    We will always try to answer any questions asked here - I've just posted some simple code for your original question.

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.0.4)

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,525

    Re: Open a blank screen and draw with mouse on it

    Quote Originally Posted by Ketanco View Post
    SOrry but itdirects me to github where there are dozens of diles which I dont know nothing to do

    All I ask is what code to write to i can draw with mouse on a blank screen
    Perhaps it's not as easy as you think. There aren't "diles" in the github link, those are the source code files for a sample application that does what you want it to do. If you think drawing in C++ is something you can learn in 10 minutes with a few lines of code, your expectations are way off. It's a very powerful, but fairly low level language that requires not only an understanding of the language, but the operating system and its interfaces.

  11. #11
    Join Date
    Mar 2017
    Posts
    12

    Re: Open a blank screen and draw with mouse on it

    great it worked....

    Now I will try to understand this

    Thanks a lot

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,974

    Re: Open a blank screen and draw with mouse on it

    Quote Originally Posted by 2kaud View Post
    Note that this uses MFC - which is a Microsoft c++ class library which is a thin OO wrapper around the Win32 API's.
    Exactly. In this day and age, why would anyone choose to code the Win API without using an object oriented wrapper?

  13. #13
    Join Date
    Mar 2017
    Posts
    12

    Re: Open a blank screen and draw with mouse on it

    Quote Originally Posted by Arjay View Post
    Exactly. In this day and age, why would anyone choose to code the Win API without using an object oriented wrapper?
    sorry, I didnt understand. I am just beginning C++. i dont understand this sentence. all I have is windows 10 on my laptop and i saw that i had ms visual studio installed so i started writing C++ like that.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,671

    Re: Open a blank screen and draw with mouse on it

    Nobody these days would start out to write a windows program using WIN32 API. Back in the 1990's this was all we had so we had to to get to grips with it. Then MFC came along which was an OO wrapper around WIN32 and made life easier. Then other windows frameworks came along. One of the most popular today is QT (which is x-platform). Today anyone writing a windows application uses one of these modern-day frameworks. There's probably just some of us 'old lags' who went through the pain of WIN32 API windows programming who still 'bear the scars' - and programmers who want to understand for interest the underlying os stuff concerning windows.

    To re-iterate what has been said previously - you first learn c++ programming (especially classes) and then you learn a windows programming framework. Documentation for the frameworks (books or the internet) will assume c++ knowledge. Which one often depends upon what you are doing

    Eg If you want to create new UWP apps for Windows 10 with c++ you should use c++/winrt - which is Microsoft's new framework for windows c++. See https://docs.microsoft.com/en-us/win...nd-winrt-apis/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.0.4)

  15. #15
    Join Date
    Mar 2017
    Posts
    12

    Re: Open a blank screen and draw with mouse on it

    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 CAD programs. Drawing straight or curved lines and dimensions and angles are enough, I am not looking to make cartoons or game graphics 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... I want to learn by doing it. I can not learn by studying only.
    Last edited by Ketanco; May 12th, 2019 at 05:06 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)