MFC can't find GetDocument() in a Dialog - c++

I'm new to MFC,and I want to get some data in a Dialog,but this doesn't work,
CTestDoc* pDoc=GetDocument();
pDoc->Get(...);
I Google it and find GetDocument() only used in CView.
So I try this and it really works:
CMainFrame *pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd;
CTestView *pView=(CTestView *)pMain->GetActiveView();
m_name=pView->v_name;
But I don't think it's a good solution,so I want to know is there any functions to solve this?Thank you.

You didn't say anything about what the dialog does but it may be desirable to pass this data to the dialog, and not have the dialog access the document data directly. This helps keep your dialog more isolated and more likely to be usable in other programs.
I would look at the command where the dialog is displayed. Is it in the view? If so, then you can pass the document data that is needed by the dialog. If not, then the code you posted may still make sense. Either way, that's where you should locate the desired document information, and then pass it to the dialog.
I would avoid making your dialog directly aware of the document if it's not necessary.

If you want to get data from dialog, first set the data, and then get it.
For example:
CString save;
filePath.GetWindowsText(save);
richBox1.AddString(save); // to display in another box

Related

How can I open an *modal* file dialog with IFileOpenDialog?

I've been trying to implement a file dialog into my C++ application for a while now, and I achieved good success with the code described in this article (It is german, but the code should be understandable):
https://msdn.microsoft.com/de-de/library/windows/desktop/ff485843(v=vs.85).aspx
However, using this code in my window class, which is a CDialogImpl, I just can't find out how to make this file picker modal. If I use this code, I can just spawn multiple file picker.
Of course, I could just keep track of the state by adding a member variable representing the state, but it would still not solve the problem of being able to click around in the main window while the dialog is opened.
Is there any way which would allow me to make this window modal? I've been trying to scan through all available methods, but I couldn't find anything. I didn't find any flags which could be passed in the creation, neither any options which I could set after creation.
Any help is appreciated!
The example you link to is very simple and has no UI other than the file dialog. Your program is more complex, having a window from which the file dialog is invoked.
You need to provide an owner for the file dialog. When you do that the owner is disabled, part of what makes the dialog modal. Failing to pass an owner means that the other windows are not disabled and so still respond to user input.
The example code provides no owner, but since there are no other windows in that program, that is benign. Modality is only meaningful when there are multiple windows.
So, to solve the problem, pass the owner, the handle of your window, to the Show method of the file dialog.
Disabling owner windows is one of the key parts of a modal dialog. You will find that any API for modal dialogs expects you to specify an owner. Get into the habit of expecting to provide that ownwr window, and looking for the means to do so.

Copying text from an MFC CDialog

I'm currently working on code that I inherited. There is a class (I'll refer to it as logWindow) which inherits from CDialog. Overall the logWindow class creates a window and prints out text.
What I need to do is copy the text that is automatically generated in the window.
I know I need some sort of mouse and keyboard listener, but I'm a little lost on how to do this and how to select text.
I also have working code for a different log window written by the same person. That code has a class (I'll refer to it as copyList) which inherits from CListbox. Unfortunately the code isn't well documented or managed, so it is difficult to figure out which functions are related to copying text and which functions are related to other things such as auto scrolling.
I apologize if this is very unspecific, because of what I'm working on I'm limited in how much I can post. I will update the question with as much information as I can.
you can use GetWindowText or CWnd::GetWindowText to get the text from the control that holds the text, but this will copy all the text inside that control, so you will have to do tinker the text if you want some filtering. you said you already have a CListBox example working so you know how to iterate over the items.then you can use this link Clipboard: Using the Windows Clipboard and check how to handle the clipboard.you could also add a simple button "Send to clipboard" that sends the text to the clipboard

using SHAutoComplete with CEdit control

I am developing an MFC application, can i use SHAutoComplete with a CEdit control? Also is there any ready made auto complete controls are available? or i need to use write all the code for creating the list box below the edit control as user types in edit control?
Just pass CEdit's m_hwnd member to SHAutoComplete. I don't think that extension warrant another class. The listbox is created by the AutoComplete object created by SHAutoComplete.
SHAutoComplete helps to autocomplete paths (system or URL).
If this is a combo box and you want to use autocomplete for suggesting string contained in the combo, you have to write a code to handle it.
There are samples you can find. One I found (working):
http://www.ucancode.net/Visual_C_MFC_COM_faq/Visual-C-Auto-completion-ComboBox-CComboBox.htm

MFC how to automatically select all text in CEdit control

I have a CMFCToolBarComboBoxButton on a CMFCToolBar. I want that whenever the CMFCToolBarComboBoxButton gets the focus its entire text will be selected.
What is the most elegant way to implement it?
Thanks a lot!
Adi Barda
Not sure what the most elegant way is, but I guess the most common way to do this is to make a derived class and override OnSetFocus (exact method name not checked), and call SetCurSel() on the contained edit control. WM_FOCUS is only send to the control and there is no notification message for it afaik, so you'll have not many options besides doing something that will make the control handle the event - be it reflect it to somewhere else, or just implement the behavior itself. (I guess theoretically there's all sorts of finicking one can do with intercepting messages, but that's certainly not the most elegant way...)
That should be the default behavior of the standard edit control. If it's not, something else is removing that behavior explicitly. Maybe you can find some flag you can set on the combobox button?
If not: subclass the edit control, handle the WM_GETDLGCODE message. Return a value ORed with DLGC_HASSETSEL. If the control has this flag set, then Windows will automatically select all text on focus.

Extend an external application's menu

I am currently writing a plugin for a third party application.
As the plugin framework does not provide any way to access the UI I am now trying to do this manually via the WinAPI.
More specifically, I want to add a custom menu item for my plugin in the "File" menu.
My first attempt using FindWindow to retrieve the handle of the main window and the using GetMenu was not successful, as GetMenu simply returned NULL.
My next step was to use EnumChildWindows and search for a child having the text "&File" (I really don't like this approach as it makes localization quite terrible). However, I only found out the handle of the menu item, but I need the corresponding HMENU to use AppendMenu then, don't I?
Simply casting does not work and results in an "Invalid menu handle".
Is it actually possible to achieve what I am trying? How?
Thanks for your ideas in advance!
It more than likely just isn't a HMENU. Custom menu implementations are common, the one Window provides is dated and inflexible. Compare to Windows Forms' MenuStrip for example.
Of course, that blows a gaping hole in your approach.