Important alert: (current site time 7/15/2013 5:32:26 PM EDT)
 

VB icon

^A Paint Program in Visual C++^

Email
Submitted on: 8/18/2002 9:04:27 AM
By: Niloy Mondal 
Level: Beginner
User Rating: By 13 Users
Compatibility: Microsoft Visual C++
Views: 59481
author picture
(About the author)
 
     This shows how to make a simple free-hand drawing program in Visual C++. The code is commented.

 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :^A Paint Program in Visual C++^
//**************************************
#include<windows.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: ^A Paint Program in Visual C++^
// Description:This shows how to make a simple free-hand drawing program in Visual C++. The code is commented.
// By: Niloy Mondal
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4600&lngWId=3//for details.//**************************************

//Programmer:- Niloy Mondal. Email:- niloygk@yahoo.com
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int lastx,lasty,x,y;		//GLOBAL VARIABLES used in drawing.
//The WinMain contains all formality stuff that must be written in almost every Windows Program.
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
 PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Paint") ;//Winddow Class name
HWND hwnd ;
MSG msg ;
WNDCLASSwndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance= hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor= LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
 MessageBox (NULL, TEXT ("This program requires Windows 98!"), 
szAppName, MB_ICONERROR) ;
 return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
 TEXT ("Paint in Visual C++."), // window caption
 WS_OVERLAPPEDWINDOW,// window style
 CW_USEDEFAULT,// initial x position
 CW_USEDEFAULT,// initial y position
 CW_USEDEFAULT,// initial x size
 CW_USEDEFAULT,// initial y size
 NULL,// parent window handle
 NULL,// window menu handle
 hInstance, // program instance handle
 NULL) ;// creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))				//The Message Loop
{
 TranslateMessage (&msg) ;
 DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void line(HDC _hdc,int x1,int y1,int x2,int y2)//This function draws line by the given four coordinates.
{
	MoveToEx(_hdc,x1,y1,NULL);
	LineTo(_hdc,x2,y2);
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECTrect ;
	 	
switch (message)
{
	 case WM_LBUTTONDOWN:					//If Left mouse button is pressed
			lastx=LOWORD(lParam);			//Store the x-coordiante in lastx
			lasty=HIWORD(lParam);			//Store the y-coordinate in lasty
			return 0;
case WM_MOUSEMOVE:						//When mouse is moved on the client area (or form for VB users)
		 hdc = GetDC(hwnd);					//hdc is handle to device context
		 x=LOWORD(lParam);					//Store the current x 
		 y=HIWORD(lParam);					//Store the current y
		 if (wParam & MK_LBUTTON)			//If Left mouse button is down then draw
		 {	
			line(hdc,lastx,lasty,x,y);		//Draw the line frome the last pair of coordiates to current
			lastx=x;						//The current x becomes the lastx for next line to be drawn
			lasty=y;						//The current y becomes the lasty for next line to be drawn
		 }
		 ReleaseDC(hwnd,hdc);
		 return 0;
case WM_PAINT:
 hdc = BeginPaint (hwnd, &ps) ;
 GetClientRect (hwnd, &rect) ;
		 TextOut(hdc,0,0 ,"Programmer :- Niloy Mondal. Email:- niloygk@yahoo.com",53);		 		 		 
		 EndPaint (hwnd, &ps) ;
 return 0 ;
 
case WM_DESTROY:
 PostQuitMessage (0) ;
 return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}


Other 12 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/18/2002 12:12:44 PMTushar Goswami

Nice Code! : )

Hey Niloy! I have a question for the "Keyboard programming tutorial you posted on psc.. Can you "simulate" the pressing of Ctrl+Alt+Delete on a windows os??You wrote the code to detect Ctrl+Alt+Del but can we simulate it??hhmm??:) I also sent a message to you thru email.. but perheps it got misplaced :)

Anyway!! Good code.. PLEASE Help me.. I am giving 4 globes:)
(If this comment was disrespectful, please report it.)

 
8/20/2002 12:53:56 PMDAK

When I tried to compile the code I got an undefined variable error message: RECTrect. Don't know what I'm doing wrong.
(If this comment was disrespectful, please report it.)

 
8/21/2002 8:59:28 AMNiloy Mondal

Put a space between RECT & rect. It should be like this:-
RECT rect;
This should solve any problem you have while compiling the code.
(If this comment was disrespectful, please report it.)

 
10/21/2002 4:27:48 AM

i love visual c++ and change language in windows to farsi
(If this comment was disrespectful, please report it.)

 
10/24/2002 4:53:50 AM

Hey niloy i have a question how to call a bitmap in dialog box using picture box
and make the changes on the bitmap according the value of slider...
(If this comment was disrespectful, please report it.)

 
12/2/2002 9:55:31 AM

i get 12 errors when compiling it using visual c++, errors about the class/struct/union type, and undeclared identifiers
(If this comment was disrespectful, please report it.)

 
12/21/2002 9:38:23 AM

You have to put a space between WNDCLASS and wndclass, like this :
WNDCLASS wndclass ;
(line 32)
(If this comment was disrespectful, please report it.)

 
12/28/2002 10:31:51 AM

Nice Code!
Hello, Niloy. I have a question for you.
You know a place where I can find a dll or source C++ for read a picture in JPG format, bigger than 4.000x3.000 24 bits color, and save it with 1.24x760 24 bits of size, or less than it?. Thanks in advance.
4 globes for you
(If this comment was disrespectful, please report it.)

 
3/18/2003 2:42:55 PM

Do I need to create my own main for this program to run because i got no main()error
Thanks
(If this comment was disrespectful, please report it.)

 
6/23/2003 12:04:25 AM

love this stuff.

(If this comment was disrespectful, please report it.)

 
10/16/2003 8:57:25 PMRichard

Had the rect and a wndclass undeclared error that was able to fix magically with a space bar :) But how is the line feature suppose to be called? I see its suppose to be left mouse-drag? but it just draws by pixel. Oh played with rmouse and it sort of works...Guess it doesn't show a rubber band of the line first? Plus took a couple of extra clicks for line to show up.
(If this comment was disrespectful, please report it.)

 
12/7/2003 1:46:19 PM

Good effort! I have been trying to write a simple animation program in C++ and i think your code could help. Thankyou.
(If this comment was disrespectful, please report it.)

 
4/12/2004 1:04:29 AM

this is a nice code
(If this comment was disrespectful, please report it.)

 
6/22/2004 6:18:48 PMCinny

C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccOUaaaa.o(.text+0x94):test.cpp: undefined reference to `GetStockObject@4'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccOUaaaa.o(.text+0x19d):test.cp p:
undefined reference to `MoveToEx@16'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccOUaaaa.o(.text+0x1b1):test.cpp: undefined reference to `LineTo@12'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccOUaaaa.o(.text+0x33e):test.cpp: undefined reference to `TextOutA@20'


(If this comment was disrespectful, please report it.)

 
3/18/2005 2:20:14 AM

nice code...but im confused if i try to make it more like paint application that have menu bar, change the paintbrush size..and safe my document..what do i have to include and what is the codes to do that?
(If this comment was disrespectful, please report it.)

 
6/23/2005 3:25:53 PM

very nice code, though like people have said you have to put a space between RECTrect;, and WNDCLASSwndclass;,
(If this comment was disrespectful, please report it.)

 
8/25/2005 9:23:07 PMHien

can u share me this code, thank u.
(If this comment was disrespectful, please report it.)

 
9/30/2005 5:39:47 PMCJDude

I got 1 error when I ran the program in Visual C++ 6 Standard Edition
(If this comment was disrespectful, please report it.)

 
1/27/2006 11:51:21 PMVishnu

That was well!!!
(If this comment was disrespectful, please report it.)

 
6/19/2006 11:28:20 AM

Where do I put the code?
I ran it with Visual C++ Express Edition 2005
These are debug:
------ Build started: Project: MySecondApp, Configuration: Debug Win32 ------
Compiling...
stdafx.cpp
Compiling...
AssemblyInfo.cpp
MySecondApp. cpp
c:\documents
and settings\daniel\my documents\visual studio 2005\projects\mysecondapp\mysecondapp\Form1.h(17) : fatal error C1083: Cannot open include file: 'windows.h': No such file or directory
Generating Code...
Build log was saved at "file://c:\Documents and Settings\Daniel\My Documents\Visual Studio 2005\Projects\MySecondApp\MySecondApp\Debug\BuildLog.htm"
MySecondApp - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

(If this comment was disrespectful, please report it.)

 
6/24/2006 9:54:27 PMSefk

Thanks for posting this code. Your code had some few mistakes which it took me about five minutes to fix. It works great now, so you should keep submitting code that you feel free to give out =)

Some things to point out like others have said:

RECTrect; is RECT Rect; with a space
WNDLCASSwndclass is WNDCLASS wndclass; with a space.
There should be some undefined variables where you should declare them globally (lastx,lasty,x,y)

Note: Make sure to make the application in win32 project.
(If this comment was disrespectful, please report it.)

 
3/31/2007 1:58:27 PMJohn

I know it's been a year since anyone has commented on this code, but I'm hoping someone can help me.
I've fixed the two space errors in the code, but when I try to compile, I get this error: fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

When I add that to the beginning of my code, I then get the following 2 errors: warning C4244: 'return' : conversion from 'WPARAM' to 'int', possible loss of data
error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [54]' to 'LPCWSTR'

can someone please tell me how to correct this?

note: i also created the app in win32 project as suggested above.
(If this comment was disrespectful, please report it.)

 
9/9/2009 3:39:24 AMmohmad

machkor
(If this comment was disrespectful, please report it.)

 
11/15/2009 5:17:48 AMquestion

i found this error
`RECTrect' undeclared (first use this function)
rect' undeclared (first use this function)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.