任何人都可以提供一个如何使用Windows ShutdownBlockReasonCreate的示例 [英] Can anyone provide an example of how to use windows ShutdownBlockReasonCreate

查看:1298
本文介绍了任何人都可以提供一个如何使用Windows ShutdownBlockReasonCreate的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提供一个简单的例子 ShutdownBlockReasonCreate ?我一直在试图通过MSDN页面,但我不明白它,我真的很累了,不得不关闭我的电脑,每次我尝试和测试它,它不工作。如果有人是熟悉的,可以提供一个简洁的例子,将是真棒!

Can anyone provide a simple clear example of ShutdownBlockReasonCreate? I've been trying to figure it out through the MSDN pages but I'm not understanding it, and I'm getting really tired of having to turn off my computer every time I try and test it and it doesn't work. If someone is familiar and can provide a concise example that would be awesome!

这是我到目前为止所做的,但我'

This is what I have so far, but I do'

#include <Windows.h>
#include <iostream>
std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

int main(int argc, char* argv[])
{
    if (ShutdownBlockReasonCreate(GetForegroundWindow(), s2ws("TEST").c_str()) != 0)
        std::cout << "Success" << std::endl;
    else
        std::cout << "Failure" << std::endl;

    while (1) 
    {
        Sleep(1000);
        std::cout << "Testing..." << std::endl;
    }
}


推荐答案

在Windows 7中为我工作。你也可以在控制台程序中如果你喜欢,这是一个更简单的例子来写。您应该添加错误检查,我没有保持短暂。

This works for me in Windows 7. You could also do it in a console program if you liked, this was a simpler example to write. You should add error checking, I didn't to keep it short.

#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

HINSTANCE hInst;
HWND hWnd;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    switch(message)
    {
        case WM_QUERYENDSESSION:
            return FALSE;
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
            break;
        case WM_CREATE:
            ShutdownBlockReasonCreate(hWnd, L"Don't do it!");
            break;
        case WM_DESTROY:
            ShutdownBlockReasonDestroy(hWnd);
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = L"TestClass";
    wcex.hIconSm = NULL;
    return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance;
    RECT sz = {0, 0, 512, 512};
    AdjustWindowRect(&sz, WS_OVERLAPPEDWINDOW, TRUE);
    hWnd = CreateWindow(L"TestClass", L"Test Window", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, sz.right - sz.left, sz.bottom - sz.top,
        NULL, NULL, hInstance, NULL);
    if(!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    return TRUE;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    MyRegisterClass(hInstance);
    if(!InitInstance(hInstance, nCmdShow))
    {
        return FALSE;
    }

    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

控制台版本不需要大不相同,创建一个窗口并运行消息泵,所以我不知道什么它通过一个GUI应用程序购买你。显然,如果你想在任一版本中工作,你很可能最终需要一个单独的线程为窗口或你的工作。

A console version doesn't need to be drastically different, although you do need to create a window and run a message pump so I'm not sure what it buys you over a GUI app. Obviously if you want to do work in either version you'll most likely end up needing a separate thread for the window or for your work.

这里是一个最简单的例子控制台程序我想出了。你会注意到,除了ConsoleCtrlHandler,它几乎相同,以拦截各种方式关闭控制台窗口和杀死GUI窗口。它也预计是一个MBCS项目,因为我不想在我的测试控制台项目中更改它。

Here's the simplest example of a console program I came up with. You'll notice it's almost identical other than the ConsoleCtrlHandler to intercept the various ways of closing the console window and killing the GUI window instead. It's also expected to be a MBCS project since I didn't want to change it in my test console project.

#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

HINSTANCE hInst;
HWND hWnd;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    switch(message)
    {
        case WM_QUERYENDSESSION:
            return FALSE;
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
            break;
        case WM_CREATE:
            ShutdownBlockReasonCreate(hWnd, L"Don't do it!");
            break;
        case WM_DESTROY:
            ShutdownBlockReasonDestroy(hWnd);
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "TestClass";
    wcex.hIconSm = NULL;
    return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance;
    RECT sz = {0, 0, 512, 512};
    AdjustWindowRect(&sz, WS_OVERLAPPEDWINDOW, TRUE);
    hWnd = CreateWindow("TestClass", "Test Window", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, sz.right - sz.left, sz.bottom - sz.top,
        NULL, NULL, hInstance, NULL);
    if(!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    return TRUE;
}

BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType)
{
    if(dwCtrlType == CTRL_C_EVENT ||
        dwCtrlType == CTRL_BREAK_EVENT ||
        dwCtrlType == CTRL_CLOSE_EVENT)
    {
        SendMessage(hWnd, WM_CLOSE, 0, 0);
        return TRUE;
    }
    return FALSE;
}

int main()
{
    SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);

    HINSTANCE hInstance = GetModuleHandle(NULL);

    MyRegisterClass(hInstance);
    if(!InitInstance(hInstance, SW_HIDE))
    {
        return FALSE;
    }

    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

这篇关于任何人都可以提供一个如何使用Windows ShutdownBlockReasonCreate的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆