Creating Controls (buttons, listboxes, etc.) in Main Window (Win32)?

2»

Comments

  • : [blue]I am lost here... you want to be able to exit the application by pressing 'X' anywhere in the program? What if you typing some text in the edit box?
    :
    : Can you clarify what exactly you need to do?
    : Also, please, post the code how did you get that FaceKey variable?
    : [/blue]
    :
    Sorry,

    I'm expecting to have key recognition for the buttons when focused on the main window/buttons. Edit controls will handle their text when focused (I hope).

    FaceKey is actually arbitrary and a holdover from two previous iterations where I was working with sending keystrokes to an alternate window. At this point it should probably be PressedKey.

    [code]
    case WM_KEYDOWN:
    FaceKey = wParam;
    ExtKey = lParam;
    if (FaceKey == 'X')
    PostMessage(hwnd, WM_CLOSE, 0, 0);
    checkButtons();
    [/code]

    This currently gives me recognition of the EXIT button. checkButtons gives me recognition of the other buttons in the same manner. This code is inside WndProc. checkButtons is outside WndProc. The PostMessage with WM_CLOSE only works here, not in checkButtons. A PostMessage to ID_EXIT doesn't work anywhere.

    This is really only a curiosity right now since I have working code for what I need presently. (Although, I will admit I may have many convention errors.):-o

    I really appreciate all your help and hope I'm not tying you up too much with my queries. And thanks for the casting note. I should have realized that after having to cast the HMENU for the ID_####. Thanks for all.

    Take Care,
    Ed


  • Here's a bigger picture of some of the code so you can see what I'm doing. If you notice I end up having to handle the keystroke recognition separately from the button click recognition. This won't be an issue when I provide the routines for each button, but I had expected to be able to use PostMessage to the ID_#### in WndProc from within checkButtons. Thanks again for all.

    [code]
    void checkButtons()
    {
    if (FaceKey == 'N')
    NextCommand();
    if (FaceKey == 'C')
    MessageBox(hwnd, "Cancel button pressed.", "It worked!", MB_OK);
    }


    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_CREATE:
    hwndNext = CreateWindow("BUTTON", "&Next", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 200, 350, 60, 30, hwnd,(HMENU) ID_NEXT, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    hwndCancel = CreateWindow("BUTTON", "&Cancel", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 360, 350, 60, 30, hwnd,(HMENU) ID_CANCEL, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    hwndExit = CreateWindow("BUTTON", "E&xit", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 560, 350, 60, 30, hwnd,(HMENU) ID_EXIT, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);

    if (MessageBox(hwnd, "Acceptance statement", "Agreement", MB_YESNO) != IDYES)
    PostMessage(hwnd, WM_DESTROY, 0, 0);
    break;

    case WM_KEYDOWN:
    FaceKey = wParam;
    ExtKey = lParam;
    if (FaceKey == 'X')
    PostMessage(hwnd, WM_CLOSE, 0, 0);
    checkButtons();
    break;

    case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case ID_EXIT:
    PostMessage(hwnd, WM_CLOSE, 0, 0);
    break;
    case ID_NEXT:
    MessageBox(hwnd, "Next button pressed.", "It worked!", MB_OK);
    break;
    case ID_CANCEL:
    MessageBox(hwnd, "Cancel button pressed.", "It worked!", MB_OK);
    break;
    case ID_FILE_EXIT:
    PostMessage(hwnd, WM_CLOSE, 0, 0);
    break;
    }
    break;

    case WM_CLOSE:
    if (MessageBox(hwnd, "Close program?","Exit Application?", MB_YESNO) == IDYES)
    DestroyWindow(hwnd);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
    }
    [/code]

    Take Care,
    Ed

  • [blue]I see...

    Keyboard input will be received by a window which currently has focus. That means if any of buttons has focus - this code will not work, because that WM_KEYDOWN will be intercepted by a button.

    Try to click outside of a button and then press 'X' - see if it works.

    There is also a way to do it with accelerator, but you need to use the key combination, because 'X' may be needed to edit things. Accelerator intercepts keys BEFORE they go into window procedure, so edit boxes on your window will lose 'X' character.

    You can use ALT+X for exiting, so lovable by Borland. :-)[/blue]
  • : [blue]I see...
    :
    : Keyboard input will be received by a window which currently has focus. That means if any of buttons has focus - this code will not work, because that WM_KEYDOWN will be intercepted by a button.
    :
    : Try to click outside of a button and then press 'X' - see if it works.
    :
    : There is also a way to do it with accelerator, but you need to use the key combination, because 'X' may be needed to edit things. Accelerator intercepts keys BEFORE they go into window procedure, so edit boxes on your window will lose 'X' character.
    :
    : You can use ALT+X for exiting, so lovable by Borland. :-)[/blue]
    :
    Actually, it's almost totally working as wanted. While the main window has focus, all keystrokes and buttons work. When in an edit or combo box, it has the keyboard and mouse. This way I can type away in the Notes editbox and all keys type. I've chosen to include CR in the edit so that doesn't leave the box either. Unfortunately, a mouse click in the unused main window doesn't remove focus from the edit (yet). Choosing a button via the mouse does remove focus from the edit. More studies to pursue... Thanks again.

    Take Care,
    Ed

  • : : [blue]I see...
    : :
    : : Keyboard input will be received by a window which currently has focus. That means if any of buttons has focus - this code will not work, because that WM_KEYDOWN will be intercepted by a button.
    : :
    : : Try to click outside of a button and then press 'X' - see if it works.
    : :
    : : There is also a way to do it with accelerator, but you need to use the key combination, because 'X' may be needed to edit things. Accelerator intercepts keys BEFORE they go into window procedure, so edit boxes on your window will lose 'X' character.
    : :
    : : You can use ALT+X for exiting, so lovable by Borland. :-)[/blue]
    : :
    : Actually, it's almost totally working as wanted. While the main window has focus, all keystrokes and buttons work. When in an edit or combo box, it has the keyboard and mouse. This way I can type away in the Notes editbox and all keys type. I've chosen to include CR in the edit so that doesn't leave the box either. Unfortunately, a mouse click in the unused main window doesn't remove focus from the edit (yet). Choosing a button via the mouse does remove focus from the edit. More studies to pursue... Thanks again.
    :
    : Take Care,
    : Ed
    :
    :
    [blue]It is easy to remove focus from edit: in your main window procedure include case WM_LBUTTONDOWN and respond to it with SetFocus (your main HWND); Return 0 in that case. This way mouse click will send the focus back to main and since Windows cannot have focus in two places - it will effectively remove it from edit box.[/blue]
  • : [blue]It is easy to remove focus from edit: in your main window procedure include case WM_LBUTTONDOWN and respond to it with SetFocus (your main HWND); Return 0 in that case. This way mouse click will send the focus back to main and since Windows cannot have focus in two places - it will effectively remove it from edit box.[/blue]
    :
    Works great! Thanks. I had tried SetForegroundWindow without success. I don't know why I didn't think to try SetFocus. I guess I was just too busy with my comboboxlist. I think that's all set now, but I have a huge list to put in it. I think I'll have to make it an include file.

    Take Care,
    Ed

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories