中级会员
- 积分
- 257
- 威望
- 146
- 贡献
- 67
- 兑换币
- 80
- 注册时间
- 2013-3-15
- 在线时间
- 22 小时
- 毕业学校
- USTC
|
#include <windows.h>
//#include <debug.h>
#include <math.h>
//#include <stdio.h>
#define NFIELD 2000
#define NHEIGHT 50
#define NWIDTHBYTES 16
#define NWIDTH 128//16*8=128
POINT point,point1;
FLOAT SweepAngle;
UINT ntimer;
RECT rect1;
TCHAR sztimer[10]={0};
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void fnInit(void);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
fnInit();
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("GDIDraw"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT,// initial x position
CW_USEDEFAULT,// initial y position
800,// initial x size
600,// initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void fnInit(void){
point.x=0;point.y=0;point1.x=0;point1.y=0;
SweepAngle=0;
ntimer=0;
rect1.bottom=20;
rect1.left=20;
rect1.right=20;
rect1.top=20;
}
void fnDrawBezier(HDC hdc,POINT *apt){
PolyBezier(hdc,apt,4);
MoveToEx(hdc,apt[0].x,apt[0].y,NULL);
LineTo(hdc,apt[1].x,apt[1].y);
MoveToEx(hdc,apt[2].x,apt[2].y,NULL);
LineTo(hdc,apt[3].x,apt[3].y);
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
INT iLength,ix,iy,i,j,k;
static int cxClient,cyClient;
static int cxPerRect;
static INT cxChar,cyChar;
//FILE *pFile=NULL;
OFSTRUCT OfStruct;
HFILE hFile=NULL;
DWORD w;
static byte arrMovie[NFIELD][NHEIGHT][NWIDTHBYTES];
static byte arrInfo[512];
TCHAR szBuffer[100];
static int curField=0;
PAINTSTRUCT ps ;
TEXTMETRIC tm;
RECT rect;
POINT aptsin[1000];
static POINT apt[4];
GetClientRect(hwnd,&rect);
switch (message)
{
case WM_CREATE:
// PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
SetTimer(hwnd,1,1000,NULL);
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);//得到设备字体
cxChar=tm.tmAveCharWidth;//作为宽度
cyChar=tm.tmHeight+tm.tmExternalLeading;//作为高度
ReleaseDC(hwnd,hdc);
//读文件放到大数组里面
/*if(!(pFile=fopen("datatest.hex","r")))exit(1);*/
// fseek(pFile,512,SEEK_SET);
hFile=OpenFile("data.hex",(LPOFSTRUCT)&OfStruct,OF_READ);
if(!hFile)exit(1);
INT nSectorPerField,nBytesPerField;
nSectorPerField=NHEIGHT*NWIDTHBYTES/512+1;
nBytesPerField=nSectorPerField*512;
if(!ReadFile((HANDLE)hFile,(LPVOID)arrInfo,512,&w,NULL))exit(2);
for(i=0;i<NFIELD;i++){
//fread((byte *)arrMovie[i],nBytesPerField,nBytesPerField,pFile);
if(!ReadFile((HANDLE)hFile,(LPVOID)arrMovie[i],NWIDTHBYTES*NHEIGHT,&w,NULL))exit(2);
//if(!ReadFile((HANDLE)hFile,(LPVOID)arrInfo,nBytesPerField-NWIDTHBYTES*NHEIGHT,&w,NULL))exit(3);
SetFilePointer((HANDLE)hFile,nBytesPerField-NWIDTHBYTES*NHEIGHT,NULL,FILE_CURRENT);
}
CloseHandle((HANDLE)hFile);
OutputDebugStr("Completed read file");
return 0 ;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
cxPerRect=cxClient/NWIDTH-1;
// apt[0].x=cxClient/4;
// apt[0].y=cyClient/2;
// apt[1].x=cxClient/2;
// apt[1].y=cyClient/4;
// apt[2].x=cxClient/2;
// apt[2].y=3*cyClient/4;
// apt[3].x=3*cxClient/4;
// apt[3].y=cyClient/2;
return 0;
// case WM_LBUTTONDOWN:
// case WM_RBUTTONDOWN:
// case WM_MOUSEMOVE:
// if(wParam & MK_LBUTTON || wParam & MK_RBUTTON){
// hdc=GetDC(hwnd);
// SelectObject(hdc,GetStockObject(WHITE_PEN));
// fnDrawBezier(hdc,apt);
//
// if(wParam & MK_LBUTTON){
// apt[1].x=LOWORD(lParam);
// apt[1].y=HIWORD(lParam);
// }
// if(wParam & MK_RBUTTON){
// apt[2].x=LOWORD(lParam);
// apt[2].y=HIWORD(lParam);
// }
// SelectObject(hdc,GetStockObject(BLACK_PEN));
// fnDrawBezier(hdc,apt);
// ReleaseDC(hwnd,hdc);
// InvalidateRect(hwnd,NULL,TRUE);
// }
//
// return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
/* GetClientRect (hwnd, &rect) ;*/
// DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
// DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
POINT point_middle;
HPEN hPen,hOldPen;
HBRUSH hBrush,hOldBrush;
LOGBRUSH logBrush;
logBrush.lbColor=RGB(0xff,0,0);
logBrush.lbStyle=BS_SOLID;
logBrush.lbHatch=HS_HORIZONTAL;
hPen=CreatePen(PS_SOLID,2,RGB(128,0,0));
hBrush=CreateBrushIndirect(&logBrush);
hOldPen=(HPEN)SelectObject(hdc,hPen);
hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
point_middle.x=(rect.right+rect.left)/2;
point_middle.y=(rect.top+rect.bottom)/2;
SetTextColor(hdc,RGB(0xff,0,0));
/*SetROP2(hdc,R2_NOT);*/
/*SetBkMode(hdc,TRANSPARENT);*/
MoveToEx(hdc,point_middle.x,point_middle.y,NULL);
/* LineTo(hdc,point.x,point.y);*/
/*AngleArc(hdc,point_middle.x,point_middle.y,100,0,SweepAngle);*/
Rectangle(hdc,rect1.left,rect1.top,rect1.right,rect1.bottom );
/* Rectangle(hdc,10,10,50,50);*/
TCHAR bfcxPerRect[20];
RECT rectPerDot;
BYTE byteColor;
UINT iHozn,iVert;
// SetRect(&rectPerDot,0,0,30*cxChar,3*cyChar);
// hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
// FillRect(hdc,&rectPerDot,hBrush);
// DeleteObject(hBrush);
HRGN hRgnMap;
HRGN hRgnText;
hRgnMap=CreateRectRgn(0,3*cyChar,cxClient,cyClient);
hRgnText=CreateRectRgn(0,0,cxClient,cyClient);
SelectClipRgn(hdc,hRgnMap);
SetViewportOrgEx(hdc,0,3*cyChar,NULL);
for(iVert=0;iVert<NHEIGHT;iVert++){
SetRect(&rectPerDot,0,0,cxPerRect-1,cxPerRect-1);
OffsetRect(&rectPerDot,0,iVert*cxPerRect);
for(i=0;i<NWIDTH;i++){
OffsetRect(&rectPerDot,cxPerRect,0);
byteColor= ((arrMovie[curField][iVert][i/8]) & (1<<i%8)) ? 0:255;
hBrush=CreateSolidBrush(RGB(byteColor,byteColor,byteColor));
FillRect(hdc,&rectPerDot,hBrush);//do NOT need to select the brush to hdc
DeleteObject(hBrush);
if(i%8==0){
MoveToEx(hdc,(1+i)*cxPerRect,0,NULL);
LineTo(hdc,(1+i)*cxPerRect,cxPerRect*NHEIGHT);
}
}
}
DeleteObject(hRgnMap);
SelectClipRgn(hdc,hRgnText);
// for(ix=0;ix<rect.right;ix+=20){
// MoveToEx(hdc,ix,0,NULL);
// LineTo(hdc,ix,rect.bottom);
// }
// for(iy=0;iy<rect.bottom;iy+=20){
// MoveToEx(hdc,0,iy,NULL);
// LineTo(hdc,rect.right,iy);
// }
// int i;
// for(i=0;i<1000;i++){
// aptsin[i].x=i*rect.right/1000;
// aptsin[i].y=(int)(rect.bottom/2*(1-sin(2*3.1415926*i/1000)));
// }
// Polyline(hdc,aptsin,1000);
/*fnDrawBezier(hdc,apt);*/
SetViewportOrgEx(hdc,0,0,NULL);
TextOut(hdc,0,cyChar,szBuffer,strlen(strcat(strcpy(szBuffer,"time elapsed:"),sztimer)));
TextOut(hdc,0,2*cyChar,bfcxPerRect,wsprintf(bfcxPerRect,"cxPerRect:%d",cxPerRect));
TextOut(hdc,20*cxChar,2*cyChar,szBuffer,wsprintf(szBuffer,"Current Field:%4d",curField));
/* DrawText(hdc,TEXT(sztimer),-1,&rect,DT_SINGLELINE);*/
iLength=wsprintf(szBuffer,TEXT("It is %d,cxChar %d,cyChar %d"),399,cxChar,cyChar);//类似printf,不过是输出到字符串中。
TextOut(hdc,0,0,szBuffer,iLength);//iLength是长度
DeleteObject(hPen);
DeleteObject(hBrush);
SelectObject(hdc,hOldBrush);
SelectObject(hdc,hOldPen);
DeleteObject(hRgnText);
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_TIMER:
/*point.x+=10;*/
/*SweepAngle!=360?SweepAngle+=10:SweepAngle=0;*/
ntimer++;
_itoa_s(ntimer,sztimer,10);
InvalidateRect(hwnd,&rect,0);
return 0;
case WM_KEYDOWN:
switch(wParam){
case VK_UP:
SweepAngle+=10;
break;
case VK_DOWN:
SweepAngle-=10;
break;
case VK_ESCAPE:
PostQuitMessage(1);
break;
case VK_RIGHT:
curField++;
if(curField==NFIELD){
curField=0;
}
break;
case VK_LEFT:
curField--;
if(curField==-1){
curField=NFIELD-1;
}
break;
default:
;
}
InvalidateRect(hwnd,&rect,0);
return 0;
case WM_CHAR:
/*ntimer=0;*/
switch(wParam){
case '1':
rect1.bottom=50;rect1.left=10;rect1.right=50;rect1.top=10;
break;
case 'a':
rect1.left += 10;
break;
case 'd':
rect1.right += 10;
break;
case 'w':
rect1.top += 10;
break;
case 's':
rect1.bottom += 10;
break;
default:
;
}
InvalidateRect(hwnd,&rect,0);
return 0;
case WM_DESTROY:
KillTimer(hwnd,1);
PostQuitMessage (0) ;
wsprintf(szBuffer,"test %d",399);
OutputDebugStr(szBuffer);
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
|
|