1

I have an application where, on a specific event, I want to open a edit control at a specific position x,y with a specific size width,height. In the opened window, I want the user to enter a string and confirm with the enter-button.

I'm absolutely new to MFC, so I have very little knowledge on how this could be possible. Here is what I have tried so far:

// given: long x, y, width, height;
POINT p;
p.x = x;
p.y = y;
auto* edit = new CEdit();
edit->SetCaretPos(p);

However, I have no clue on how to display the new field, how to set its' size and how to capture the event of user input. Can you help me out with some ideas at least on how I could start?

3
  • 1
    It needs to have a parent windiw and you need to Create the button Commented Nov 18, 2024 at 11:40
  • 2
    You don't need a button control to handle the Enter key being pressed. But you do need a parent window. An Edit control cannot be a top-level window or an owned window. Commented Nov 18, 2024 at 12:15
  • 1
    A better way to capture user input is to use a DialogBox; see https://learn.microsoft.com/en-us/cpp/mfc/reference/cdialog-class?view=msvc-170. Commented Nov 18, 2024 at 14:02

1 Answer 1

1

Go to your Resource View and right-click on the Dialog node. Select the option Add Resource.

Add dialog via Resource View

Note, the Resource View is not visible by default, and needs to be activated from the **View **menu:

Select Resource View from View menu

Now,

  1. drag an edit control onto your dialog and position it.
  2. right-click the dialog and choose Add Class, and give it a suitable name.

Context menu - Add Class

  1. right-click the edit control and choose Add Variable

Select Add Variable from context menu

  1. In your main dialog add a button.
  2. Double-click the button to create a button click handler.
  3. Then do something like:
CMainDialog:MyButton()
{
     COtherDialog dlg(this);
     dlg.DoModal();
}

I have added some screenshots to the answer. Let me know if you need more or need anything clarified. And, you will find many tutorials about how to properly use MFC.

Now, if you want to show this popup window at a specific location on the screen, then extra code will need to be added to the dialog to restore / save the window position to the registry.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.