C# asked in 電腦與網際網路程式設計 · 1 decade ago

API初學TextOut的問題?

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

int wmId, wmEvent;

PAINTSTRUCT ps;

HDC hdc;

char *szStr =" 你好!!! ";

switch (message)

{

case WM_COMMAND:

wmId = LOWORD(wParam);

wmEvent = HIWORD(wParam);

// 剖析功能表選取項目:

switch (wmId)

{

case IDM_ABOUT:

DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);

break;

case IDM_EXIT:

DestroyWindow(hWnd);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

break;

case WM_PAINT:

hdc = BeginPaint(hWnd, &ps);

TextOut(hdc, 10, 10, LPCTSTR(szStr),

(int)strlen(szStr));

// TODO: 在此加入任何繪圖程式碼...

EndPaint(hWnd, &ps);

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}

程式碼如上,主要功能只是在應用視窗印出→"你好!!!"

可是執行後結果都是亂碼,請問該如何修改呢

感覺上像是unicode的問題?

我是用visual studio 2005練習的

1 Answer

Rating
  • Jerry
    Lv 4
    1 decade ago
    Favorite Answer

    看起來是 Unicode 的問題,改成下面這樣試試看吧~

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

    {

    int wmId, wmEvent;

    PAINTSTRUCT ps;

    HDC hdc;

    TCHAR *szStr =TEXT(" 你好!!! ");

    switch (message)

    {

    case WM_COMMAND:

    wmId = LOWORD(wParam);

    wmEvent = HIWORD(wParam);

    // 剖析功能表選取項目:

    switch (wmId)

    {

    case IDM_ABOUT:

    DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);

    break;

    case IDM_EXIT:

    DestroyWindow(hWnd);

    break;

    default:

    return DefWindowProc(hWnd, message, wParam, lParam);

    }

    break;

    case WM_PAINT:

    hdc = BeginPaint(hWnd, &ps);

    TextOut(hdc, 10, 10, szStr,

    (int)_tcslen(szStr));

    // TODO: 在此加入任何繪圖程式碼...

    EndPaint(hWnd, &ps);

    break;

    case WM_DESTROY:

    PostQuitMessage(0);

    break;

    default:

    return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

    }

Still have questions? Get your answers by asking now.