|
As the title says I am trying to host a .net modeless form inside an MFC dialog. I read the article Using WinForms controls in an MFC dialog[^] but I do not want to do it this was because I do not wish to compile the project that contains the MFC dialog in /clr.
So I tried a different path.
I created a /clr dll that exports a very simple function
HWND getDotNetWindowHandler()
{
HostedForm ^frm = gcnew HostedForm();
return static_cast<HWND>(frm->Handle.ToPointer());
}
The hosted form is a .net form without borders. So my dialog takes the handler(by calling the dll, no need to show the implementation here) and places the form in a STATIC control window.
m_pDotNetWnd = CWnd::FromHandle(getDotNetWindowHandler());
CWnd* pWndContainer = GetDlgItem(IDC_DOTNET_WND);
if(m_pDotNetWnd != NULL && pWndContainer != NULL)
{
m_pDotNetWnd->SetParent(pWndContainer);
m_pDotNetWnd->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_ASYNCWINDOWPOS | SWP_FRAMECHANGED);
m_pDotNetWnd->ShowWindow(SW_SHOW);
HWND hWndChild = m_pDotNetWnd->GetSafeHwnd();
// reset the style
DWORD style = GetWindowLong(hWndChild, GWL_STYLE);
style &= ~(WS_POPUP|WS_CAPTION); //reset the "caption" and "popup" bits
style |= WS_CHILD; //set the "child" bit
SetWindowLongPtr(hWndChild, GWL_STYLE, style); //set the new style
}
Everything seems to work just fine but after a while I am having a deadlock. The window freezes.
After a lot of research on the internet it seems to be a problem with the SendMessage when the child window is created from a different thread. I couldn't find any workarround though. Is there any? Can someone give me a clue?
|
|
|
|
|
Hello Everybody,
I am new to MFC. I used the application wizard to create a SDI application, I was able to replace the existing menu and toolbar.
In this, I added a dialog with some controls. Now, what I need is, whatever the details I got from the dialog controls, I need to display the document view.
On CView::OnDraw,I added the code like this.
<code>
CSDITestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
pDC->TextOut(20, 20, L"test sdi", 10);
</code>
This text is writing only once in the beginning. On closing the dialog, this function is not called, how can I do that?
I tried with UpdateAllViews and OnUpdate also.
Thanks in advance.
A. Gopinath.
|
|
|
|
|
On CView::OnDraw,I added the code like this
Really?
|
|
|
|
|
Ok, I understood what you are trying to say.
I added this line only in the existing function.
pDC->TextOut(20, 20, L"test sdi", 10);
Also, I would request you to read the "How to answer the question", especially, 3rd point.
|
|
|
|
|
You need to show some more of your code, as it is not obvious where these different events occur. Pleas also use <pre> tags around your codeblocks so they are readable, like this:
CSDITestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
pDC->TextOut(20, 20, L"test sdi", 10);
|
|
|
|
|
Thanks for you reply.
Actually, I created one MFC Application using the Template, and its SDI. So, it creates list of files, Doc, View, etc. For View, i select the base class as CView (in the wizard itself).
I removed the default created menu and included my menu list.
Now, i added a simple dialog, and for that, I created a class also derived from CDialog. Following are the .h and .cpp file contents
.h file
class CSelectOptionsDlg : public CDialog
{
public:
CSelectOptionsDlg(CWnd* pParent = NULL);
virtual BOOL OnInitDialog();
public:
enum {IDD = IDD_SELECTOPTIONSDLG};
public:
void OnOkClicked();
DECLARE_MESSAGE_MAP()
};
.cpp
CSelectOptionsDlg::CSelectOptionsDlg(CWnd* pParent)
: CDialog(CSelectOptionsDlg::IDD, pParent)
{
}
BEGIN_MESSAGE_MAP(CSelectOptionsDlg, CDialog)
ON_COMMAND(IDOK, OnOkClicked)
END_MESSAGE_MAP()
void CSelectOptionsDlg::OnOkClicked()
{
AfxMessageBox("Ok clicked");
EndDialog(1);
}
BOOL CSelectOptionsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
return TRUE;
}
As I mentioned earlier, if i add this line in OnDraw, its drawing on the first time only,
pDC->TextOut(20, 20, L"test sdi", 10);
Also, in MainFrm.cpp file,
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_COMMAND(ID_START, OnStartPlan )
ON_COMMAND(ID_STOP, OnStopPlan )
ON_COMMAND(ID_SELECTOPTIONS, OnSelectOption )
ON_COMMAND(ID_EXIT, OnExit )
END_MESSAGE_MAP()
void CMainFrame::OnSelectOption()
{
CSelectOptionsDlg cSODlg;
cSODlg.DoModal();
}
doing like this.
OnDraw is not calling everytime. What I need is, whenever i click ok and close the dialog, OnDraw need to be called. How can i do that? Any help.
Thanks.
|
|
|
|
|
As far as I recall from my MFC days, a Window's OnDraw() procedure will get called when the dialog closes, so this should happen automatically. Unfortunately that's the part of the code that you did not show us. You may like to add some breakpoints and use your debugger to see the flow of code when the dialog closes.
|
|
|
|
|
Hello Richard,
I debug the whole code by putting breakpoints in all the functions, after closing the dialog, its not going anywhere in the code, remains on the application itself.
I am attaching the code herewith. Please download and check.
http://www.4shared.com/zip/eDwcyehu/SDITest.html[^]
Thanks.
|
|
|
|
|
Sorry, I cannot download that. you need to check that your OnDraw() function is being activated properly. If necessary you may need to add a call to UploadAllViews() after the dialog closes.
|
|
|
|
|
To force the window to redraw (any window, not only view) you need to call CWnd::Invalidate()[^] method. Calling it will trigger all the drawing routines
|
|
|
|
|
Hi,
I created 1 worker thread AFxBeginThread and 4 UI Thread via new CWinThread
However when I look at the thread display under debug Visual Studio display all threads as Worker Threads
Thanks
|
|
|
|
|
And do you have a question, related to C++?
|
|
|
|
|
I thought about posting this in a different forum
But the concept of UI threads and worker threads
is MFC
|
|
|
|
|
I never quite placed much attention as to how threads are labeled in the debug display but... does it really matter? A UI thread is really just a label for a thread, it differs only in the extra functionality that comes from the framework around it, so does the label really make a difference? If it works fine why are you worried about the label the debugger is using?
|
|
|
|
|
ForNow wrote: I created 1 worker thread AFxBeginThread and 4 UI Thread via new CWinThread Maybe I'm misunderstanding your intent here, but worker threads and UI threads are both created using AfxBeginThread() . The difference is whether the thread has a message pump or not.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Sorry you are right the different UI
Has CWinThread::Run. While workers have a thread proc
|
|
|
|
|
........
modified 26-Feb-13 6:47am.
|
|
|
|
|
Why are you using CString s for your data buffers, and why are you trying to convert the file content with wcstombs() ? Use the BYTE (unsigned char ) type for your data buffer, and read the files in binary mode.
|
|
|
|
|
|
I already told you how to do it. Do not use CString it will not work. And do not convert the file contents, it will leave you with corrupt data. A file is just a stream of bytes and in moving it from one location to another you should process it as such.
|
|
|
|
|
Message Closed
modified 15-Feb-13 1:10am.
|
|
|
|
|
If there is still a problem in this code then you need to run it through your debugger and isolate the point of failure. We cannot guess what is happening without more information.
|
|
|
|
|
|
|
Hi,
I'm hitting a snag as I try to read back an array which has been written to a text file. Here's the relevant part of the code:
if((fp=fopen("myfile","r"))==NULL)
{
printf("Unable to open file.\n");
exit(1);
}
for(j=0;j<6;j++)
{
for(l=0;l<7;l++)
{
k=fgetc(fp);
if(k==",") break;
str2[j][l]=k;
}
}
fclose(fp);
for(j=0;j<6;j++)
{
for (l=0;l<7;l++)
{
printf("%c", str2[j][l]);
}
}
This is generating an error:
[Warning] comparison between pointer and integer.
Though it runs, it is revealing that the break code which checks for a comma is not functioning as the compiler warns. Why is it claiming this is a pointer? Why is it saying anything about an integer when I've deliberately cast k as a character?
Thanks for any input.
------
Update: The problem was solved by: (a) changing the double quotes to single quotes (apostrophes) and (b) switching
if(k==",") break; below the assignment. In order to match the file derived string with one defined in code I also had to modify it:
str2[j][strlen(str2[j]-1]='\0'; as there is something foreign added in the file transfer process.
Thanks, and if there is a mark as solved button I couldn't find it
modified 14-Feb-13 16:08pm.
|
|
|
|
|