where is the message loop in mfc

Started by
9 comments, last by BriTeg 19 years, 3 months ago
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...
Advertisement
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.
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...
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.
For more on MFC message pumps, check out, Meandering Through the Maze of MFC Message and Command Routing.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
You want to use Hook functions; specifically SetWindowsHookEx with the WH_FOREGROUNDIDLE hook selector.
enum Bool { True, False, FileNotFound };
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]
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

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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++;        }    }}



Brianmiserere nostri Domine miserere nostri
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement