• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1259
  • Last Modified:

Create c++ program using Visual Studio 2005

I want to develop simple program in C++ using MFC.
I created a project for MFC Application using default setup from Visual Studio 2005.

MFC Application, Single window and so on. Then it create all sort of files cpp and header
files.

When I run it (without making any changes), it actually display a windows similar to text
document editor.

Now I want to change it so that it can do the followings:

1. Add a menu called Draw (line and circle)
2. Add a menu called Change Line (linetype, color and sizes)
3. Functions/method to draw line
4. Functions/method to draw circle
5. Functions/method to change linetype, color and sizes
6. Last - able to save & open the drawing file.

So please helpme to develop this small program using visual studio 2005, step-by-step.
Thanks in advance.
0
KG1973
Asked:
KG1973
  • 9
  • 4
  • 3
2 Solutions
 
jkrCommented:
Take a look at http://www.codeproject.com/KB/system/painter_program.aspx ("Painter Program") which pretty much covers this issue and can serve you as a starting point. This article comes with full source code.
0
 
AndyAinscowCommented:
In the MFC samples there is one called scribble - used as a step by step tutorial for MFC.  
Sounds like you just described its functionality.
0
 
KG1973Author Commented:
hi jkr,

I have gone through the link that you gave me "PainterProgram". It look ok to me, but  im bit confuse where should i pu the code in, i mean which file ?

For the 1st one its ok, coz it mention PainterView.h as follow :
//=======================================================
class CPainterView : public CView
{
protected:
    CPoint Anchor;
    CPoint DrawTo;
    CPoint OldPoint;
    BOOL bDrawFlag;
    BOOL bLineFlag;
    BOOL bRectangleFlag;
    BOOL bEllipseFlag;
    BOOL bFillFlag;
    void MakeAllFlagsFalse();
};

2nd one is to initilise.
//=================
void CPainterView::MakeAllFlagsFalse()
{
    bDrawFlag = FALSE;
    bLineFlag = FALSE;
    bRectangleFlag = FALSE;
    bEllipseFlag = FALSE;
    bFillFlag = FALSE;
}

3rd is for constructor
//======================
CPainterView::CPainterView()
{
    // TODO: add construction code here
    MakeAllFlagsFalse();
}

So where should i put the 2nd and 3rd code?
I don't want to go further until i m sure where it should be.

thanks.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
KG1973Author Commented:
CPainterView::CPainterView()
: MakeAllFlagsFalse(0)
{
      // TODO: add construction code here

}

Also I found above code in PainterView.cpp file. I think the code are automatically generated.

Is this same as the 3rd one that I mentioned before ?
What is the different between both ?

0
 
jkrCommented:
Well, what code would you like to add?
0
 
KG1973Author Commented:
I like to add code no 2 and 3. After this, i want to continue with the following :

I want to try drawing freehand first. This has to be executed when I press Freehand icon (after creating the icon at the toolbar).

I am using Visual Studio 2008 now. I doubt the instruction/tutorial stated in
( http://www.codeproject.com/KB/system/painter_program.aspx )
done using VS2008, it has to be older version.

>>The interface between the flags and the drawing tools
>>Open the class wizard, (CTRL+w), in the class name choose CPaintView, in the object ID's choose >>ID_TOOLS_DRAWFREEHAND, in the messages, choose command, and click add function button, then >>click edit code. The Class Wizard will add the OnToolsDrawfreehand() function.

>>Add the following code to OnToolsDrawfreehand():

 void CPainterView::OnToolsDrawfreehand()
{
    MakeAllFlagsFalse();
    bDrawFlag = TRUE;    
}

For the time being, i am not gonna do other (drawing line, ellipse, rect, fill). Just concentrate on freehand drawing first. If this work, ie using toolbar button, then I will proceed for other drawing method.

Thanks
0
 
jkrCommented:
Um that code should go to where the other methods of 'CPainterView' view are - what errors are you getting?
0
 
KG1973Author Commented:
I am not getting any error yet because i want to try small program first rather than copy & paste stuffs that i don't know where it should be.

This is because I cannot compile the first half to test because it seems that I have to completely copy all the code first. Then I will get definitely get more errors.

Is there anyone who actually done this before ? Can I have the project file ?
or is there any other way ?

Thanks.
0
 
AndyAinscowCommented:
Have you checked out the scribble sample ?
(Unless microsoft have pulled it - it is meant as a step by step MFC drawing app tutorial, expanding the functionality at each stage)
0
 
KG1973Author Commented:
AndyAinscow,
I already download it and try to build it.
But i got this message
"1>------ Skipped Build: Project: Scribble ------1>
========== Build: 0 succeeded or up-to-date, 0 failed, 1 skipped ==========
so i cannot continue ....

I already follow the instruction :

>Building and Running the Sample
>To build and run the SCRIBBLE sample
>Open the solution Scribble.sln.

>On the Build menu, click Build.
>On the Debug menu, click Start Without Debugging.
The sample file is for VS2005 not 2008.

so what should i do ?
0
 
AndyAinscowCommented:
Sounds like VS 2008 isn't converting the project format (I don't have VS 2008 so I can't test it).

Give this a quick try.
Create a new project, call it scribble. Close the project in the IDE.
From the scribble source files you have copy all the .cpp, .h, .rc files and paste into this new projects folder (overwrite with yes).
Copy the contents of the res folder from the scribble example to this new project and again overwrite.
Now open the new project again.  Add all the .cpp/.h files into the project - solution view, add existing items.

Hopefully it will now compile.
0
 
KG1973Author Commented:
Andy,
I already did what you told me. Unfortunately it doesn't work. I tried couple of time already but
no progress at all.
0
 
AndyAinscowCommented:
I don't have VS 2008 - sorry I can't help further.
0
 
KG1973Author Commented:
AndyAinscow,
it's ok. You already helping me. I really appreciate. I will try myself.
cheers
0
 
KG1973Author Commented:
Hi everyone,

I manage to run the program from this link ( the one  suggested by jkr ) http://www.codeproject.com/KB/system/painter_program.aspx ).

Drawing freehand is ok. But with the line, rectangle and ellipse, there are still logical problem.

For example to draw multiple lines,
It stsrt from top left corner and to anywhere when we click (press left button down)
and if we continue clicking to any other place in the window, starting point = previous endpoint
and so on. It continue drawing line.

Similarly with rectangle and ellipse.

Here the code for drawing freehand, line, rectangle and ellipse :

void CPaintView::OnMouseMove(UINT nFlags, CPoint point)
{
      // TODO: Add your message handler code here and/or call default
      int nOldMode;

      CClientDC* pDC = new CClientDC(this);
      
if((nFlags && MK_LBUTTON) && bDrawFlag)
{
        pDC->MoveTo(Anchor.x, Anchor.y);
        pDC->LineTo(point.x, point.y);
        Anchor.x = point.x;
        Anchor.y = point.y;
    }  

    if((nFlags && MK_LBUTTON) && bLineFlag)
    {
       
        nOldMode = pDC->GetROP2();
        pDC->SetROP2(R2_NOT);

        pDC->MoveTo(Anchor.x, Anchor.y);
        pDC->LineTo(OldPoint.x, OldPoint.y);
        pDC->MoveTo(Anchor.x, Anchor.y);
        pDC->LineTo(point.x, point.y);

        OldPoint.x = point.x;
        OldPoint.y = point.y;
        pDC->SetROP2(nOldMode);
    }
   
    if((nFlags && MK_LBUTTON) && bRectangleFlag)
    {
        nOldMode = pDC->GetROP2();
        pDC->SetROP2(R2_NOT);
        pDC->SelectStockObject(NULL_BRUSH);
        pDC->Rectangle(OldPoint.x, OldPoint.y, Anchor.x, Anchor.y);
        pDC->Rectangle(Anchor.x, Anchor.y, point.x, point.y);
        OldPoint.x = point.x;
        OldPoint.y = point.y;
        pDC->SetROP2(nOldMode);
    }
   
    if((nFlags && MK_LBUTTON) && bEllipseFlag)
      {
        nOldMode = pDC->GetROP2();
        pDC->SetROP2(R2_NOT);
        pDC->SelectStockObject(NULL_BRUSH);
        pDC->Ellipse(OldPoint.x, OldPoint.y, Anchor.x, Anchor.y);
        pDC->Ellipse(Anchor.x, Anchor.y, point.x, point.y);
        OldPoint.x = point.x;
        OldPoint.y = point.y;
        pDC->SetROP2(nOldMode);
    }

      delete pDC;
      CView::OnMouseMove(nFlags, point);
}

==========================================================
So helpme, how to change the code so that the start and end point are the one that I just clicked, NOT taken from previous point.

Thanks in advance.
0
 
KG1973Author Commented:
Ok I managed to fix it again.
But last one, does anyone know how to save my drawing to a file ?
The progrem do not implement CObject, it use metafile instead.
So I don't know really how to fix this one.

Please I really need HELP..... guys, I am really dying to fix this one.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now