Sign in to follow this  

where is the message loop in mfc

This topic is 4730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i got a refresh function i want to be run in the time the app doesnt handle any messages. the problem is that its a simple dialog based mfc app and i have no idea where to the real time message loop or the equavelent is. where do i stick my function call? i prosume i need to over-ride a member func of CWinApp but i have no idea which one...

Share this post


Link to post
Share on other sites
Well, CWinApp::OnIdle() seems to match what you want, but I think that would depend on what you are "refreshing".

Check out MSDN for the details of OnIdle.

-Halo7

Edit: BTW, here is the message loop.

Share this post


Link to post
Share on other sites
iam just refreshing some static text objects, onidel seems to be what i want. thnk you very much halo7.

i got another question now. want to get the caption of the dialog box on start up to name of the computer. i use GetComputerName in the constructor for the my dialog class. however when i try to use this->SetWindowText(Buffer); or GetDlgItem(IDD_DIALOG)->SetWindowText(Buffer); i get no compiler errors but strage assertion error when in winocc.cpp (not part of my app at all) when i run the app. WTF is going on?! i know i prob have to set the caption in cwinapp::Create but i dont want to override it coz by the looks of the parameters it looks quite important. for the time being i am putting the caption setting into cwinapp::onidel, but thats not desirable as at some point i might want to change the caption dynamicly again, and what happends then?!

any ideas...

Share this post


Link to post
Share on other sites
I don't know much about MFC, but shouldn't that code be called in response to the WM_CREATE message, rather than in the constructor? If you call SetWindowText(), you are making use of the window handle indirectly. Until WM_CREATE has been handled, there is no guarantee that the window handle exists/is valid. Like I said, I don't use MFC, but I would guess that you need to move the code from the constructor of the class to the OnCreate() function or to the WM_CREATE portion of the message handler.

Share this post


Link to post
Share on other sites
and i thought MFC would be complicated.

i thought i could just make a member function which could get the dlg object and then pass a pointer of it to the overriden onidle through a member variable of app class and there i would just go dlg->refresh() (dlg is a member variable of app class). so in the code i have something like:
<code>
class CApp : public CWinApp
{
public:
void GetDlg(CSysInfoDlg* dlg);
virtual BOOL OnIdle(LONG lCount);
CApp();

...
AppWizard+Class wizard crap
...

private:
CSysInfoDlg* Dialog;
};
//..........................................
void CSysInfoApp::GetDlg(CSysInfoDlg* dlg)
{
Dialog = dlg;
}

BOOL CSysInfoApp::OnIdle(LONG lCount)
{
Dialog->Refresh();
return TRUE;
}

//...........................................

BOOL CApp::InitInstance()
{
...
CDlg dlg;
theApp.GetDlg(&dlg);
...
}
</code>

which suprsisingly didnt work. so i inserted MessageBox(0, "idel", "app", MB_OK); into OnIdle and the message box never came up. which mean that my dialog app is never idel. wtf is going? iam thinking of dropping my attempts to learn mfc and stick to win32 api. plz help.

PS
thnx for the linkies to articles i havent read them all yet so pardon me if what iam asking here is something to do with the message maps idea...

[Edited by - GregLoutsenko on December 31, 2004 8:35:16 AM]

Share this post


Link to post
Share on other sites
AFAIK dialog based MFC apps don't call OnIdle, as that's part of the message loop inside CWinApp::Run. The dialog based apps are basically done inside CYourMFCApp::InitInstance (the call to DoModal).

This thread in Flipcode shows how to make the OnIdle get called:

Here

Share this post


Link to post
Share on other sites
If your app is derived from CWinApp, simply overload the CWinApp::Run function. Here's mine from a previous MFC project:


int CMyApp::Run()
{
MSG msg;
while (1)
{
// Process All Messages
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
{
if (GetMessage(&msg, NULL, 0, 0))
{
if (!PreTranslateMessage(&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
return TRUE;
}

// render
if(drawWnd && ready)
{
drawWnd->DoFrame();
framecounter++;
}
}
}



Share this post


Link to post
Share on other sites
Quote:
Original post by BriTeg
If your app is derived from CWinApp, simply overload the CWinApp::Run function.


In a dialog based MFC app Run is never called. The dialog is opened modally in InitInstance, which returns FALSE to stop the app.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endurion
In a dialog based MFC app Run is never called. The dialog is opened modally in InitInstance, which returns FALSE to stop the app.


Looking at my code, you are right. However, I also overloaded the InitInstance function, and only opened the modal dialog and returned FALSE under certain conditions. E.g. this was for a screen saver, where the app was initially created as a dialog based MFC app (for the main configuration dialog) but I overrode the InitInstance function so it could also start in "run" (window) mode and not just in "configure" (dialog) mode.

Sorry for the confusion. :)

Brian

Share this post


Link to post
Share on other sites

This topic is 4730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this