LoginSignup
0
0

More than 5 years have passed since last update.

Windowsアプリケーション MessageBoxAでの漢字の文字化けについて

Last updated at Posted at 2013-06-01

MinGWとEmEditorでディレクトリやファイルを検索するWindowsアプリケーションを作成しています。
Shift-JISで保存し「g++ main.cpp -o main -mwindows」で保存して実行すると

qita
MSG("列挙完了, 表示します.", "成功");

の「表示」の部
分が文字化けします。
マルチバイトも文字になっていないのかと思い、MSGマクロにTEXT()を追加したのですが、やはり文字化けします。

原因は何でしょうか?

ソースコード

main.cpp
#include <windows.h>
#include <iostream>
#include <cstring>

#define MSG(m, t) { MessageBoxA(NULL, TEXT(m), TEXT(t), MB_OK); }

const int SCREEN_WIDTH  = 500;
const int SCREEN_HEIGHT = 300;

HWND g_hwnd;        // ウィンドウハンドル
HINSTANCE g_hinst;  // インスタンスハンドル

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
    WNDCLASS wc;

    wc.style            = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = WndProc;
    wc.cbClsExtra       = wc.cbWndExtra = 0;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName     = NULL;
    wc.lpszClassName    = TEXT("Windows");

    if (!RegisterClass(&wc)) {
        MSG("クラスの登録失敗", "エラー");
        return -1;
    }

    g_hinst = hInstance;

    g_hwnd = CreateWindowA(
            TEXT("Windows"), TEXT("Music Search"),
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT,
            (int)SCREEN_WIDTH, (int)SCREEN_HEIGHT,
            NULL, NULL, hInstance, NULL
    );

    if (g_hwnd == NULL) {
        MSG("ウィンドウ作成失敗", "エラー");
        return -1;
    }

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

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    char buf[10000];

    static HANDLE s_handle;

    WIN32_FIND_DATA find;

    switch (msg) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_LBUTTONDOWN:
        memset(buf, 0, sizeof(buf));    // バッファ初期化

        s_handle = FindFirstFile("*", &find);

        if (s_handle!=INVALID_HANDLE_VALUE) {
            // FindFirstFile() によって得られたファイル情報を取得する
            if (find.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) {   // ディレクトリチェック
                strcat(buf, "【ディレクトリ】");
            }
            else {                                                  // ファイル
                strcat(buf, "【ファイル】");
            }

            strcat(buf, find.cFileName);
            strcat(buf, "\n");

            // 検索対象のファイルがあるまでループ
            while (1) {
                if (FindNextFile(s_handle, &find)) {
                    if (find.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) {   // ディレクトリェック
                        strcat(buf, "【ディレクトリ】");
                    }
                    else {                                                  // ファイル
                        strcat(buf, "【ファイル】");
                    }

                    strcat(buf, find.cFileName);
                    strcat(buf, "\n");
                }
                else {
                    if (GetLastError()==ERROR_NO_MORE_FILES) {
                        // ファイルが無い場合は終了
                        MSG("列挙完了, 表示します.", "成功");
                        MSG(buf, "成功");
                        FindClose(s_handle);
                        return 0;
                    }
                    else {
                        MSG("何らかのエラーが発生しました.", "エラー");
                        FindClose(s_handle);
                        return -1;
                    }
                }
            }
        }
        else {
            MSG("FindFirstFile() で不明なエラーが発生しました.", "エラー");
            return -1;
        }

        return 0;
    default:
        return DefWindowProc(hWnd, msg, wp, lp);
    }
}
0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0