Thread: default button

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    default button

    Why doesn't this default button react when Enter is pressed?

    Code:
    CreateWindow (TEXT("button"),TEXT("Ok"),
    WS_CHILDWINDOW	| WS_VISIBLE | BS_DEFPUSHBUTTON,
    105, 160, 110, 20, hwnd, (HMENU)IDOK, GetModuleHandle(NULL),
    NULL);
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Without seeing how you are handling the parent's WM_COMMAND message, it's difficult to say. Assuming your handler is good, are you using IsDialogMessage in your message loop to get default keyboard action?

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    No, I'm not using IsDialogMessage(). I didnt know it was needed. The main window is created using CreateWindow instead of from a resource file.

    For what it's worth:
    Code:
    case WM_COMMAND:
    switch (LOWORD(wParam))
    {
       case IDOK:
          SendMessage(hwnd,WM_CLOSE,0,0);
       break;
       default:
       break;
    }
      return 0;
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Your WM_COMMAND handler looks fine. Put a call to IsDialogMessage in your message loop and it should work as you intended. It doesn't matter how the control/window is created; if you want default dialog keyboard behaviour you will need to use IsDialogMessage (or do it all manually).

  5. #5
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    CreateWindow (TEXT("button"),TEXT("Ok"),
    WS_CHILDWINDOW | WS_VISIBLE | BS_DEFPUSHBUTTON,
    105, 160, 110, 20, hwnd, (HMENU)IDOK, GetModuleHandle(NULL),NULL);
    Why are you using GetModualHandle()?
    The GetModuleHandle function returns a module handle for the specified module if the file has been mapped into the address space of the calling process.
    HMODULE GetModuleHandle(LPCTSTR lpModuleName)

    lpModuleName
    Pointer to a null-terminated string that contains the name of the module (either a .DLL or .EXE file). If the filename extension is omitted, the default library extension .DLL is appended. The filename string can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case independently) to the names of modules currently mapped into the address space of the calling process.
    If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process.
    You don't need a handle to a file,you need the HINSTANCE for the main window. This is what you should be using
    Code:
    CreateWindow (TEXT("button"),TEXT("Ok"),WS_CHILDWINDOW | WS_VISIBLE | BS_DEFPUSHBUTTON,
    105, 160, 110, 20, hwnd,(HMENU)IDOK, 
    (HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE), NULL);
    Now I may be mistaken but I am pretty sure GetModualHandle is not what you need, but I could be wrong. I never seen this used before so that is why I am questioning it.
    Last edited by Dohojar; 07-15-2003 at 08:54 PM.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    I don't know why I'm using GetModule(), but it does works. You probabley are right that I have an error and will correct it.

    Thanks again for the help.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Lambs4: Use IsDialogMessage in your message loop as I have previously described as it will give you your intended functionality unless there is some other error in your code.

    Originally posted by Dohojar
    Why are you using GetModualHandle()?
    Because an HMODULE is the same basic type as an HANDLE and an HINSTANCE. Using GetModuleHandle(0) returns the application instance - try it for yourself by comparing its value with that passed to WinMain or returned from GetWindowLong and you will see that they are the same. I assure you the use of GetModuleHandle(0) in this way is very common.

  8. #8
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    See one can learn something new everyday. After re-reading microsoft's documentation on it for the 5th time, I was starting to wonder if they were the same. Thanks for the info. I will try using that the next app I make. sure is a lot easier to type. So the return from GetModualHandle() doesn't need to be cast either then?
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default Button Problem
    By Dark Dude in forum Windows Programming
    Replies: 5
    Last Post: 12-25-2008, 08:06 PM
  2. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  3. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  4. Default checking of radio button in a group box
    By juhigarg in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2001, 12:25 AM
  5. Default checking of radio button in a group box
    By juhigarg in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 01:31 AM