使用C++语言编写一个Windows的登陆界面,并实现登陆用户名和密码的记录
时间: 2024-06-09 17:07:59 浏览: 150
下面是一个简单的登陆界面的代码示例,使用C++和Win32 API编写:
```cpp
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Login");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Login"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit1, hwndEdit2, hwndButton;
static TCHAR szUsername[32], szPassword[32];
switch (message)
{
case WM_CREATE:
hwndEdit1 = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
100, 50, 150, 20, hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hwndEdit2 = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_PASSWORD | ES_AUTOHSCROLL,
100, 80, 150, 20, hwnd, (HMENU)2, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hwndButton = CreateWindow(TEXT("button"), TEXT("Login"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
130, 120, 80, 30, hwnd, (HMENU)3, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 3: // Login button clicked
GetWindowText(hwndEdit1, szUsername, 32);
GetWindowText(hwndEdit2, szPassword, 32);
// TODO: Check username and password and do something
MessageBox(hwnd, TEXT("Username and password saved!"), TEXT("Login"), MB_OK);
return 0;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
```
这个登陆界面包括了两个文本框和一个“Login”按钮。当用户点击“Login”按钮时,程序会获取文本框中的用户名和密码,然后弹出一个消息框来提示用户用户名和密码已经记录下来了。在实际应用中,你需要将这些数据保存到一个文件或者数据库中,然后在进行登陆验证时读取这些数据。
阅读全文