Thread: Window won't open on button command?

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

    Window won't open on button command?

    I can't get a window to display on a button command.....here is my WM_COMMAND code:
    Code:
    case WM_COMMAND:
    WORD wID,wNotify;
    HINSTANCE hInstance;
    HWND BrushWin;
    
      wID=LOWORD(wparam);
      wNotify=HIWORD(wparam);
      if (lparam)
        {
        //if a button was clicked
        if (wNotify==BN_CLICKED)
          {
           //if a buttons id was clicked
          if (wID==10)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
          if (wID==20)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
          if (wID==30)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
          if (wID==40)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
          if (wID==50)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
          if (wID==60)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
          if (wID==70)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
          if (wID==80)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
          if (wID==90)
          {
          BrushWin = CreateWindow( 
    	  "ModelWin", "Model Preview", 
    	  WS_CHILD | WS_DLGFRAME | WS_VISIBLE,
    	  64, 64, 400, 300,
    	  hWnd, NULL, hInstance, NULL );
          }
          if (wID==100)
          {
          MessageBox (NULL, "it works" , ":)", 0);
          }
       }
    }
    break;
    it's probably somthing simple that i have done wrong....but i can't seem to figure it out
    Also please not that the 'it works' message boxs are so can tell if button command is at least being processed.

    any help will be appreciated
    -psychopath

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Is the Window of that windows-procedure (WNDPROC) the parent window of the buttons?
    Double check that the button ID's are correct.
    Use you debugger to set a break point in the windows procedure to check the values you are receiving.

    gg

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Q #1: no....the WNDPROC is for a window that is the parent of 3 child windows, and one of those 3 is the parent of the buttons

    Q #2: the buttons ID's are correct

    -psychopath

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Then BN_CLICKED notification is sent to the parent window of the button.
    If you want the grand-parent to handle the message, the WM_COMMAND messages will have to be forwarded to it.

    gg

  5. #5
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    ummm......ok sorry if i start sounding stupid, but i don't quite understand what you mean....as far as i know the BN_CLICKED notification IS going to the parent window of the button....otherwise those message boxs' for the other buttons wouldn't display would they?
    please correct me if i'm wrong.

    -psychopath

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >Is the Window of that windows-procedure (WNDPROC) the parent window of the buttons?
    >>no....the WNDPROC is for a window that is the parent of 3 child windows, and one of those 3 is the parent of the buttons

    The BN_CLICKED notification is sent only to the immediate parent. The parent of the parent (grandparent) does not receive BN_CLICKED.

    gg

  7. #7
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    iv'e changed the code a bit...(don't think it makes much of a difference tho...just looks cleaner to me):

    Code:
    case WM_COMMAND:
    WORD wID,wNotify;
    
    wID=LOWORD(wparam);
    wNotify=HIWORD(wparam);
    
    case BN_CLICKED:
    if(wID == 90)
    {
    HWND BrushWin;
    HINSTANCE hInstance;
    BrushWin = CreateWindow( 
    "BrushWin", "", 
    WS_CHILD | WS_CAPTION | WS_VISIBLE,
    64, 64, 400, 300,
    hWnd, NULL, hInstance, NULL );
    }
    return 0;
    break;
    ok...now i'm being REALLY thick, but..

    >The BN_CLICKED notification is sent only to the immediate parent. The parent of the parent (grandparent) does not receive BN_CLICKED.<

    ok...i understand that...but i don't see how this relates to the problem: "the window won't appear".....the button notification is obviously going somewhere and working, because when i replace the window creation code with a message box, the message box displays on the button press, thus, the notification is being sent and processed.

    please correct me if i'm wrong as i really don't understand this very well.

    -psychopath

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It does help to establish what the actual problem is - seeing how I thought the problem was with receiving the BN_CLICKED notification....

    Does the call CreateWindow() actually succeed? What does GetLastError() return if it doesn't?

    gg

  9. #9
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    sorry for the mis-communication....i thought i did state the problem, as the title of the post was
    "window won't open on button command?"....but obviously not..my bad

    anyway..
    >>Does the call CreateWindow() actually succeed? What does GetLastError() return if it doesn't?<<
    i'm not quite sure if it's succeeding or not....i also don't really know how to use the GetLastError() function, but i did try adding:
    Code:
    if(BrushWin = NULL)
    {
    	MessageBox (NULL, "Window could not be displayed" , ":(", 0);
    }
    after the CreateWindow() function, and nothing happend, which makes me think that the CreateWindow() call IS succeeding, but the window just won't display

    -psychopath

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    9
    I may be wrong, but I think hInstance needs to be set to the HINSTANCE of the parent. You are defining it, but not setting it to anything.

  11. #11
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    hInstance is the HINSTANCE of the parent.
    without that definition there it says "hInstance not defined"

    -psychopath

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    9
    Quote Originally Posted by psychopath
    hInstance is the HINSTANCE of the parent.
    without that definition there it says "hInstance not defined"

    -psychopath
    My point is that it needs to be set to the HINSTANCE of the parent, instead of just being empty.. actually easier to just use a global variable that holds the parent's hinstance, usually hInst in my programs.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    9
    You also need to register the class for the child window..here's a short example:
    Code:
    LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
      switch (msg) {
      case WM_COMMAND:
        {
          switch(LOWORD(wParam))
          {
            case 1001:
            {
                WNDCLASS wc;
                memset(&wc,0,sizeof(WNDCLASS));
                wc.style = 0;
                wc.lpfnWndProc = (WNDPROC)MainWndProc;
                wc.hInstance = hInst;
                wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
                wc.lpszClassName = "TestWndChildClass";
                wc.lpszMenuName = NULL;
                wc.hCursor = LoadCursor(NULL,IDC_ARROW);
                wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); 
                if (RegisterClass(&wc))
                  {
                    hwndNew = CreateWindow("TestWndChildClass","",
                    WS_CHILD|WS_VISIBLE|WS_CAPTION,
                    110,10,100,100,
                    hwndMain,
                    NULL,
                    hInst,
                    NULL);
                  }
              }
          break;
          }
        }
        break;
      case WM_DESTROY:
        PostQuitMessage(0);
        break;
      default:
        return DefWindowProc(hwnd,msg,wParam,lParam);
      }
      return 0;
    }
    hwndMain and hInst are both global vars holding the HWND and HINSTANCE of the parent.
    Hope this helps.
    Last edited by SnarlingSheep; 08-15-2004 at 09:31 PM.

  14. #14
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    ok awesome i got it to work....thanx!!

    -psychopath

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM