无标题界面跳转以及倒计时
立即下载
资源介绍:
啦啦啦啦啦啦啦
#include "logindialog.h"
#include "ui_logindialog.h"
#include
#include "mainwindow.h"
#include
#include
LoginDialog::LoginDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::LoginDialog)
{
ui->setupUi(this);
}
LoginDialog::~LoginDialog()
{
delete ui;
}
void LoginDialog::on_pushButton_login_clicked()
{
QString userName = ui->lineEdit_username->text().trimmed();
QString passWord = ui->lineEdit_password->text().trimmed();
QString errorString;
if (loginToWindows(userName, passWord, errorString))
{
this->close();
m_MainWindow = new MainWindow;
m_MainWindow->show();
}
else
{
QMessageBox::critical(this, "error", errorString);
}
}
void LoginDialog::on_pushButton_exit_clicked()
{
qApp->quit();
}
bool LoginDialog::loginToWindows(const QString &username, const QString &password, QString &errorInfo)
{
// 将QString转换为wchar_t*
wchar_t *usernameW = new wchar_t[username.length() + 1];
wchar_t *passwordW = new wchar_t[password.length() + 1];
username.toWCharArray(usernameW);
password.toWCharArray(passwordW);
bool loginResult = true;
// 调用LogonUser API
HANDLE token;
if (!LogonUserW(usernameW, NULL, passwordW,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &token))
{
DWORD errorCode = GetLastError();
// 使用FormatMessage获取错误描述
LPWSTR messageBuffer = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, // 指向错误信息的地址,NULL表示使用系统的最后一个错误信息
errorCode, // 错误代码
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // 消息语言
(LPWSTR)&messageBuffer, // 指向缓冲区的指针
0, // 缓冲区大小
NULL // 指向Arguments数组的指针
);
// 检查是否成功获取错误消息
if (messageBuffer)
{
errorInfo = QString::fromWCharArray(messageBuffer); // 将获取的错误消息显示给用户
LocalFree(messageBuffer); // 释放由FormatMessage分配的内存
}
else
{
errorInfo = "unknow error!"; // 如果FormatMessage失败,显示一个通用的错误消息
}
CloseHandle(token);
loginResult = false;
}
delete[] usernameW;
delete[] passwordW;
return true;
}