テキストによって色を指定し描画するプログラム
Posted: 2016年5月03日(火) 22:54
記号がかいてあるテキストファイルを読み込んで色を指定し、描画するプログラムを書いていますが、ビルドがうまくいきません。アドバイスおねがいします。
テキストには@;:,. などの記号が書いてあり、@だったら赤、;だったら青、:だったら緑、などの色を2*2ほどのピクセルで描画するプログラムです。
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define MSG(m) {\
MessageBoxA(NULL, m, NULL, MB_OK); }
//ウィンドウハンドル
HWND hwnd;
//インスタンスハンドル
HINSTANCE hinst;
//ウィンドウ横幅
#define WIDTH 500
#define HEIGHT 300
int cTab[9] =
{ 0x330000, 0xFFFFFF, 0x0000FF, 0x00FF00,
0xFF0000, 0x00FFFF, 0x0033FF, 0xFF00FF ,0x000000};
int ch;
FILE *fp;
int main(void){
char *filename = "text.txt";
if ((/*fp = */fopen_s(&fp,filename, "rb")) != 0/*NULL*/){
exit(EXIT_FAILURE);
}
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){
int c = 0;
int x = 0, x1 = 0, y = 2, y1 = 2;
HDC hBuff;
PAINTSTRUCT ps;
//RECT rect;
while ((ch = fgetc(fp)) != EOF){
switch (ch){
case '@':
c = 0;
break;
case '#':
c = 1;
break;
case '*':
c = 2;
break;
case '|':
c = 3;
break;
case '!':
c = 4;
break;
case ':':
c = 5;
break;
case ',':
c = 6;
break;
case '.':
c = 7;
break;
case ' ':
c = 8;
break;
case '\n':
c = 9;
y += 2;
y1 += 2;
break;
}
switch (msg){
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
hBuff = BeginPaint(hwnd, &ps);
//四角形を描画
HPEN hpen = CreatePen(PS_SOLID, 1, cTab[c]); //外枠の色
SelectObject(hBuff, hpen);
HBRUSH hbrush = CreateSolidBrush(cTab[c]); //四角の塗りつぶし
SelectObject(hBuff, hbrush);
Rectangle(hBuff, x, y, x1, y1);
DeleteObject(hbrush);
DeleteObject(hpen);
EndPaint(hwnd, &ps);
return 0;
}
x += 2;
x1 += 2;
} //whileとじ
return DefWindowProc(hwnd, msg, wp, lp);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = wc.hIcon = NULL;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszClassName = "test";
wc.lpszMenuName = NULL;
if (!RegisterClass(&wc)){
MSG("クラスの登録失敗");
return -1;
}
hwnd = CreateWindowA("test", "テストウィンドウ", WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
0, 0, 1800, 1000, NULL, NULL, hInstance, NULL);
if (hwnd == NULL){
MSG("ウィンドウ作成失敗");
return -1;
}
//インスタンスハンドル
hinst = hInstance;
//エラーチェック用変数
int check;
while (check = GetMessage(&msg, NULL, 0, 0)){
if (check == -1){
break;
}
DispatchMessage(&msg);
}
//クラス解放
UnregisterClass("test", hinst);
return 0;
}
テキストには@;:,. などの記号が書いてあり、@だったら赤、;だったら青、:だったら緑、などの色を2*2ほどのピクセルで描画するプログラムです。
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define MSG(m) {\
MessageBoxA(NULL, m, NULL, MB_OK); }
//ウィンドウハンドル
HWND hwnd;
//インスタンスハンドル
HINSTANCE hinst;
//ウィンドウ横幅
#define WIDTH 500
#define HEIGHT 300
int cTab[9] =
{ 0x330000, 0xFFFFFF, 0x0000FF, 0x00FF00,
0xFF0000, 0x00FFFF, 0x0033FF, 0xFF00FF ,0x000000};
int ch;
FILE *fp;
int main(void){
char *filename = "text.txt";
if ((/*fp = */fopen_s(&fp,filename, "rb")) != 0/*NULL*/){
exit(EXIT_FAILURE);
}
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){
int c = 0;
int x = 0, x1 = 0, y = 2, y1 = 2;
HDC hBuff;
PAINTSTRUCT ps;
//RECT rect;
while ((ch = fgetc(fp)) != EOF){
switch (ch){
case '@':
c = 0;
break;
case '#':
c = 1;
break;
case '*':
c = 2;
break;
case '|':
c = 3;
break;
case '!':
c = 4;
break;
case ':':
c = 5;
break;
case ',':
c = 6;
break;
case '.':
c = 7;
break;
case ' ':
c = 8;
break;
case '\n':
c = 9;
y += 2;
y1 += 2;
break;
}
switch (msg){
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
hBuff = BeginPaint(hwnd, &ps);
//四角形を描画
HPEN hpen = CreatePen(PS_SOLID, 1, cTab[c]); //外枠の色
SelectObject(hBuff, hpen);
HBRUSH hbrush = CreateSolidBrush(cTab[c]); //四角の塗りつぶし
SelectObject(hBuff, hbrush);
Rectangle(hBuff, x, y, x1, y1);
DeleteObject(hbrush);
DeleteObject(hpen);
EndPaint(hwnd, &ps);
return 0;
}
x += 2;
x1 += 2;
} //whileとじ
return DefWindowProc(hwnd, msg, wp, lp);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = wc.hIcon = NULL;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszClassName = "test";
wc.lpszMenuName = NULL;
if (!RegisterClass(&wc)){
MSG("クラスの登録失敗");
return -1;
}
hwnd = CreateWindowA("test", "テストウィンドウ", WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
0, 0, 1800, 1000, NULL, NULL, hInstance, NULL);
if (hwnd == NULL){
MSG("ウィンドウ作成失敗");
return -1;
}
//インスタンスハンドル
hinst = hInstance;
//エラーチェック用変数
int check;
while (check = GetMessage(&msg, NULL, 0, 0)){
if (check == -1){
break;
}
DispatchMessage(&msg);
}
//クラス解放
UnregisterClass("test", hinst);
return 0;
}