C++ C++;WinAPI。列表框中显示的文本错误

C++ C++;WinAPI。列表框中显示的文本错误,c++,winapi,sendmessage,C++,Winapi,Sendmessage,我的任务是创建一个程序,该程序显示系统中所有窗口的描述符列表。我得到以下输出: 也许是编码错误 这是我的密码: #include <windows.h> ATOM RegMyWindowClass(HINSTANCE, LPCTSTR); HWND hListBox; HINSTANCE hin; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { WCHAR str[255]; if (GetWi

我的任务是创建一个程序,该程序显示系统中所有窗口的描述符列表。我得到以下输出:

也许是编码错误

这是我的密码:

#include <windows.h>

ATOM RegMyWindowClass(HINSTANCE, LPCTSTR);
HWND hListBox;
HINSTANCE hin;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    WCHAR str[255];
    if (GetWindowTextW(hwnd, str, 255)) {
        if (IsWindowVisible(hwnd) && (!GetWindow(hwnd, GW_OWNER)))
            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)str);
    }
    return 1;
}

LRESULT CALLBACK WndProc(
    HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_LBUTTONUP:
        MessageBox(hWnd, TEXT("Вы кликнули!"), TEXT("событие"), 0);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);  
        break;
    case WM_CREATE:
        hListBox = CreateWindow("LISTBOX", "", WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL,
            0, 0, 400, 400, hWnd, (HMENU)1111, hin, NULL);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE         hPrevInstance,
    LPSTR             lpCmdLine,
    int               nCmdShow)
{
    LPCTSTR lpzClass = TEXT("My Window Class!");

    if (!RegMyWindowClass(hInstance, lpzClass))
        return 1;

    RECT screen_rect;
    GetWindowRect(GetDesktopWindow(), &screen_rect); 
    int x = screen_rect.right / 2 - 200;
    int y = screen_rect.bottom / 2 - 200;

    HWND hWnd = CreateWindow(lpzClass, TEXT("Window"),
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, x, y, 400, 400, NULL, NULL,
        hInstance, NULL);
    ShowWindow(hWnd, SW_SHOW);
    EnumWindows(&EnumWindowsProc, 0);
    if (!hWnd) return 2;

    MSG msg = { 0 };   
    int iGetOk = 0;   
    while ((iGetOk = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (iGetOk == -1) return 3;  
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;  
}

ATOM RegMyWindowClass(HINSTANCE hInst, LPCTSTR lpzClassName)
{
    WNDCLASS wcWindowClass = { 0 };
    wcWindowClass.lpfnWndProc = (WNDPROC)WndProc;
    wcWindowClass.style = CS_HREDRAW | CS_VREDRAW;
    wcWindowClass.hInstance = hInst;
    wcWindowClass.lpszClassName = lpzClassName;
    wcWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcWindowClass.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
    return RegisterClass(&wcWindowClass); 
}
#包括
ATOM RegMyWindowClass(HINSTANCE,LPCTSTR);
HWND-hListBox;
欣欣;
BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM)
{
WCHAR-str[255];
如果(GetWindowTextW(hwnd,str,255)){
if(IsWindowVisible(hwnd)和(!GetWindow(hwnd,GW_OWNER)))
SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)str);
}
返回1;
}
LRESULT回调WndProc(
HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
开关(信息)
{
案例WM_LBUTTONUP:
消息框(hWnd,文本(“ыыыыыыы!”),0);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
案例WM_创建:
hListBox=CreateWindow(“列表框”),WS|u CHILD | WS|u VISIBLE | LBS|u NOTIFY | WS|u VSCROLL,
0,0,400,400,hWnd,(汉努)1111,hin,NULL);
打破
违约:
返回DefWindowProc(hWnd、message、wParam、lParam);
}
返回0;
}
int APIENTRY WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpCmdLine,
国际展览(nCmdShow)
{
LPCTSTR lpzClass=TEXT(“我的窗口类!”);
如果(!RegMyWindowClass(hInstance,lpzClass))
返回1;
矩形屏幕;
GetWindowRect(GetDesktopWindow(),&screen_rect);
int x=屏幕右/2-200;
int y=屏幕垂直底部/2-200;
HWND HWND=CreateWindow(lpzClass,文本(“窗口”),
WS|U重叠窗口| WS|U可见,x,y,400,400,NULL,NULL,
hInstance,NULL);
展示窗口(hWnd、SW_展示);
EnumWindows(&EnumWindowsProc,0);
如果(!hWnd)返回2;
MSG={0};
int-iGetOk=0;
while((iGetOk=GetMessage(&msg,NULL,0,0))!=0)
{
if(iGetOk==-1)返回3;
翻译信息(&msg);
发送消息(&msg);
}
返回msg.wParam;
}
ATOM RegMyWindowClass(HINSTANCE hInst,LPCTSTR lpzClassName)
{
WNDCLASS wcWindowClass={0};
wcWindowClass.lpfnWndProc=(WNDPROC)WNDPROC;
wcWindowClass.style=CS_HREDRAW | CS_VREDRAW;
wcWindowClass.hInstance=hInst;
wcWindowClass.lpszClassName=lpzClassName;
wcWindowClass.hCursor=LoadCursor(空,IDC_箭头);
wcWindowClass.hbrBackground=(HBRUSH)颜色\u应用工作区;
返回注册表类(&wcWindowClass);
}

有没有办法解决这个问题?

问题是您正在为ANSI编译项目,
TCHAR
CHAR
的别名,因此创建了一个基于ANSI的列表框,但您正在向它发送Unicode字符串。这就是为什么您在输出中看到垃圾。您需要将ANSI字符串发送到列表框,例如:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    if (IsWindowVisible(hwnd) && (!GetWindow(hwnd, GW_OWNER)))
        CHAR str[255] = {};
        if (GetWindowTextA(hwnd, str, 255)) {
            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)str);
        }
    }
    return TRUE;
}
或者:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    if (IsWindowVisible(hwnd) && (!GetWindow(hwnd, GW_OWNER))) {
        CHAR str[255] = {};
        if (IsWindowUnicode(hwnd)) {
            WCHAR wstr[255] = {}; 
            int len = GetWindowTextW(hwnd, wstr, 255);
            if (len) {
                len = WideCharToMultiByte(CP_ACP, 0, wstr, len+1, str, 255, NULL, NULL);
            }
            if (!len) {
                return TRUE;
            }
        }
        else if (!GetWindowTextA(hwnd, str, 255)) {
            return TRUE;
        }
        SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)str);
    }
    return TRUE;
}
也就是说,您混合了ANSI、Unicode和
TCHAR
API。你需要选择1种API风格,并在任何情况下都坚持使用,不要混用它们(除非你必须这样做)

由于您显示的大多数代码已经在使用
TCHAR
,因此您可以对所有内容使用
TCHAR
(尽管如此,您确实不应该这样做,因为
TCHAR
是为了向后兼容Win9x/ME,现在没有人再使用它了,只是为了帮助人们将代码迁移到Unicode。现代代码根本不应该使用
TCHAR
):

#包括
ATOM RegMyWindowClass(HINSTANCE,LPCTSTR);
HWND-hListBox;
欣欣;
BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM)
{
if(IsWindowVisible(hwnd)和(!GetWindow(hwnd,GW_OWNER))){
TCHAR-str[255];
if(GetWindowText(hwnd,str,255)){
SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)str);
}
}
返回TRUE;
}
LRESULT回调WndProc(
HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
开关(信息)
{
案例WM_LBUTTONUP:
//在这种情况下,使用TCHAR是没有意义的
MessageBoxW(hWnd,L“ыыыыыыы”,0);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
案例WM_创建:
hListBox=CreateWindow(文本(“列表框”),文本(“”),WS|u CHILD | WS|u VISIBLE | LBS|u NOTIFY | WS|VSCROLL,
0,0,400,400,hWnd,(汉努)1111,hin,NULL);
打破
违约:
返回DefWindowProc(hWnd、message、wParam、lParam);
}
返回0;
}
int APIENTRY WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpCmdLine,
国际展览(nCmdShow)
{
LPCTSTR lpzClass=TEXT(“我的窗口类!”);
如果(!RegMyWindowClass(hInstance,lpzClass))
返回1;
矩形屏幕;
GetWindowRect(GetDesktopWindow(),&screen_rect);
int x=屏幕右/2-200;
int y=屏幕垂直底部/2-200;
HWND HWND=CreateWindow(lpzClass,文本(“窗口”),
WS|U重叠窗口| WS|U可见,x,y,400,400,NULL,NULL,
hInstance,NULL);
展示窗口(hWnd、SW_展示);
EnumWindows(&EnumWindowsProc,0);
如果(!hWnd)返回2;
MSG={0};
int-iGetOk=0;
while((iGetOk=GetMessage(&msg,NULL,0,0))!=0)
{
if(iGetOk==-1)返回3;
翻译信息(&msg);
发送消息(&msg);
}
返回msg.wParam;
}
ATOM RegMyWindowClass(HINSTANCE hInst,LPCTSTR lpzClassName)
{
WNDCLASS wcWindowClass={0};
wcWindowClass.lpfnWndProc=&WndProc;
wcWindowClass.style=CS_HREDRAW | CS_VREDRAW;
wcWindowClass.hInstance=hInst;
wcWindowClass.lpszClassName=lpzClassName;
wcWindowClass.hCursor=LoadCursor(空,IDC_箭头);
wcWindowClass.hbrBackground=(HBRUSH)颜色\u应用工作区;
返回注册表类(&wcWindowClass);
}
或者,对所有内容都使用Unicode:

#include <windows.h>

ATOM RegMyWindowClass(HINSTANCE, LPCWSTR);
HWND hListBox;
HINSTANCE hin;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    if (IsWindowVisible(hwnd) && (!GetWindow(hwnd, GW_OWNER))) {
        WCHAR str[255];
        if (GetWindowTextW(hwnd, str, 255)) {
            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)str);
        }
    }
    return TRUE;
}

LRESULT CALLBACK WndProc(
    HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_LBUTTONUP:
        MessageBoxW(hWnd, L"Вы кликнули!", L"событие", 0);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);  
        break;
    case WM_CREATE:
        hListBox = CreateWindowW(L"LISTBOX", L"", WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL,
            0, 0, 400, 400, hWnd, (HMENU)1111, hin, NULL);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE         hPrevInstance,
    LPSTR             lpCmdLine,
    int               nCmdShow)
{
    LPCWSTR lpzClass = L"My Window Class!";

    if (!RegMyWindowClass(hInstance, lpzClass))
        return 1;

    RECT screen_rect;
    GetWindowRect(GetDesktopWindow(), &screen_rect); 
    int x = screen_rect.right / 2 - 200;
    int y = screen_rect.bottom / 2 - 200;

    HWND hWnd = CreateWindowW(lpzClass, L"Window",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, x, y, 400, 400, NULL, NULL,
        hInstance, NULL);
    ShowWindow(hWnd, SW_SHOW);
    EnumWindows(&EnumWindowsProc, 0);
    if (!hWnd) return 2;

    MSG msg = { 0 };   
    int iGetOk = 0;   
    while ((iGetOk = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (iGetOk == -1) return 3;  
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;  
}

ATOM RegMyWindowClass(HINSTANCE hInst, LPCWSTR lpzClassName)
{
    WNDCLASSW wcWindowClass = { 0 };
    wcWindowClass.lpfnWndProc = &WndProc;
    wcWindowClass.style = CS_HREDRAW | CS_VREDRAW;
    wcWindowClass.hInstance = hInst;
    wcWindowClass.lpszClassName = lpzClassName;
    wcWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcWindowClass.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
    return RegisterClassW(&wcWindowClass); 
}
#包括
#include <windows.h>

ATOM RegMyWindowClass(HINSTANCE, LPCWSTR);
HWND hListBox;
HINSTANCE hin;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    if (IsWindowVisible(hwnd) && (!GetWindow(hwnd, GW_OWNER))) {
        WCHAR str[255];
        if (GetWindowTextW(hwnd, str, 255)) {
            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)str);
        }
    }
    return TRUE;
}

LRESULT CALLBACK WndProc(
    HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_LBUTTONUP:
        MessageBoxW(hWnd, L"Вы кликнули!", L"событие", 0);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);  
        break;
    case WM_CREATE:
        hListBox = CreateWindowW(L"LISTBOX", L"", WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL,
            0, 0, 400, 400, hWnd, (HMENU)1111, hin, NULL);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE         hPrevInstance,
    LPSTR             lpCmdLine,
    int               nCmdShow)
{
    LPCWSTR lpzClass = L"My Window Class!";

    if (!RegMyWindowClass(hInstance, lpzClass))
        return 1;

    RECT screen_rect;
    GetWindowRect(GetDesktopWindow(), &screen_rect); 
    int x = screen_rect.right / 2 - 200;
    int y = screen_rect.bottom / 2 - 200;

    HWND hWnd = CreateWindowW(lpzClass, L"Window",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, x, y, 400, 400, NULL, NULL,
        hInstance, NULL);
    ShowWindow(hWnd, SW_SHOW);
    EnumWindows(&EnumWindowsProc, 0);
    if (!hWnd) return 2;

    MSG msg = { 0 };   
    int iGetOk = 0;   
    while ((iGetOk = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (iGetOk == -1) return 3;  
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;  
}

ATOM RegMyWindowClass(HINSTANCE hInst, LPCWSTR lpzClassName)
{
    WNDCLASSW wcWindowClass = { 0 };
    wcWindowClass.lpfnWndProc = &WndProc;
    wcWindowClass.style = CS_HREDRAW | CS_VREDRAW;
    wcWindowClass.hInstance = hInst;
    wcWindowClass.lpszClassName = lpzClassName;
    wcWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcWindowClass.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
    return RegisterClassW(&wcWindowClass); 
}
#include <windows.h>

ATOM RegMyWindowClass(HINSTANCE, LPCSTR);
HWND hListBox;
HINSTANCE hin;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    if (IsWindowVisible(hwnd) && (!GetWindow(hwnd, GW_OWNER))) {
        CHAR str[255];
        if (GetWindowTextA(hwnd, str, 255)) {
            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)str);
        }
    }
    return TRUE;
}

LRESULT CALLBACK WndProc(
    HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_LBUTTONUP:
        // this is one case where it doesn't make sense to use ANSI
        MessageBoxW(hWnd, L"Вы кликнули!", L"событие", 0);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);  
        break;
    case WM_CREATE:
        hListBox = CreateWindowA("LISTBOX", "", WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL,
            0, 0, 400, 400, hWnd, (HMENU)1111, hin, NULL);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE         hPrevInstance,
    LPSTR             lpCmdLine,
    int               nCmdShow)
{
    LPCSTR lpzClass = "My Window Class!";

    if (!RegMyWindowClass(hInstance, lpzClass))
        return 1;

    RECT screen_rect;
    GetWindowRect(GetDesktopWindow(), &screen_rect); 
    int x = screen_rect.right / 2 - 200;
    int y = screen_rect.bottom / 2 - 200;

    HWND hWnd = CreateWindowA(lpzClass, "Window",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, x, y, 400, 400, NULL, NULL,
        hInstance, NULL);
    ShowWindow(hWnd, SW_SHOW);
    EnumWindows(&EnumWindowsProc, 0);
    if (!hWnd) return 2;

    MSG msg = { 0 };   
    int iGetOk = 0;   
    while ((iGetOk = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (iGetOk == -1) return 3;  
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;  
}

ATOM RegMyWindowClass(HINSTANCE hInst, LPCSTR lpzClassName)
{
    WNDCLASSA wcWindowClass = { 0 };
    wcWindowClass.lpfnWndProc = &WndProc;
    wcWindowClass.style = CS_HREDRAW | CS_VREDRAW;
    wcWindowClass.hInstance = hInst;
    wcWindowClass.lpszClassName = lpzClassName;
    wcWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcWindowClass.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
    return RegisterClassA(&wcWindowClass); 
}