windows程式設計 Unicode和多位元組
阿新 • • 發佈:2018-12-25
Unicode和多位元組 Unicode是寬字元 多位元組是窄字元
型別 | 變數型別 | 初始化方式 |
Unicode | LPWSTR | L"string" |
多位元組 | LPSTR | "string" |
根據開發環境的設定自動適應 | LPTSTR | TEXT("string") |
程式碼演示
#include <windows.h> int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow ) { //定義LPWSTR 型別的寬字串 LPWSTR szUnicode = L"This is a Unicode String;嘿嘿"; //定義LPSTR 型別的窄字串 LPSTR szMutliByte = "This is not a Unicode String;哈哈"; //定義LPTSTR 型別的自適用字串 LPTSTR szString = TEXT("This string is Unicode or not depends on the option.呵呵"); //使用W版本的API函式,以寬字串為引數MessageBoxW(NULL, szUnicode, L"<字元編碼1>", MB_OK); //使用A版本的API函式,以窄字串為引數 MessageBoxA(NULL, szMutliByte, "<字元編碼2>", MB_OK); //根據編譯條件自動選擇A版本火W版本的API函式,採用相適應的字串型別為引數 MessageBox(NULL, szString, TEXT("<字元編碼3>"), MB_OK); return 0; }