Win32程序更改笔记本电脑刷新率-崩溃

问题描述

因此我正在快速制作Win32应用程序。基本上应该根据笔记本电脑是否插入而在60到144 Hz之间更改刷新率。当我启动该应用程序时,GUI会启动但会崩溃。我将在下面发布代码并进行调试。

#ifndef UNICODE
#define UNICODE
#define UNICODE_WAS_UNDEFINED
#endif

#include <Windows.h>

#ifdef UNICODE_WAS_UNDEFINED
#undef UNICODE
#endif
#include <iostream>

using namespace std;

BOOL checked = FALSE;

LRESULT CALLBACK WndProc(HWND,UINT,WParaM,LParaM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdshow) {

    MSG msg;
    WNDCLASS wc = { 0 };
    wc.lpszClassName = L"autoRefresh";
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(0,IDC_ARROW);


    RegisterClass(&wc);
    HWND hWnd = CreateWindowW(wc.lpszClassName,L"Refresh Changer",WS_OVERLAPPEDWINDOW | WS_VISIBLE,860,540,500,150,hInstance,0);

    while (GetMessage(&msg,NULL,0)) {
        TranslateMessage(&msg);
        dispatchMessage(&msg);
    }

    RegisterPowerSettingNotification(hWnd,&GUID_ACDC_POWER_SOURCE,DEVICE_NOTIFY_WINDOW_HANDLE);

    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WParaM wParam,LParaM lParam) {
    DEVMODE dm;
    ZeroMemory(&dm,sizeof(dm));
    dm.dmSize = sizeof(dm);

    switch (msg) {
    case WM_CREATE: {
        CreateWindow(TEXT("button"),TEXT("Enable automatic refresh rate changer (Will run at startup)"),WS_VISIBLE | WS_CHILD | BS_CHECKBox,20,35,hwnd,(HMENU)1,((LPCREATESTRUCT)lParam)->hInstance,NULL);
        CheckDlgButton(hwnd,1,BST_UNCHECKED);
        break;
    }

    case WM_COMMAND: {
        OutputDebugStringW(L"WM_COMMAND case\n");
        checked = IsDlgButtonChecked(hwnd,1);
        if (checked) {
            OutputDebugStringW(L"Box unchecked\n");
            CheckDlgButton(hwnd,BST_UNCHECKED);
            checked = FALSE;
        }
        else {
            OutputDebugStringW(L"Box checked\n");
            CheckDlgButton(hwnd,BST_CHECKED);
            checked = TRUE;
        }
        break;
    }

    case WM_POWERbroADCAST: {
        OutputDebugStringW(L"WM_broADCAST case\n");
        if (checked) {
            OutputDebugStringW(L"WM_broADCAST checked is TRUE\n");
            //std::wstring progPath = L"C:\\Users\\user\\AppData\\Roaming\\Microsoft\\Windows\\RefreshChanger.exe";
            //HKEY hkey = NULL;
            //LONG createStatus = RegCreateKey(HKEY_CURRENT_USER,L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey); //Creates a key       
            //LONG status = RegSetValueEx(hkey,L"RefreshChanger",REG_SZ,(BYTE*)progPath.c_str(),(progPath.size() + 1) * sizeof(wchar_t));

            SYstem_POWER_STATUS powerStatus;
            GetSystemPowerStatus(&powerStatus);
            if (powerStatus.ACLinestatus == 1) {
                OutputDebugStringW(L"Refresh rate before changing: " + dm.dmdisplayFrequency);
                dm.dmdisplayFrequency = 144;
                LONG ret = ChangedisplaySettingsEx(NULL,&dm,NULL);
                OutputDebugStringW(L"ChangedisplaySettingsEx returned " + ret);
                if (0 != EnumdisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&dm)) {
                    OutputDebugStringW(L"Refresh rate after changing: " + dm.dmdisplayFrequency);
                }
                switch (ret) {
                case disP_CHANGE_SUCCESSFUL:
                    OutputDebugStringW(L"display successfully changed\n");
                    break;
                case disP_CHANGE_BADDUALVIEW:
                    OutputDebugStringW(L"The settings change was unsuccessful because the system is DualView capable\n");
                    break;
                case disP_CHANGE_BADFLAGS:
                    OutputDebugStringW(L"An invalid set of flags was passed in.\n");
                    break;
                case disP_CHANGE_BADMODE:
                    OutputDebugStringW(L"The graphics mode is not supported.\n");
                    break;
                case disP_CHANGE_BADParaM:
                    OutputDebugStringW(L"An invalid parameter was passed in. This can include an invalid flag or combination of flags.\n");
                    break;
                case disP_CHANGE_Failed:
                    OutputDebugStringW(L"The display driver Failed the specified graphics mode.\n");
                    break;
                case disP_CHANGE_NOTUPDATED:
                    OutputDebugStringW(L"Unable to write settings to the registry.\n");
                    break;
                case disP_CHANGE_RESTART:
                    OutputDebugStringW(L"The computer must be restarted for the graphics mode to work.\n");
                    break;
                }//switch

            }
            else {
                OutputDebugStringW(L"WM_broADCAST checked is FALSE\n");
                OutputDebugStringW(L"Refresh rate before changing: " + dm.dmdisplayFrequency);
                dm.dmdisplayFrequency = 60;
                LONG ret = ChangedisplaySettingsEx(NULL,&dm)) {
                    OutputDebugStringW(L"Refresh rate after changing: " + dm.dmdisplayFrequency);
                }
                switch (ret) {
                case disP_CHANGE_SUCCESSFUL:
                    OutputDebugStringW(L"display successfully changed\n");
                    break;
                case disP_CHANGE_BADDUALVIEW:
                    OutputDebugStringW(L"The settings change was unsuccessful because the system is DualView capable\n");
                    break;
                case disP_CHANGE_BADFLAGS:
                    OutputDebugStringW(L"An invalid set of flags was passed in.\n");
                    break;
                case disP_CHANGE_BADMODE:
                    OutputDebugStringW(L"The graphics mode is not supported.\n");
                    break;
                case disP_CHANGE_BADParaM:
                    OutputDebugStringW(L"An invalid parameter was passed in. This can include an invalid flag or combination of flags.\n");
                    break;
                case disP_CHANGE_Failed:
                    OutputDebugStringW(L"The display driver Failed the specified graphics mode.\n");
                    break;
                case disP_CHANGE_NOTUPDATED:
                    OutputDebugStringW(L"Unable to write settings to the registry.\n");
                    break;
                case disP_CHANGE_RESTART:
                    OutputDebugStringW(L"The computer must be restarted for the graphics mode to work.\n");
                }//switch
            }//if/else
        }//if checked
    }//case

    case WM_DESTROY: {
        OutputDebugStringW(L"WM_DESTROY case\n");
        PostQuitMessage(0);
        break;
    }

    }

    return DefWindowProc(hwnd,msg,wParam,lParam);
}

这是调试控制台的屏幕截图:

Debug console

拔下充电器时,GUI崩溃。它会从拔掉电源开始运行,但是当我插入充电器时也会崩溃。即使我没有单击窗口顶部的X,刷新率也不会更改,并且会调用WM_DESTROY。我是Win32的超级新手,所以如果我错过了一些超级简单的内容,我们深感抱歉。我的背景主要是Android开发。感谢您的帮助!

解决方法

您不会break脱离外部开关/外壳,因此会陷入WM_DESTROY外壳。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...