#12
by かずま » 5年前
mbstowcs を使うためには、setlocale が必要です。
コード:
#include <windows.h>
#include <stdio.h> // fopen_s, fclose, fgets
#include <stdlib.h> // mbstowcs
#include <string.h> // strchr
#include <locale.h> // setlocale
wchar_t wbuf2[100];
size_t n2;
void onCreate(HWND hwnd)
{
FILE *fp2;
if (fopen_s(&fp2, "test.txt", "r")) return;
char str1[100];
fgets(str1, 100, fp2);
// char str2[100];
// fgets(str2, 100, fp2);
fclose(fp2);
char *p = strchr(str1, '\n');
if (p) *p = '\0'; // erase '\n'
n2 = mbstowcs(wbuf2 , str1 ,100);
}
void onPaint(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 30, 80, wbuf2, n2);
EndPaint(hwnd, &ps);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_CREATE: onCreate(hwnd); break;
case WM_PAINT: onPaint(hwnd); break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, msg, wp, lp);
}
return 0;
}
int WINAPI WinMain(HINSTANCE h, HINSTANCE p, LPSTR cl, int cs)
{
setlocale(LC_ALL, ""); // for mbstowcs
WNDCLASS wcl = { 0, WindowProc, 0, 0, h, NULL,
LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1), NULL, L"wc" };
if (!RegisterClass(&wcl)) return FALSE;
if (!CreateWindowEx(WS_EX_COMPOSITED, L"wc", L"Title",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
80, 60, 640, 480, NULL, NULL, h, NULL)) return FALSE;
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
mbstowcs を使うためには、setlocale が必要です。
[code]
#include <windows.h>
#include <stdio.h> // fopen_s, fclose, fgets
#include <stdlib.h> // mbstowcs
#include <string.h> // strchr
#include <locale.h> // setlocale
wchar_t wbuf2[100];
size_t n2;
void onCreate(HWND hwnd)
{
FILE *fp2;
if (fopen_s(&fp2, "test.txt", "r")) return;
char str1[100];
fgets(str1, 100, fp2);
// char str2[100];
// fgets(str2, 100, fp2);
fclose(fp2);
char *p = strchr(str1, '\n');
if (p) *p = '\0'; // erase '\n'
n2 = mbstowcs(wbuf2 , str1 ,100);
}
void onPaint(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 30, 80, wbuf2, n2);
EndPaint(hwnd, &ps);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_CREATE: onCreate(hwnd); break;
case WM_PAINT: onPaint(hwnd); break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, msg, wp, lp);
}
return 0;
}
int WINAPI WinMain(HINSTANCE h, HINSTANCE p, LPSTR cl, int cs)
{
setlocale(LC_ALL, ""); // for mbstowcs
WNDCLASS wcl = { 0, WindowProc, 0, 0, h, NULL,
LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1), NULL, L"wc" };
if (!RegisterClass(&wcl)) return FALSE;
if (!CreateWindowEx(WS_EX_COMPOSITED, L"wc", L"Title",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
80, 60, 640, 480, NULL, NULL, h, NULL)) return FALSE;
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
[/code]