Sætte focus til en editboks i en dialog
Jeg har prøvet mange løsninger for at prøve at ændre focus mellem mine kontroller i en dialogboks. Det er meningen, at når brugeren kommer ind i programmet, så skal en editboks allerede være markeret.Til dette formål har jeg prøvet: WM_SETFOCUS, der godt nok sætter focus til editboksen, men herefter vil programmet ikke tage i mod keyboard inputs
Derudover har jeg prøvet SetFocus(hwnd), men den havde tilsyneladende ingen effekt.
Her er min kode:
LRESULT CALLBACK ExpressionProc(HWND HwndEdit, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_KEYDOWN)
{
if(wParam == VK_RETURN)
{
CalcExpression(DialogHandle);
return 0;
}
}
return CallWindowProc(OldEditProc, HwndEdit, msg, wParam, lParam);
}
BOOL CALLBACK MathCalcDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HWND hTemp;
switch(Message)
{
case WM_INITDIALOG:
//Save the dialoghandle in a global var
DialogHandle=hwnd;
//Resize controls
PostMessage(hwnd, WM_SIZE,0,0);
//Set the windowprocedure for editboks
hTemp = GetDlgItem(hwnd, IDC_EXPRESSION);
OldEditProc = (WNDPROC )GetWindowLong(hTemp, GWL_WNDPROC);
SetWindowLong(hTemp, GWL_WNDPROC, (LONG )ExpressionProc);
//Set the icon
HICON hIcon;
hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_MAIN_ICON), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),0);
if(hIcon)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
break;
case WM_CLOSE:
DestroyWindow(hwnd); //Send besked om at lukke vinduet
break;
case WM_DESTROY:
PostQuitMessage(0); //Luk vinduet
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_CALC_EXPRESSION:
CalcExpression(hwnd);
break;
}
break;
case WM_SIZE:
RECT rcClient;
//Få fat i det område vi kan vise på.
GetClientRect(hwnd, &rcClient);
//Tilpas listboksen
hTemp = GetDlgItem(hwnd, IDC_CALCRESULTS);
SetWindowPos(hTemp, NULL, rcClient.left+5, rcClient.top, rcClient.right-10, rcClient.bottom-40, SWP_NOZORDER);
//Tilpas inputboksen
hTemp = GetDlgItem(hwnd, IDC_EXPRESSION);
SetWindowPos(hTemp, NULL, rcClient.left+30, rcClient.bottom-25, rcClient.right-35, 20, SWP_NOZORDER);
//Tilpas calcknappen
hTemp = GetDlgItem(hwnd, IDC_CALC_EXPRESSION);
SetWindowPos(hTemp, NULL, rcClient.left+5, rcClient.bottom-25, 25, 20, SWP_NOZORDER);
//Tilpas errorlabel
hTemp = GetDlgItem(hwnd, IDC_ERROR);
SetWindowPos(hTemp, NULL, rcClient.left+5, rcClient.bottom-38, 200, 13, SWP_NOZORDER);
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
g_hInst=hInstance;
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_WINMATHCALC), NULL, MathCalcDlgProc);
return 1;
}