Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2010 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/738999 to your computer and use it in GitHub Desktop.
Save anonymous/738999 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef __GNUC__
#pragma comment(lib, "gdi32.lib") /**/
#pragma comment(lib, "user32.lib") /**/
#endif
#define N (100)
typedef struct {
int ID;
double x,y;
char Sym;
int Num;
int Next[4];
} SDt;
SDt D[N] = {{0}};
int Num = 0;
int SplitStr(char* s, char** p) {
int i,j;
for (i=0 ; s[i] ; i++) if (s[i]=='\n' || s[i]=='\r') s[i]=0;
for (i=j=0 ; s[i] ; i++){
if (j==0) {
p[j] = s;
j++;
continue;
}
if (s[i]==' ') {
s[i] = 0;
p[j] = &s[i+1];
j++;
}
if (j>=9) break;
}
return j;
}
void OnMyCreate(HWND hWnd) {
FILE* f;
char s[1000];
char* p[10];
int i = 0;
if (!(f=fopen("map.dat","r"))) return;
while (fgets(s,sizeof(s),f)) {
if (SplitStr(s,p)<9) continue;
D[i].ID = atoi (p[0]);
D[i].x = strtod(p[1],NULL);
D[i].y = strtod(p[2],NULL);
D[i].Sym = *p[3];
D[i].Num = atoi (p[4]);
D[i].Next[0] = atoi (p[5]);
D[i].Next[1] = atoi (p[6]);
D[i].Next[2] = atoi (p[7]);
D[i].Next[3] = atoi (p[8]);
i++;
if (i>=N) break;
}
Num = i;
fclose(f);
}
void OnMyPaint(HWND hWnd) {
HDC hDC;
PAINTSTRUCT ps;
int i,j,x,y,x1,y1;
char str[2] = {0};
hDC = BeginPaint(hWnd, &ps);
for (i=0 ; i<Num ; i++) {
x = D[i].x *100 +300;
y = D[i].y *100 +300;
for (j=0 ; j<D[i].Num ; j++) {
x1 = D[D[i].Next[j]-1].x *100 +300;
y1 = D[D[i].Next[j]-1].y *100 +300;
MoveToEx(hDC, x , y , NULL);
LineTo (hDC, x1, y1);
}
}
for (i=0 ; i<Num ; i++) {
x = D[i].x *100 +300;
y = D[i].y *100 +300;
str[0] = D[i].Sym;
TextOut(hDC, x-5, y-5, (LPCSTR)str, strlen(str));
}
EndPaint(hWnd, &ps);
return;
}
#define MY_CLASS_NAME "Test469Class"
#define MY_WINDOW_TITLE "Test469"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain
( HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, int nCmdShow
)
{
MSG msg;
WNDCLASSEX wcex;
HWND hWnd;
freopen("Debug.txt","w",stdout);
if(hPrevInstance == NULL) {
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = MY_CLASS_NAME;
wcex.hIconSm = 0;
if (RegisterClassEx(&wcex) == 0) return 0;
}
hWnd = CreateWindow
( MY_CLASS_NAME
, MY_WINDOW_TITLE
, WS_OVERLAPPEDWINDOW
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, (HWND)NULL
, (HMENU)NULL
, hInstance
, (LPVOID)NULL
);
if (!hWnd) return 0;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc
( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
{
switch (uMsg) {
case WM_CREATE: OnMyCreate(hWnd); break;
case WM_DESTROY: PostQuitMessage(0); break;
case WM_PAINT: OnMyPaint(hWnd); break;
default: return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment