Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

My first question (MFC)

$
0
0

Greetings everyone,

this is my first post in this forum, so nice to meet you all.

Now for my question:

I just start to learn MFC, so I am a complete newbie. Before my question, I have to inform you that I created this project without MFC application wizard, just an empty project. This was instructed by my lecturer. I am able to create the initial main window, but I am stuck there for a long time now. Here is my code:

#include "MainMenuWindow.h" // application class
#include <afxwin.h>

// constructor initializes the window
MainMenu::MainMenu()
{
    // Create Window with Title Bar
    Create( NULL, // default CFrameWnd class
        CString("Main Menu"), // window title - convert char array to CString
        WS_OVERLAPPEDWINDOW, // full-featured window
        CRect( 0, 0, 1366, 730 ) ); // screen coordinates left top right bottom

    // add menu and submenu dynamically
    Option.CreateMenu();
    PopupOption.CreatePopupMenu();
    // Add items to the sub menu and menu
    PopupOption.AppendMenuW(MF_STRING, ID_INSERT_OBJECT, CString("Insert Object"));
    PopupOption.AppendMenuW(MF_STRING, ID_OBJECT_CONTENT, CString("Content"));
    PopupOption.AppendMenuW(MF_STRING, ID_EDIT_OBJECT, CString("Edit"));
    PopupOption.AppendMenuW(MF_STRING, ID_DELETE_OBJECT, CString("Delete object"));
    PopupOption.AppendMenuW(MF_STRING, ID_SAVE_FILE, CString("Save"));
    PopupOption.AppendMenuW(MF_STRING, ID_LOAD_FILE, CString("Load"));
    PopupOption.AppendMenuW(MF_STRING, ID_QUIT_WINDOW, CString("Quit"));
    PopupOption.AppendMenuW(MF_STRING, ID_DISPLAY_OBJECT, CString("T - Display Object"));
    Option.AppendMenuW(MF_STRING | MF_POPUP, (UINT)PopupOption.m_hMenu, CString("Option"));

    SetMenu(&Option);
}

// destructor releases resources
MainMenu::~MainMenu()
{
}

afx_msg void MainMenu::InsertObject()
{
    //MessageBox(CString("This is InsertObject window"),CString("Insert Object"));
}

This is just part of my code (not all my code I must admit, I take reference from other website), without including the mainwindow.h file and mainwindowapp.cpp. With this, I am able to create an initial main window with a menu bar, and that's it.

I try to make sub window appear when I choose an option from the window menu, and only the messagebox that I commented above work fine.

Take the insertobject() above as example, when I choose this option in the menu bar, I am hoping to get a subwindow which let user choose an shape object and select its size, then it will draw the shape on the main window. I imagine the subwindow should have some static text, drop down list and buttons.

I try the cdialog function, but generate an error.

I try putting another window create code inside insertobject() function, got another error:

 // Create Window with Title Bar
    Create( NULL, // default CFrameWnd class
        CString("Insert Object Menu"), // window title - convert char array to CString
        WS_OVERLAPPEDWINDOW, // full-featured window
        CRect( 0, 0, 500, 500) ); // screen coordinates left top right bottom

If you need more information, I am able to draw shape object on main window:

afx_msg void MainMenu::OnPaint()
{
    PAINTSTRUCT ps;
    pDC = BeginPaint(&ps);

    //MainMenu::DrawDiamond();
    //MainMenu::DrawSquare();
    //MainMenu::DrawTriangle();

    EndPaint(&ps);
}

So to summarize this, how do I code my program so that a sub window will appear, let user select an object, then draw that object on the main window?

Furthermore, is there any MFC tutorial without using MFC application wizard? Most of the tutorials I found on the internet include using the wizard, so it's not suitable for me.

Lastly, thank you for anyone who read this long post. Since this is my first post, I hope my question is as clear as possible. And pardon my bad English. Thanks!


Viewing all articles
Browse latest Browse all 15302

Trending Articles