使用SetFocus(GetDlgItem(hDlg,IDC_EDIT1));得不到焦点,但GetDlgItem(hDlg,IDC_EDIT1);有值源程序:
#include <windows.h>
#include <string.h>
#include "resource.h"BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
BOOL APIENTRY passwordDlgProc(HWND,UINT,WPARAM,LPARAM);HINSTANCE hInst;//当前实例句柄
HWND hWndMain;//主窗口句柄//主程序
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg; if(!DialogBox(hInstance,(LPCTSTR)IDD_DIALOG1,NULL,(DLGPROC)passwordDlgProc))
return(FALSE); if(!InitApplication(hInstance))
return(FALSE);
if(!InitInstance(hInstance,nCmdShow))
return(FALSE); while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return(msg.wParam);
}BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS studywin;//定义窗口类 //填写所定义的窗口类的各种属性
studywin.style =0;
studywin.lpfnWndProc =(WNDPROC)MainWndProc;
studywin.cbClsExtra =0;
studywin.cbWndExtra =0;
studywin.hInstance =hInstance;
studywin.hIcon =LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
studywin.hCursor =LoadCursor(NULL,IDC_ARROW);
studywin.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
studywin.lpszMenuName =NULL;
studywin.lpszClassName ="studywin"; //对所定义的窗口类进行注册
return(RegisterClass(&studywin));
}BOOL InitInstance(HINSTANCE hInstance,
int nCmdShow)
{
hInst=hInstance;//保存实例句柄 //创建窗口,成功返回窗口句柄,否则返回0
hWndMain=CreateWindow( "studywin",
"我的窗口",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); if(!hWndMain)
return(FALSE); //显示窗口
ShowWindow(hWndMain,nCmdShow);
//更新窗口客户区
UpdateWindow(hWndMain); return(TRUE);
}LRESULT CALLBACK MainWndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
//HDC hdc;//设备描述句柄
//PAINTSTRUCT ps; switch(message)
{
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd,message,wParam,lParam));
}
return(0);
}BOOL APIENTRY passwordDlgProc(HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HWND error;
switch(message)
{
case WM_INITDIALOG:
//设置对话框中EDIT控件可输入的字符数目为5个
SendDlgItemMessage(hDlg,IDC_EDIT1,EM_SETLIMITTEXT,(WPARAM)8,(LPARAM)0);
//把取消按钮设置为默认按钮
//SendMessage(hDlg,DM_SETDEFID,(WPARAM)IDCANCEL,(LPARAM)0); error=GetDlgItem(hDlg,IDC_EDIT1);
SetFocus(error);
return(TRUE);//模式对话框此处必须返回TRUE
case WM_COMMAND:
switch(LOWORD(wParam))
{
case EN_CHANGE:
break;
case IDOK:
EndDialog(hDlg,TRUE);
return(TRUE);
case IDCANCEL:
EndDialog(hDlg,FALSE);
default:
return(FALSE);
}
break;
}
return(FALSE);
}
#include <windows.h>
#include <string.h>
#include "resource.h"BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
BOOL APIENTRY passwordDlgProc(HWND,UINT,WPARAM,LPARAM);HINSTANCE hInst;//当前实例句柄
HWND hWndMain;//主窗口句柄//主程序
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg; if(!DialogBox(hInstance,(LPCTSTR)IDD_DIALOG1,NULL,(DLGPROC)passwordDlgProc))
return(FALSE); if(!InitApplication(hInstance))
return(FALSE);
if(!InitInstance(hInstance,nCmdShow))
return(FALSE); while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return(msg.wParam);
}BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS studywin;//定义窗口类 //填写所定义的窗口类的各种属性
studywin.style =0;
studywin.lpfnWndProc =(WNDPROC)MainWndProc;
studywin.cbClsExtra =0;
studywin.cbWndExtra =0;
studywin.hInstance =hInstance;
studywin.hIcon =LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
studywin.hCursor =LoadCursor(NULL,IDC_ARROW);
studywin.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
studywin.lpszMenuName =NULL;
studywin.lpszClassName ="studywin"; //对所定义的窗口类进行注册
return(RegisterClass(&studywin));
}BOOL InitInstance(HINSTANCE hInstance,
int nCmdShow)
{
hInst=hInstance;//保存实例句柄 //创建窗口,成功返回窗口句柄,否则返回0
hWndMain=CreateWindow( "studywin",
"我的窗口",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); if(!hWndMain)
return(FALSE); //显示窗口
ShowWindow(hWndMain,nCmdShow);
//更新窗口客户区
UpdateWindow(hWndMain); return(TRUE);
}LRESULT CALLBACK MainWndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
//HDC hdc;//设备描述句柄
//PAINTSTRUCT ps; switch(message)
{
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd,message,wParam,lParam));
}
return(0);
}BOOL APIENTRY passwordDlgProc(HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HWND error;
switch(message)
{
case WM_INITDIALOG:
//设置对话框中EDIT控件可输入的字符数目为5个
SendDlgItemMessage(hDlg,IDC_EDIT1,EM_SETLIMITTEXT,(WPARAM)8,(LPARAM)0);
//把取消按钮设置为默认按钮
//SendMessage(hDlg,DM_SETDEFID,(WPARAM)IDCANCEL,(LPARAM)0); error=GetDlgItem(hDlg,IDC_EDIT1);
SetFocus(error);
return(TRUE);//模式对话框此处必须返回TRUE
case WM_COMMAND:
switch(LOWORD(wParam))
{
case EN_CHANGE:
break;
case IDOK:
EndDialog(hDlg,TRUE);
return(TRUE);
case IDCANCEL:
EndDialog(hDlg,FALSE);
default:
return(FALSE);
}
break;
}
return(FALSE);
}
LayOut-TabOrder
焦点落在TabOrder=1 的控件上。【先后点击控件即可产生TabOrder】
SendDlgItemMessage(hDlg,IDC_EDIT1,EM_SETLIMITTEXT,(WPARAM)8,(LPARAM)0); if (GetDlgCtrlID((HWND) wParam) != IDC_EDIT1)
{
error = GetDlgItem(hDlg,IDC_EDIT1);
SetFocus(error);
return FALSE;
}
return(TRUE);
我已经使用了SetFocus函数,没有效果。 ww51xh: “SetFocus()自己给它设置窗口”
如何设置?请原谅,我刚刚学习VC。 pomelowu:
我在调用的时候,SetFocus(error);返回值为0我昨天仔细研究了一下,的确与tab stop属性有关,目前还不清楚如何改动,先多谢各位指导!
谢谢,真的好用。xuzheng318(forever C++):
GetDlgCtrlID((HWND) wParam);获得了句柄
setfocus();返回值为0pomelowu(羽战士):
我刚刚试过你的代码,果然好用。谢了。谢谢各位的帮助!
if (GetDlgCtrlID((HWND) wParam) != IDC_EDIT1)
{
error = GetDlgItem(hDlg,IDC_EDIT1);
SetFocus(error);
return FALSE;
}
return(TRUE);
如果执行SetFocus(error);return FALSE;那么return(TRUE);语句将不被执行,
那么就违反了WM_INITDIALOG:返回值必须为TRUE的规定了。我试过把上面的语句
该为如下写法:
case WM_INITDIALOG:
SendDlgItemMessage(hDlg,IDC_EDIT1,EM_SETLIMITTEXT,(WPARAM)8,(LPARAM)0); //if (GetDlgCtrlID((HWND) wParam) != IDC_EDIT1)
//{
error = GetDlgItem(hDlg,IDC_EDIT1);
SetFocus(error);
return FALSE;
//}
//return(TRUE);
键盘焦点也可以设置到EDIT控件上。这种使用方法标准吗