Link to home
Start Free TrialLog in
Avatar of KG1973
KG1973

asked on

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.
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KG1973
KG1973

ASKER

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 ?

Well, what code would you like to add?
Avatar of KG1973

ASKER

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
Um that code should go to where the other methods of 'CPainterView' view are - what errors are you getting?
Avatar of KG1973

ASKER

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.
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)
Avatar of KG1973

ASKER

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 ?
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.
Avatar of KG1973

ASKER

Andy,
I already did what you told me. Unfortunately it doesn't work. I tried couple of time already but
no progress at all.
I don't have VS 2008 - sorry I can't help further.
Avatar of KG1973

ASKER

AndyAinscow,
it's ok. You already helping me. I really appreciate. I will try myself.
cheers
Avatar of KG1973

ASKER

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.
Avatar of KG1973

ASKER

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.