How to invoke a button using C++ Win32 API when pr

2019-08-16 20:14发布

问题:

i have created a dialogbox using c++ win32 API... there are 3 text box,1 combo box and 3 buttons...

now i have 2 problems...

1.when i press the ENTER button,it invoke second button(ID_OK) function,but i want to invoke first button(ID_MYBUTTON)...

2.i am using the code to focus a textbox is,

SetFocus(GetDlgItem(_hwnd, IDC_NAME));

But it cant focus that dialogbox,i mean cursor position is there,but cant get any value,when i typed...

Can anyone resolve it?

回答1:

This may answer both your questions: http://blogs.msdn.com/b/oldnewthing/archive/2004/08/02/205624.aspx:

Use the DM_SETDEFID message to set the default button in a dialog

Use the WM_NEXTDLGCTL message instead of SetFocus()

// set default button
SendMessage(_hwnd, DM_SETDEFID, (WPARAM)ID_MYBUTTON, 0);
//TODO: if the former default button's style remains, update with BM_SETSTYLE

// set focus
SendMessage(_hwnd, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(_hwnd, IDC_NAME), TRUE);