Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows - WinMain, MsgHandler, WinProc...

Status
Not open for further replies.

ProgramminFool

Technical User
Oct 26, 2003
10
CA
I am moving (trying to!) from C++ console apps to windows apps. I have looked at some textbooks, and they explain that you need WinMain, sort of in place of main, and also a message handler or pump or something.

I understand that windows are sent messages and that it is generally a good idea to use a switch statement to use them. Is the function ALWAYS in the form: LRESULT CALLBACK WndProc (..blahblah...) ? Are there other ways to get these messages? I have seen a function called MsgHandler, what's that all about? I've seen PeekMessage();. Finally, what the heck is this?

Code:
        MSG Msg;
	while(GetMessage(&Msg, NULL, 0, 0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
[\code]

Thanks for your time people, any help would be hugely appreciated.

------------------------------
“Talent is cheap, dedication is expensive. It will cost you your life.”
 
1.
> understand that windows are sent messages and that it is
>generally a good idea
What kind of good idea? it is the standard method.

2.
>Are there other ways to get these messages
So far these ways are enough for you and will be enough for very long time. Overall, these ways are standard. Do you think, you will do a revolution in informatics in the matter of getting messages from message queue?
Some specific functions you will use in specific situations.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
No revolutions :)

I thought messages were automatically sent to your WndProc function. Then I saw the loop with GetMessage, TranslateMessage, and DispatchMessage.

So two questions: 1) Does a windows message go automatically to your WndProc function? 2) Does the Get,Translate,Dispatch loop have to run in order to get messages?

I checked MSDN and my textbook. MSDN doesn't list WndProc as a WinApi function, but my textbook uses it all the time.

-------------------------------------------------
“Talent is cheap, dedication is expensive. It will cost you your life.”
 
GetMessage extracts a message from the MessageQueue, but DispatchMessage sends it to the right window

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
WindowProc is the message handler that you write yourself in order to handle windows messages manually. When the message is dispatched, it gets sent to your windowProc, where you can handle the messages that you want, and pass the ones that you don't to DefWindowProc.

for example

LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wPar, LPARAM lPar)
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
Break;
default: return DefWindowProc(hWnd, uMsg, wPar, lPar);
}
return 0;
}
 
Thank you very much for responding, your post and further reading have straightened this matter out for me. Have a good day BGrego!

-------------------------------------------------
“Talent is cheap, dedication is expensive. It will cost you your life.”
 
A standard method for keeping server running when there are no windows is a message loop like that:
while(GetMessage(&Msg, NULL, 0, 0))
DispatchMessage(&Msg);
this loop wil finish when somewhere in application will ve called PostQuitMessage(exitCode);
this exitCode you will retreive in msg.wParam
So you can return from WinMain this msg.wParam.


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top