Thread: button 'message'

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071

    button 'message'

    how would i get a message in the WM_COMMAND from the following button?:

    Code:
    HWND button;
    button = CreateWindow("BUTTON", "NPC's", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 616, 30, 64, 24, hwnd, NULL, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    -psychopath

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If you mean get a notification of a button click event then you would check for BN_CLICKED notifications. Something like:
    Code:
    case WM_COMMAND:
      {
      WORD wID,wNotify;
    
      wID=LOWORD(wParam);
      wNotify=HIWORD(wParam);
      if (lParam)
        {
        /* it's a control notification message */
        if (wNotify==BN_CLICKED)
          {
           /* a button control has been clicked */
          if (wID==id of your button control) /* replace with actual value */
            {
            /* the button clicked has an identifier of 
            id of your button control */
            }
          }
        }
      }
    Note that you should give your button control a numeric identifier as the HMENU parameter of CreateWindow/CreateWindowEx; id of your button control should be replaced with the actual value you use.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    hmm...it dosn't seem to work...i copyed and pasted and edited the code you posted to look like:
    Code:
    case WM_COMMAND:
    {
    LPARAM lparam;
      WORD wID,wNotify;
    
      wID=LOWORD(wparam);
      wNotify=HIWORD(wparam);
      if (lparam)
        {
        /* it's a control notification message */
        if (wNotify==BN_CLICKED)
          {
           /* a button control has been clicked */
          if (wID==10) /* replace with actual value */
            {
            MessageBox (NULL, "it works" , ":)", 0);
            }
          }
        }
      }
    and the button code to look like:
    Code:
    HWND button;
    button = CreateWindow("BUTTON", "NPC's", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 616, 30, 64, 24, hwnd, (HMENU) 10, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you wrap your code?
    Code:
    HWND button;
    button = CreateWindow("BUTTON", "NPC's", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 
                          616, 30, 64, 24, hwnd, (HMENU) 10, 
                          (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    It cuts down on the horizontal scroll bars
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    ok....still a bit new...anyway..can anyone help?

    -psychopath

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>LPARAM lparam;<<

    This wasn't in the original example I posted and with good reason. lParam is the last parameter passed to your window procedure.ie
    Code:
    LRESULT CALLBACK Wndproc(HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam)
    {
    case WM_COMMAND:
    /*etc*/
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    ok..removed that but it still dosn't work!?

    -psychopath

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Please post or attach your code. It would also be helpful if you could post any error messages you're getting.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Code:
    // Include files
    #include <windows.h>
    #include <gl/gl.h>
    #include "menu.h"
    //end include files
    
    //defs
    #define SCREEN_DEPTH 32
    #define IDBTN01 10
    //end defs
    
    HWND edit_box = NULL; //edit window handle
    
    // Function Declarations
    LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
    VOID EnableOpenGL( HWND edit_box, HDC * hDC, HGLRC * hRC );
    VOID DisableOpenGL( HWND edit_box, HDC hDC, HGLRC hRC );
    
    // WinMain
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow )
    {
    WNDCLASS wc;  //main window class
    WNDCLASS ed;  //edit window class
    WNDCLASS mp;  //model preview class
    HWND hwnd;   //main window handle
    HDC hdc;     //windows device context
    HGLRC gl_hrc;//OGL device context
    MSG msg;     //message handle
    BOOL bQuit = FALSE;
    float theta = 0.0f;
    
    // register window class
    wc.style = NULL;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = (HBRUSH)GetStockObject( GRAY_BRUSH );
    wc.lpszMenuName = "MAINMENU";
    wc.lpszClassName = "PsychoEngine";
    RegisterClass( &wc );
    
    //regester edit class
    ed.style = CS_OWNDC;
    ed.lpfnWndProc = WndProc;
    ed.cbClsExtra = 0;
    ed.cbWndExtra = 0;
    ed.hInstance = hInstance;
    ed.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    ed.hCursor = LoadCursor( NULL, IDC_ARROW );
    ed.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
    ed.lpszMenuName = NULL;
    ed.lpszClassName = "EDIT";
    RegisterClass( &ed );
    
    //regester model preview class
    mp.style = CS_OWNDC;
    mp.lpfnWndProc = WndProc;
    mp.cbClsExtra = 0;
    mp.cbWndExtra = 0;
    mp.hInstance = hInstance;
    mp.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    mp.hCursor = LoadCursor( NULL, IDC_ARROW );
    mp.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
    mp.lpszMenuName = NULL;
    mp.lpszClassName = "MODEL";
    RegisterClass( &mp );
    
    // create main window
    hwnd = CreateWindow(
    "PsychoEngine", "PsychoEngine | PsychopathProductions",
    WS_TILEDWINDOW | WS_POPUPWINDOW | WS_VISIBLE,
    0, 0, 800, 570,
    NULL, NULL, hInstance, NULL );
    
    HWND edit_box = NULL;
    HWND model_box = NULL;
    LPARAM lParam;
    
    RECT rect;
    GetClientRect(hwnd,&rect);
    //set up and create editbox
    edit_box = CreateWindow("EDIT", "Edit Window",WS_CHILD | WS_CAPTION | WS_VISIBLE,
    0, 0, 600, 516, hwnd, NULL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
    
    //setup and create model preview box
    model_box = CreateWindow("MODEL", "Model Preview",WS_CHILD | WS_CAPTION | WS_VISIBLE,
    600, 338, 192, 178, hwnd, NULL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
    
    //draw buttons
    HWND button;
    button = CreateWindow("BUTTON", "NPC's", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 616, 30, 64, 24, hwnd,(HMENU) 10, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    button = CreateWindow("BUTTON", "Player", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 708, 30, 64, 24, hwnd, NULL, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    button = CreateWindow("BUTTON", "World Options", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 646, 58, 98, 24, hwnd, NULL, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    button = CreateWindow("BUTTON", "Lighting", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 616, 86, 64, 24, hwnd, NULL, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    button = CreateWindow("BUTTON", "", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 708, 86, 64, 24, hwnd, NULL, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    // enable OpenGL for the edit window
    EnableOpenGL( edit_box, &hdc, &gl_hrc );
    
    // program main loop
    while ( !bQuit ) {
    
    // check for messages
    if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
    
    // handle or dispatch messages
    if ( msg.message == WM_QUIT ) {
    bQuit = TRUE;
    } else {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
    }
    
    } else {
    
    // OpenGL animation code
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT );
    
    glPushMatrix();
    glBegin( GL_TRIANGLES );
    glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 0.87f, 0.0f );
    glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.87f, -0.5f, 0.0f );
    glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( -0.87f, -0.5f, 0.0f );
    glEnd();
    glPopMatrix();
    
    SwapBuffers( hdc );
    
    theta += 1.0f;
    
    }
    
    }
    
    // shutdown OpenGL
    DisableOpenGL( hwnd, hdc, gl_hrc );
    
    // destroy the window explicitly
    DestroyWindow( hwnd );
    
    return msg.wParam;
    
    }
    
    // Window Procedure
    
    LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
    {
    HWND edit_box = NULL;
    HWND model_box = NULL;
    
    switch ( message )
    {
    case WM_CREATE:
    break;
    
    case WM_CLOSE:
    MessageBox (NULL, "Are you sure you want to close the program?\nAll un-saved data will be lost." , "Exiting...", 0 + MB_YESNO + MB_DEFBUTTON2 + MB_ICONQUESTION);
    PostQuitMessage( 0 );
    break;
    return 0;
    
    case WM_DESTROY:
    break;
    return 0;
    
    case WM_KEYDOWN:
    switch ( wparam ) {
    
    case VK_ESCAPE:
    PostQuitMessage( 0 );
    break;
    return 0;
    
    case WM_COMMAND:
    {
      WORD wID,wNotify;
    
      wID=LOWORD(wparam);
      wNotify=HIWORD(wparam);
      if (lparam)
        {
        /* it's a control notification message */
        if (wNotify==BN_CLICKED)
          {
           /* a button control has been clicked */
          if (wID==10) /* replace with actual value */
            {
            MessageBox (NULL, "it works" , ":)", 0);
            }
          }
        }
      }
    
    }
    return 0;
    
    default:
    return DefWindowProc( hwnd, message, wparam, lparam );
    
    }
    
    }
    
    // Enable OpenGL
    
    VOID EnableOpenGL( HWND edit_box, HDC * hdc, HGLRC * gl_hrc )
    {
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;
    
    // get the device context (DC)
    *hdc = GetDC( edit_box );
    
    // set the pixel format for the DC
    ZeroMemory( &pfd, sizeof( pfd ) );
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
    PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = SCREEN_DEPTH;
    pfd.cDepthBits = SCREEN_DEPTH;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat( *hdc, &pfd );
    SetPixelFormat( *hdc, iFormat, &pfd );
    
    // create and enable the render context (RC)
    *gl_hrc = wglCreateContext( *hdc );
    wglMakeCurrent( *hdc, *gl_hrc );
    
    }
    
    // Disable OpenGL
    
    VOID DisableOpenGL( HWND edit_box, HDC hdc, HGLRC gl_hrc )
    {
    wglMakeCurrent( NULL, NULL );
    wglDeleteContext( gl_hrc );
    ReleaseDC( edit_box, hdc );
    }
    thats the code for the whole program....there arn't any error messages...but i kno it dosn't work, other wise it would display the message box when i click the button

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Spacing your code pays dividends:
    Code:
    case WM_KEYDOWN:
    switch ( wparam ) {
    
    	case VK_ESCAPE:
    		PostQuitMessage( 0 );
    		break;
    		return 0;
    
    	case WM_COMMAND:
    	{
    		WORD wID,wNotify;
    
    		wID=LOWORD(wparam);
    		wNotify=HIWORD(wparam);
    		if (lparam)
    		{
    			/* it's a control notification message */
    			if (wNotify==BN_CLICKED)
    			{
    				/* a button control has been clicked */
    				if (wID==10) /* replace with actual value */
    				{
    				        MessageBox (NULL, "it works" , ":)", 0);
    				}
    			}
    		}
    	}
    }

  11. #11
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    hmm....ok...but i still don't know how to get the code to work properly......or am i missing somthing?

  12. #12
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    WM_ COMMAND is a separate message - you appear to be treating it as part of your WM_KEYDOWN switch statement.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  13. #13
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    ooops!!!...ok now i feel really stupid..lol...anyway thanks all!

    -psychopath

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom button using bitmaps
    By Eirik in forum Windows Programming
    Replies: 2
    Last Post: 11-04-2008, 12:20 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM