简体   繁体   English

Win32 应用程序失败

[英]Win32 Application failure

My application compiles successfully in visual studio 2019 but when i try to compile this code in Code::Blocks (Mingw64) it fails.我的应用程序在 visual studio 2019 中编译成功,但是当我尝试在 Code::Blocks (Mingw64) 中编译此代码时,它失败了。

First, i had this error message twice: error: cannot convert 'const wchar_t*' to 'LPCSTR' {aka 'const char*'}首先,我两次收到此错误消息: error: cannot convert 'const wchar_t*' to 'LPCSTR' {aka 'const char*'}

so i solved the issue by replacing this:所以我通过替换这个解决了这个问题:

 AppendMenu(HSUBMENU, MF_STRING, FILE_MENU_EXIT, L"Quit");
 AppendMenu(MENU1,  MF_POPUP, (UINT_PTR)HSUBMENU, L"FILE");

with this:有了这个:

 AppendMenu(HSUBMENU, MF_STRING, FILE_MENU_EXIT, "Quit");
 AppendMenu(MENU1,  MF_POPUP, (UINT_PTR)HSUBMENU, "FILE");

So, this issue was solved,but had still another error:所以,这个问题解决了,但是还有一个错误:

error: cannot convert 'LPSTR' {aka 'char*'} to 'LPCWSTR' {aka 'const wchar_t*'} in assignment

wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);

I' m stuck at this error我卡在了这个错误上

Here below is the original source code that compiles successfully in Visual Studio but fails in Code::Blocks.以下是在 Visual Studio 中编译成功但在 Code::Blocks 中编译失败的原始源代码。

I need help please.我需要帮助。

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "resource.h"



#define FILE_MENU_EXIT 3



LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);







void AddMenue(HWND hwnd)
{
    HMENU MENU1(0), HSUBMENU(0);
    HSUBMENU = CreateMenu();
    MENU1 = CreateMenu();

    AppendMenu(HSUBMENU, MF_STRING, FILE_MENU_EXIT, L"Quit");
    AppendMenu(MENU1,  MF_POPUP, (UINT_PTR)HSUBMENU, L"FILE");

    SetMenu(hwnd, MENU1);
}






HMENU hmenu;


int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PWSTR pCmdLine, int nCmdShow) {

    MSG msg;
    HWND hwnd;
    WNDCLASSW wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.lpszClassName = L"Window";
    wc.hInstance = hInstance;
    wc.hbrBackground = /*(HBRUSH)(COLOR_WINDOW+4);*/GetSysColorBrush(COLOR_3DFACE);
    wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);

    

    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassW(&wc);
    hwnd = CreateWindowW(wc.lpszClassName, L"Window",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, 600, 600, NULL, NULL, hInstance, NULL);





    ShowWindow(hwnd, nCmdShow);
    // pas necessaire UpdateWindow(hwnd);





    while (GetMessage(&msg, NULL, 0, 0)) {

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }


    return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {





    switch (msg) {


    

    case WM_CREATE:
        AddMenue(hwnd);
        break;


    case WM_DESTROY:

        PostQuitMessage(0);
        break;
    }


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

Some of your code is using TCHAR-based APIs without using TCHAR-based strings with them.您的某些代码使用基于 TCHAR 的 API,但没有使用基于 TCHAR 的字符串。 And some of your code is using Unicode APIs without using Unicode strings with them.并且您的一些代码使用 Unicode API 而没有使用 Unicode 字符串。 This is why you are getting conversion errors.这就是您收到转换错误的原因。

Specifically...具体来说...

This code:这段代码:

AppendMenu(HSUBMENU, MF_STRING, FILE_MENU_EXIT, L"Quit");
AppendMenu(MENU1,  MF_POPUP, (UINT_PTR)HSUBMENU, L"FILE");

Needs to be either the following, since AppendMenu() is a TCHAR -based macro:需要是以下之一,因为AppendMenu()是基于TCHAR的宏:

AppendMenu(HSUBMENU, MF_STRING, FILE_MENU_EXIT, TEXT("Quit"));
AppendMenu(MENU1,  MF_POPUP, (UINT_PTR)HSUBMENU, TEXT("FILE"));

Or this, if you want to use Unicode strings specifically:或者这个,如果你想专门使用 Unicode 字符串:

AppendMenuW(HSUBMENU, MF_STRING, FILE_MENU_EXIT, L"Quit");
AppendMenuW(MENU1,  MF_POPUP, (UINT_PTR)HSUBMENU, L"FILE");

And this code:这段代码:

wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);

Needs to be the following, since you are using the Unicode-based WNDCLASSW instead of the TCHAR-based WNDCLASS ( MAKEINTRESOURCE() is TCHAR-based):需要如下,因为您使用的是基于 Unicode 的WNDCLASSW而不是基于 TCHAR 的WNDCLASSMAKEINTRESOURCE()是基于 TCHAR 的):

wc.lpszMenuName = MAKEINTRESOURCEW(IDR_MENU1);

And this code:这段代码:

while (GetMessage(&msg, NULL, 0, 0)) {

    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

Needs to be the following, since you used the Unicode-based RegisterClassW() and CreateWindowW() instead of the TCHAR-based RegisterClass() and CreateWindow() :需要如下,因为您使用了基于 Unicode 的RegisterClassW()CreateWindowW()而不是基于 TCHAR 的RegisterClass()CreateWindow()

while (GetMessageW(&msg, NULL, 0, 0)) {

    TranslateMessage(&msg);
    DispatchMessageW(&msg);
}

You really need to pick one encoding scheme and stick with it consistently.您确实需要选择一种编码方案并始终如一地坚持下去。 Either use all TCHAR-based APIs, or all Unicode-based APIs.要么使用所有基于 TCHAR 的 API,要么使用所有基于 Unicode 的 API。 Don't mix them.不要混淆它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM