/*这是我编的一个在Windows系统下运行的小程序。欢迎大家看看,发表发表您的高见!*/
#include <windows.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#define BIT(b,i) (((b)>>(i))&0x01)
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void IntToString(int n, char *str);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
char screen[8][128];
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;*/
wincl.hbrBackground = (HBRUSH) GetStockObject((WHITE_BRUSH));
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
0, /* Windows decides the position */
0, /* where the window ends up on the screen */
648, /* The programs width */
349, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
void Draw(HWND hwnd, HDC hdc)
{
RECT rc,r;
int x,y;
int i,j;
HBRUSH hbrush;
HPEN hpen, hpenblack;
char str[10]={0};
hpen = CreatePen (PS_SOLID, 0, RGB(255,0,0)) ;
hpenblack=CreatePen (PS_SOLID, 0, RGB(0,0,0)) ;
r.left=0;
r.top=0;
r.right=640;
r.bottom=40;
GetClientRect(hwnd,&rc);
IntToString(rc.right,str);
DrawText (hdc, str, -1, &r,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
r.left=0;
r.top=40;
r.right=640;
r.bottom=80;
IntToString(rc.bottom,str);
DrawText (hdc, str, -1, &r,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
for (x=0; x<=rc.right; x+=5)
{
if (x%40==0)
{
SelectObject(hdc, hpen);
MoveToEx (hdc, x, 0, NULL) ;
LineTo(hdc, x, rc.bottom) ;
}
else
{
SelectObject(hdc, hpenblack);
MoveToEx(hdc, x, 0, NULL) ;
LineTo(hdc, x, rc.bottom) ;
}
}
for (y=0; y<=rc.bottom; y+=5)
{
if (y%40==0)
{
SelectObject(hdc, hpen);
MoveToEx(hdc, 0, y, NULL) ;
LineTo(hdc, rc.right, y) ;
}
else
{
SelectObject(hdc,hpenblack);
MoveToEx(hdc, 0, y, NULL) ;
LineTo(hdc, rc.right, y) ;
}
}
hbrush= CreateSolidBrush(255);
for (y=0;y<8;y++)
{
for (x=0;x<128;x++)
{
for (i=0;i<8;i++)
{
if (BIT(screen[y][x],i)==1)
{
rc.left=x*5;
rc.top=(y*8+i)*5;
rc.right=rc.left+5;
rc.bottom=rc.top+5;
FillRect(hdc, &rc, hbrush);
}
}
}
}
}
void IntToString(int n, char *str)
{
int i=0;
while (n>0)
{
str[i++]=n%10+'0';
n=(int)n/10;
}
strrev(str);
}
int write0(char b, int w)
{
return (~(int)pow(2,(w)))&(b);
}
int write1(char b, int w)
{
return (int)(pow(2,(w)))|(b);
}
void StoreData(int x, int y, int data)
{
if (data==0)
screen[y/8][x]=(char)write0(screen[y/8][x],y%8);
else
screen[y/8][x]=(char)write1(screen[y/8][x],y%8);
}
void writeToFile()
{
FILE *fp;
int i, j;
if ((fp=fopen("\\bitmap.txt","w"))==NULL)
{
return ;
}
for (i=0;i<8;i++)
{
for (j=0;j<128;j++)
{
/*fprintf(fp,"%c,",screen[i][j]);*/
fputc(screen[i][j],fp);
}
}
fclose(fp);
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
int myx,myy;
int tmp;
switch (message) /* handle the messages */
{
case WM_LBUTTONDOWN:
tmp=LOWORD(lParam);/* x ordination */
myx=tmp-tmp%5;
tmp=HIWORD(lParam);/* y ordination */
myy=tmp-tmp%5;
/*result[myy/5][myx/5]=1;*/
StoreData(myx/5,myy/5,1);
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_MOUSEMOVE:
if (wParam & MK_LBUTTON)
{
tmp=LOWORD(lParam);/* x ordination */
myx=tmp-tmp%5;
tmp=HIWORD(lParam);/* y ordination */
myy=tmp-tmp%5;
/*result[myy/5][myx/5]=1;*/
StoreData(myx/5,myy/5,1);
InvalidateRect(hwnd, NULL, FALSE);
}
break;
case WM_LBUTTONDBLCLK:
tmp=LOWORD(lParam);/* x ordination */
myx=tmp-tmp%5;
tmp=HIWORD(lParam);/* y ordination */
myy=tmp-tmp%5;
/*result[myy/5][myx/5]=0;*/
StoreData(myx/5,myy/5,0);
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_PAINT:
hdc=BeginPaint(hwnd, &ps);
Draw(hwnd,hdc);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
writeToFile();
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}