How to Make a MFC Paint Brush Application

This article will walk you through the steps to write a simple paint brush application using the Microsoft Foundation Class (MFC) library. The program will convert your mouse into a drawing instrument on a window. When the left mouse button is pressed and dragged, a line is drawn. When the button is released, the line stops.

Things You'll Need

  • Microsoft Visual C++ development environment
Show More

Instructions

    • 1

      Launch Microsoft Visual C++. Then click File, New and Projects. Select "Win32 Application," give the project a name and click OK. Check "An empty project" and click "Finish."

    • 2

      Add a source file to the project. Click Project, "Add To Project," New and then "C++ Source File." Name the source file.

    • 3

      Copy and paste the following source code onto the file of Step 2:

      //begin MFC Paint Brush

      #include


      class CWindow :public CFrameWnd

      {

      CPoint mBegAny, mEndAny;

      public:

      CWindow()

      {

      Create(NULL,"Paintbrush Window");

      }

      void OnLButtonDown(UINT flags, CPoint any);

      void OnLButtonUp(UINT flags, CPoint any);

      DECLARE_MESSAGE_MAP()

      };


      BEGIN_MESSAGE_MAP( CWindow, CFrameWnd)

      ON_WM_LBUTTONDOWN() //macro that maps left button click

      ON_WM_LBUTTONUP() //macro that maps left button

      END_MESSAGE_MAP()


      void CWindow::OnLButtonDown(UINT flags, CPoint any)

      {

      CFrameWnd::OnLButtonDown(flags, any);

      mBegAny = any;

      }


      void CWindow::OnLButtonUp(UINT flags, CPoint any)

      {

      CFrameWnd::OnLButtonDown(flags, any);

      mEndAny = any;

      CClientDC dc(this);

      dc.MoveTo(mBegAny);

      dc.LineTo(mEndAny);

      }


      class MyApp :public CWinApp

      {

      CWindow *wnd;

      public:

      BOOL InitInstance()

      {

      wnd = new CWindow();

      m_pMainWnd = wnd;

      m_pMainWnd->ShowWindow(1);

      return 1;

      }

      };


      MyApp theApp;

      //end MFC Paint Brush
    • 4

      Understand what the code means. Class CFrameWnd is used to create a window. The coordinates of the mouse are stored in variables mBegAny and mEndAny of type CPoint. This occurs inside events OnLButtonDown and OnLButtonUp that correspond to a mouse click or release, respectively. Class CWinApp is the MFC equivalent of the main() function. It controls the initialization, running and termination of a Windows Application.

    • 5

      Build the application. To build press F7 or click Build and then "Build file_name.exe" or click on the Build icon. Run the application. Press Ctrl+F5 or click Build then "Execute file_name.exe" or click the Run icon.

Tips & Warnings

  • A class called CMetaFileDC (device context class) will solve the problems listed under Warnings.

  • You can't draw curves with this application.

  • A window resize will erase the lines drawn.

Related Searches:

Resources

Comments

  • deeptijsharma Oct 12, 2008
    i tried the above code but it has 12 errors in all....i resolved them but was unable to resolve 3 of them....m writing these errors here....please see to it n if u could resolve them i ll be very much thankful to u....
  • deeptijsharma Oct 12, 2008
    i tried the above code but it has 12 errors in all....i resolved them but was unable to resolve 3 of them....m writing these errors here....please see to it n if u could resolve them i ll be very much thankful to u....
  • deeptijsharma Oct 12, 2008
    there are 12 errors in all....but wasnt able to resolve these....if u could do them n post the code again...i ll be very much thankful to u...
  • deeptijsharma Oct 12, 2008
    there are 12 errors in all....but wasnt able to resolve these....if u could do them n post the code again...i ll be very much thankful to u...

You May Also Like

Related Ads

Featured