Archived

This topic is now archived and is closed to further replies.

MFC Question

This topic is 5150 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'm currently learning MFC.. and there is something I don't understand.. My program doesn't seem to have a 'frame' section where I could put code that would be executed every frame... ( I'm talking about frames, like in 'frames per second', not the 'window frame' ) My program needs to have a loop somewhere, where I could put some code, is there a way to create a different kind of window? ( I'm using dialog window ) I think that because my application is modal, ( it uses the fonction DoModal() ), the program execution just stops after DoModal() is called, and will only return with IDOK or IDCANCEL or maybe IDCLOSE, but i'm not sure about the last one... So this is not what I want, and it has been hard to try googling informations about what I want to do, I can't find the appropriate information Thanks for your help edit: Oh, btw, my program would only need a loop somewhere because it needs to verify if the user has pressed some keys.. I'm using Hot Keys "buttont".. But I haven't seen any built in message to handle the Hot keys.. If someone know a way to handle those hot keys, it would be just perfect Thx again [edited by - Hedos on November 10, 2003 9:33:11 AM]

Share this post


Link to post
Share on other sites
MFC follows an event driven program model. This means code gets run only when an event triggers the code to run. In MFC I think you will have to create a Timer event that will fire as often as possible to create your game loop. I''m no MFC guru, there might be another way, but that is one solution. The other solution is use the pure Win32 API which exposes the message loop.

Share this post


Link to post
Share on other sites
If you are going to use MFC, then you have to stick with the event based model. To regularly update your display, you need to do what CodeMunkie suggested. That is, create a timer, and whenever your timer fires, invalidate the area of the window you want updated. The other alternative (which accomplishes the same thing), is to create a separate thread which invalidates the window. You may get slightly better perfomance this way (in regards to more regular updates) if you boost the thread priority.

As for key presses, an easy way to do this would be to write a handler for the WM_CHAR message. This message is sent to the window which has the keyboard focus whenever a user hits a key which translates into a non system character. In your handler, just check and see if it corresponds to the hot-key you are looking for. If it does, do you what you need to to. If it doesn''t, let the base class message handler handle the WM_CHAR msg.

Hope this helps a bit.

Mike

Share this post


Link to post
Share on other sites
Ok, I think I''m going to stick with the event based model

But, for my hot keys.. I have to detect if a hotkey is pressed even if my application window don''t have the focus :S

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
check this out
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_core_idle_loop_processing.asp

and using timer for real time rendering is slower than using idle time processing.

Share this post


Link to post
Share on other sites
Btw, there is always a problem when I send my program to a friend who doesn''t have VC++ installed...

"This application has failed to start because MFC42D.DLL was not found. Re-installing the application may fix this problem"

Is there a way to fix this problem?

Share this post


Link to post
Share on other sites
Sure. Go to Project...Settings and look under the General tab. There is a drop down box with three options:
Not Using MFC
Using MFC in a Static Library
Using MFC in a Shared DLL

I''ll give you three guesses which option to choose

Share this post


Link to post
Share on other sites
Ugh.. I would opt for the second option... but.. I only have one choice: "Using MFC in a shared DLL"

What's wrong ?

[edited by - Hedos on November 10, 2003 11:42:50 AM]

Share this post


Link to post
Share on other sites
I believe the MFC DLLs are distributed with the OS (unless I''m mistaken). However, I think it is only the "retail" or "release" DLLs that are there, and not the debug versions. Try recompiling your code by under the release version and send it to your friend (Build menu option -> Set Active Configuration). If that doesn''t work, then you will need to statically link the library.

Share this post


Link to post
Share on other sites
What version of MSVC++ are you using? I know the option is there in the Professional and Enterprise editions. I guess if all else fails you can just redistribute the DLL.

[edited by - CodeMunkie on November 10, 2003 11:50:16 AM]

Share this post


Link to post
Share on other sites
In regards to your hotkey question...

A better solution than the one I mentioned ealier would simply be to use the "RegisterHotKey" windows function. You basically pass the window you want to receive notification as a parameter. Then, whenever your hotkey is pressed, the window you specified will receive a WM_HOTKEY message. Then, in your app, just handle this message.

Now with VC++ 6.0, this message is not among the list of available windows messages when you right click on your class in the class view. An easy work around is to override the virtual function WindProc (should be listed if you right click in the class view and select Add Virtual Function). In this function, just check and see if the message is WM_HOTKEY. If it is, do your processing, otherwise, pass the message on to the base class method.

Don''t know why I didn''t write this earlier, must be lack of sleep.

Mike

Share this post


Link to post
Share on other sites
CodeMunkie: I''m using MVC++ 6.0 professionnal:

MJB: Thanks, it seems to be on the correct way.. But it doesn''t work perfectly yet..

Here is my WindowProc code:

LRESULT CFirstTestDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class

if(message==WM_HOTKEY)
{
if(wParam==''W'')
{
AfxMessageBox("BOU!!!");
}

return 0;
}
else
return CDialog::WindowProc(message, wParam, lParam);
}


That doesn''t work... I guess wParam doesn''t hold the value of the key pressed
So, how can I check wich key has been pressed?

Share this post


Link to post
Share on other sites
Here's some of the documentation for the WM_HOTKEY message


WM_HOTKEY
idHotKey = (int) wParam; // identifier of hot key
fuModifiers = (UINT) LOWORD(lParam); // key-modifier flags
uVirtKey = (UINT) HIWORD(lParam); // virtual-key code


So basically if you are just looking at the wParam, this value is the ID value you set. Since this ID value has to be unique for every hotkey, you can just check this against the value you set. Otherwise, check the virtual key code.

There is more information in the SDK if you look up WM_HOTKEY or RegisterHotKey. If you don't have all the help files on the HD, you can get them online(http://msdn.microsoft.com/).

Mike

[edited by - MJB on November 10, 2003 12:20:45 PM]

Share this post


Link to post
Share on other sites
First I should probably make sure you registered your hotkey using RegisterHotKey(), though you probably wouldn''t be getting a WM_HOTKEY if you didn''t, but it''s worth asking.

That being asked, you need to use the hotkey ID, not the character value, to check against. That should be the ID that you put in when you called RegisterHotKey. You might want to look at the RegisterHotKey helpfile so you don''t register wrong, as there are numerical limits depending on what you''re using the stuff for, and you should probably use GlobalAddAtom to get a good ID.

-fel

Share this post


Link to post
Share on other sites