Initilizer-list无法转换为const MARGINS *

时间:2015-05-31 21:58:53

标签: c++ margins

今天我跳到Visual Studio(C ++)2013,我在很多时候使用Codeblocks但是我意识到在编译这样的代码时代码块开始失败:

//#define _WIN32_WINNT 0x0500
#include "hMain.h"
#include <windows.h>
#include <Uxtheme.h>

int Width = 800;
int Height = 600;

const MARGINS* Margin = { 0, 0, Width , Height };

char lWindowName[256] = "TEEEST";
HWND hWnd;

char tWindowName[256] = "TEEEST"; 
HWND tWnd;
RECT tSize;

MSG Message;

LRESULT CALLBACK WinProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch (Message)
    {
        case WM_PAINT:
            Render ();
            break;
        case WM_CREATE:
            DwmExtendFrameIntoClientArea(hWnd, Margin);
            break;
        case WM_COMMAND:
            if (wParam == VK_ESCAPE) PostQuitMessage(0);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
            return DefWindowProc(hWnd, Message, wParam, lParam);
            break;
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hSecInstance, LPSTR nCmdLine, INT nCmdShow)
{

    CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SetWindowToTarget, 0, 0, 0);

    WNDCLASSEX wClass;
    wClass.cbClsExtra = NULL;
    wClass.cbSize = sizeof(WNDCLASSEX);
    wClass.cbWndExtra = NULL;
    wClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0));
    wClass.hCursor = LoadCursor(0, IDC_ARROW);
    wClass.hIcon = LoadIcon(0, IDI_APPLICATION);
    wClass.hIconSm = LoadIcon(0, IDI_APPLICATION);
    wClass.hInstance = hInstance;
    wClass.lpfnWndProc = WinProc;
    wClass.lpszClassName = (LPCWSTR)lWindowName;
    wClass.lpszMenuName = (LPCWSTR)lWindowName;
    wClass.style = CS_VREDRAW | CS_HREDRAW;

    if(!RegisterClassEx(&wClass))
        exit(1);

    tWnd = FindWindow(0, (LPCWSTR)tWindowName);
    if (tWnd)
    {
        GetWindowRect(tWnd, &tSize);
        Width = tSize.right - tSize.left;
        Height = tSize.bottom - tSize.top;
        hWnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED, (LPCWSTR)lWindowName, (LPCWSTR)lWindowName, WS_POPUP, 1, 1, Width, Height, 0, 0, 0, 0);
        SetLayeredWindowAttributes(hWnd, 0, 1.0f, LWA_ALPHA);
        SetLayeredWindowAttributes(hWnd, 0, RGB(0, 0, 0), LWA_COLORKEY);
        ShowWindow( hWnd, SW_SHOW);
    }

    DirectXInit(hWnd);

    for (;;)
    {
        if (GetAsyncKeyState(VK_ESCAPE)) break;
        if(PeekMessage(&Message, hWnd, 0, 0, PM_REMOVE))
        {
            DispatchMessage(&Message);
            TranslateMessage(&Message);
        }
        Sleep(1);
    }
    return 0;
}

void SetWindowToTarget()
{
    while(true)
    {
        tWnd = FindWindow(0, (LPCWSTR)tWindowName);
        if (tWnd)
        {
            GetWindowRect(tWnd, &tSize);
            Width = tSize.right - tSize.left;
            Height = tSize.bottom - tSize.top;
            DWORD dwStyle = GetWindowLong(tWnd, GWL_STYLE);
            if(dwStyle & WS_BORDER)
            {
                tSize.top += 23;
                Height -= 23;
            }
            MoveWindow(hWnd, tSize.left, tSize.top, Width, Height, true);
        }
        else
        {
            char* ErrorMsg[125];
            MessageBox(0, L"MAL", (LPCWSTR)L"Error - Cannot find the game!", MB_OK | MB_ICONERROR);
            exit(1);
        }
        Sleep(100);
    }
}

所以,2个问题。首先,我如何修复此编译器错误:http://gyazo.com/6d7de9e0f3bad345dbbe7b9b80c90b8d和第二个问题。我意识到我必须放一个&#34; L&#34;和(LPCWSTR)在某些字符*之前,这是100%要求的吗?有没有办法避免至少(LPCWSTR)?谢谢你的阅读。

1 个答案:

答案 0 :(得分:1)

首先,

 const MARGINS* Margin = { 0, 0, Width , Height };

无效。您可能想要创建一个新的MARGINS对象并使用给定的值对其进行初始化,在这种情况下,您不希望Margin成为指针:

 const MARGINS Margin = { 0, 0, Width , Height };

其次,DwmExtendFrameIntoClientArea()需要const MARGINS*,它是指向MARGINS对象的指针。只需给它Margin的地址:

DwmExtendFrameIntoClientArea(hWnd, &Margin);

对于广泛的字符问题,我假设您正在使用项目选项中的“Unicode字符集”进行编译。在这种情况下,Windows API中的大多数字符串都是指向宽字符(wchar_t*)的指针。但是,您正在分配窄字符数组(例如lWindowName)并将它们转换为指向wchar_t的指针:

wClass.lpszClassName = (LPCWSTR)lWindowName;

这会给你各种奇怪的行为。确保您的字符串确实由宽字符组成:

wchar_t lWindowName[256] = L"TEEEST";

这将允许你放弃大多数演员阵容。