Thread: How does Message Mapping work?

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    How does Message Mapping work?

    After a "dummy" app. has been created(buttons menus, dialogs,etc...) how do I begin creating processes for these controls to actually do something?

    Or when the user resizes the window, let's say I want the app to do several things following this event,etc...

    And are "clean-up" procedures usually necassary for these operations?

    Generalizations are O.K.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Your best step is to have a look at the following tutorial;
    Sunlight's tutorials

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Here's the generated message map (comments removed)-

    Code:
    BEGIN_MESSAGE_MAP(CmfcApp, CWinApp)
    	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
    	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
    	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
    	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
    END_MESSAGE_MAP()
    ON_COMMAND is the message. When your app gets this message it will check which of your buttons/menus(ID_APP_ABOUT, ID_FILE_NEW) etc have recieved it. It will then call the function that will respond to the message (OnAppAbout, OnFileNew).

    But with MFC, you shouldn't have to code this yourself as the class wizard should take you through the steps where you assign messages to the Object ID's and then create the function for the handling of the message.

    I think the sizing is handled by the code that MFC generates on start up, but you could check the function CWnd::OnSize which will provide info on resizing.

    Disclaimer - I've hardly ever used MFC so some/all of this may be not exactly accurate.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Thank You!

    And where exactly in the code should this be placed?

    Re:MFC

    I have no interest in MFC! I realize that it probably simplifies programming windows 100 fold, but I would rather just learn the standard API. No offense or anything!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    This code is MFC.

    if you are only interested in learning the api you use a switch statement in your window procedure to process the messages.

    Get hold of petzold's programming windows.He explains this very well.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Originally posted by Sebastiani
    I have no interest in MFC! I realize that it probably simplifies programming windows 100 fold, but I would rather just learn the standard API. No offense or anything!
    No, problem. I prefer the straight API. I assumed you got the title of your post from some MFC code, my fault.

    It works on a similar principle using the API, as stoned coder has stated you need to write your message handling code in your window procedure. So that it will capture the WM_COMMAND messages.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    here is some code from petzold to illustrate what you need to do.....

    Code:
    #include <windows.h>
    #include <math.h>
    
    #define NUM 1000
    #define TWOPI (2*3.14159)
    
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    
    {
    	static TCHAR szAppName[]=TEXT("SineWave");
    	HWND hwnd;
    	MSG msg;
    	WNDCLASS wndclass;
    
    	wndclass.style=CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc=WndProc;
    	wndclass.cbClsExtra=0;
    	wndclass.cbWndExtra=0;
    	wndclass.hInstance=hInstance;
    	wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    	wndclass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
    	wndclass.lpszMenuName=NULL;
    	wndclass.lpszClassName=szAppName;
    
    	if (! RegisterClass(&wndclass))
    	{
    		MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
    		return 0;
    	}
    
    	hwnd=CreateWindow(szAppName,TEXT("Sine wave using polyline function!"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    
    	ShowWindow(hwnd,iCmdShow);
    	UpdateWindow(hwnd);
    
    	while (GetMessage(&msg,NULL,0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    	static int cxClient,cyClient;
    	HDC hdc;
    	int i;
    	PAINTSTRUCT ps;
    	POINT apt[NUM];
    
    	switch(message)
    	{
    	case WM_SIZE:
    		cxClient=LOWORD(lParam);
    		cyClient=HIWORD(lParam);
    		return 0;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	case WM_PAINT:
    		hdc=BeginPaint(hwnd,&ps);
    		MoveToEx(hdc,0,cyClient/2,NULL);
    		LineTo(hdc,cxClient,cyClient/2);
    		for (i=0;i<NUM;i++)
    		{
    			apt[i].x=i*cxClient/NUM;
    			apt[i].y=(int) (cyClient/2*(1-sin(TWOPI*i/NUM)));
    		}
    		Polyline(hdc,apt,NUM);
    		return 0;
    	}
    	return DefWindowProc(hwnd,message,wParam,lParam);
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Wow, thanks! I didn't realize that I was already "message-mapping", just got confused by the phrase!

    And thanks for the code, it helped me see that...

    And yes, I know, I know, get Petzold...but right now I am a poor old chap and so I am trying to jump-start with tutorials and the like till I recieve my check from those poor bastards I'm falsly suing for that little "slip and fall" incident at the Grocer's(See my tutorial "How to Fall Like a Pro...and Settle Fast" on my new website)...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ever heard of a library ??
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm...thanks! Never thought of that. Probably nothing, but I'll try anyway...Good Suggestion, though.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    That damn echo.........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  3. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 02:22 AM
  4. Problem with Printing message
    By robert_sun in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 02:09 PM
  5. arithmetic coding - end of message symbol
    By dnc_01 in forum C Programming
    Replies: 2
    Last Post: 04-11-2004, 04:20 AM