level 1
纯C WINSDK
在XP SP3下编译通过,贴上代码
-------------------------------------------------------------------
#include "windows.h"
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")
const int Matrix_Width=300;
const int Matrix_Height=200;
typedef struct _MATRIX_COLUMN
{
BOOL bActive;
int nCounter;
int nCounterMax;
char prev;
}MATRIX_COLUMN,*PMATRIX_COLUMN;
#define GET_RANDOM(min,max) ((rand()%(int)(((max)+1)-(min)))+(min))
LPCTSTR szClassName="Matrix";
HDC hMemDC=NULL;
HBITMAP hMemBitmap=NULL;
HGDIOBJ pBitmapOld=NULL;
HGDIOBJ pFontOld=NULL;
HFONT hFont=NULL;
int nTextWidth,nTextHeight;
int nCols,nRows;
int nActiveColumns=0;
PMATRIX_COLUMN pMatrixColumns=NULL;
void Matrix_OnCreate(HWND hwnd)
{
HDC hdc;
RECT rcClient;
SetWindowLong(hwnd,GWL_STYLE,GetWindowLong(hwnd,GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME);
hdc=GetDC(hwnd);
if(hdc)
{
hMemDC=CreateCompatibleDC(hdc);
if(hMemDC)
{
TEXTMETRIC tm;
hFont=CreateFont(14,0,0,0,FW_BOLD,FALSE, FALSE, 0, ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS, "Courier");
hMemBitmap=CreateCompatibleBitmap(hdc,Matrix_Width,Matrix_Height);
pBitmapOld=SelectObject(hMemDC,hMemBitmap);
pFontOld=SelectObject(hMemDC,hFont);
GetTextMetrics(hMemDC,&tm);
nTextWidth=tm.tmAveCharWidth+8;
2010年02月24日 20点02分
1
level 1
nTextHeight= tm.tmHeight;
nCols=Matrix_Width/nTextWidth+1;
nRows=Matrix_Height/nTextHeight+1;
pMatrixColumns=(PMATRIX_COLUMN)malloc(sizeof(MATRIX_COLUMN)*nCols);
if(pMatrixColumns) RtlZeroMemory(pMatrixColumns,sizeof(MATRIX_COLUMN)*nCols);
SetTimer(hwnd,1,80,NULL);
}
ReleaseDC(hwnd,hdc);
}
GetClientRect(hwnd,&rcClient);
InvalidateRect(hwnd,&rcClient,FALSE);
}
void Matrix_OnPaint(HWND hwnd)
{
if(pBitmapOld)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hwnd,&ps);
BitBlt(hdc,0,0,Matrix_Width,Matrix_Height,hMemDC,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
}
}
void Matrix_InitBackgroundCharacters(int col)
{
const int bottomOffset = 3;
pMatrixColumns[col].nCounter = 0;
if(GET_RANDOM(1,2) == 1)
pMatrixColumns[col].nCounter=GET_RANDOM(0,nRows-bottomOffset);
pMatrixColumns[col].nCounterMax=nRows;
if(GET_RANDOM(1,2)==1)
{
pMatrixColumns[col].nCounterMax=pMatrixColumns[col].nCounter+GET_RANDOM(bottomOffset,nRows-pMatrixColumns[col].nCounter);
if(pMatrixColumns[col].nCounterMax > nRows)
pMatrixColumns[col].nCounterMax=nRows;
}
pMatrixColumns[col].prev=(GET_RANDOM(1,5)==1?' ':GET_RANDOM('a', 'z'));
}
void Matrix_DrawActiveString(HDC hdc,int row,int col,char* str,int len,COLORREF color)
{
if (len > 0)
{
SIZE size;
RECT r;
GetTextExtentPoint32(hdc,str,len,&size);
2010年02月24日 20点02分
2
level 1
r.left=col*nTextWidth,r.top=row*nTextHeight;
r.right=(col+len)*nTextWidth,r.bottom=(row+1)*nTextHeight;
if(len > 1)
r.right=r.left+size.cx;
if(len == 1 && str[0]==0x01)
{
SetBkColor(hdc,color);
ExtTextOut(hdc,0,0,ETO_OPAQUE,&r,NULL,0,NULL);
}
else
{
SetBkColor(hdc,RGB(0,0,0));
ExtTextOut(hdc,0,0,ETO_OPAQUE,&r,NULL,0,NULL);
SetTextColor(hdc,color);
TextOut(hdc,col*nTextWidth + (len>1?0:(nTextWidth - size.cx)/2),row * nTextHeight,str,len);
}
}
}
void Matrix_DrawActiveBackgroundChar(int col)
{
if (pMatrixColumns[col].prev != ' ')
pMatrixColumns[col].prev=GET_RANDOM('a', 'z');
Matrix_DrawActiveString(hMemDC,pMatrixColumns[col].nCounter,col,&pMatrixColumns[col].prev,1,RGB(0,210,0));
}
void Matrix_DrawFadedBackgroundChar(int col)
{
int row=pMatrixColumns[col].nCounter-1;
SIZE size;
GetTextExtentPoint32(hMemDC,&pMatrixColumns[col].prev,1,&size);
SetTextColor(hMemDC,RGB(0,GET_RANDOM(60,100),0));
TextOut(hMemDC,col * nTextWidth+(nTextWidth - size.cx)/2,row * nTextHeight,&pMatrixColumns[col].prev,1);
}
void Matrix_UpdateBackground()
{
int i;
if(nActiveColumns < nCols)
{
int nStartColumn,nSafetyCounter = 0;
do
{
nStartColumn = rand()%nCols;
nSafetyCounter++;
if(nSafetyCounter > nCols)
break;
2010年02月24日 20点02分
3
level 1
}
while(pMatrixColumns[nStartColumn].bActive);
if (!pMatrixColumns[nStartColumn].bActive)
{
pMatrixColumns[nStartColumn].bActive = TRUE;
Matrix_InitBackgroundCharacters(nStartColumn);
Matrix_DrawActiveBackgroundChar(nStartColumn);
pMatrixColumns[nStartColumn].nCounter++;
nActiveColumns++;
}
}
for(i=0;i<nCols;i++)
{
if (pMatrixColumns[i].bActive && pMatrixColumns[i].nCounter >= pMatrixColumns[i].nCounterMax)
{
Matrix_DrawFadedBackgroundChar(i);
pMatrixColumns[i].bActive = FALSE;
nActiveColumns--;
}
else if (pMatrixColumns[i].bActive)
{
Matrix_DrawFadedBackgroundChar(i);
Matrix_DrawActiveBackgroundChar(i);
pMatrixColumns[i].nCounter++;
}
}
}
void Matrix_OnTimer(HWND hwnd,UINT nEvent)
{
switch(nEvent)
{
case 1:
if(pMatrixColumns)
{
RECT r;
Matrix_UpdateBackground();
GetClientRect(hwnd,&r);
InvalidateRect(hwnd,&r,FALSE);
}
break;
}
}
void Matrix_OnClose(HWND hwnd)
{
PostQuitMessage(0);
2010年02月24日 20点02分
4
level 1
//
// 销毁GDI资源
if(pBitmapOld)
SelectObject(hMemDC,pBitmapOld);
if(pBitmapOld)
SelectObject(hMemDC,pFontOld);
if(hMemDC)
DeleteDC(hMemDC);
if(pMatrixColumns)
free(pMatrixColumns);
}
LRESULT CALLBACK MatrixWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
Matrix_OnCreate(hwnd);
break;
case WM_PAINT:
Matrix_OnPaint(hwnd);
break;
case WM_TIMER:
Matrix_OnTimer(hwnd,(UINT)wParam);
break;
case WM_ERASEBKGND:
return TRUE;
case WM_CLOSE:
Matrix_OnClose(hwnd);
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX wcex;
HWND hwnd;
MSG message;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)MatrixWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon =
wcex.hCursor = NULL;
wcex.hbrBackground = GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szClassName;
wcex.hIconSm = NULL;
srand((unsigned int)time(NULL));
RegisterClassEx(&wcex);
hwnd=CreateWindow(szClassName,szClassName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,Matrix_Width,Matrix_Height,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOW);
while(GetMessage(&message,NULL,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return 0;
}
2010年02月24日 20点02分
5
level 1
WIN32 SDK
VC,BCC,LCC-WIN32,DEV-CPP都能编译
2010年02月25日 02点02分
7
level 1
我什么我这里缺少“excpt.h”这个文件
原文:Cannot open include file: 'excpt.h': No such file or directory
帮帮忙,我对你的程序很感兴趣
2010年02月25日 03点02分
8
level 6
编译器: Default compiler执行 g++.exe...g++.exe "H:\mian.cpp" -o "H:\mian.exe" -I"D:\Program Files\DEV-CPP\lib\gcc\mingw32\3.4.2\include" -I"D:\Program Files\DEV-CPP\include\c++\3.4.2\backward" -I"D:\Program Files\DEV-CPP\include\c++\3.4.2\mingw32" -I"D:\Program Files\DEV-CPP\include\c++\3.4.2" -I"D:\Program Files\DEV-CPP\include" -L"D:\Program Files\DEV-CPP\Lib" C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x60):mian.cpp: undefined reference to `CreateCompatibleDC@4'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0xe9):mian.cpp: undefined reference to `CreateFontA@56'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x10c):mian.cpp: undefined reference to `CreateCompatibleBitmap@12'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x12a):mian.cpp: undefined reference to `SelectObject@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x148):mian.cpp: undefined reference to `SelectObject@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x164):mian.cpp: undefined reference to `GetTextMetricsA@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x2c2):mian.cpp: undefined reference to `BitBlt@36'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x49c):mian.cpp: undefined reference to `GetTextExtentPoint32A@16'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x506):mian.cpp: undefined reference to `SetBkColor@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x54b):mian.cpp: undefined reference to `ExtTextOutA@32'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x566):mian.cpp: undefined reference to `SetBkColor@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x5ab):mian.cpp: undefined reference to `ExtTextOutA@32'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x5c0):mian.cpp: undefined reference to `SetTextColor@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x62b):mian.cpp: undefined reference to `TextOutA@20'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x726):mian.cpp: undefined reference to `GetTextExtentPoint32A@16'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x76a):mian.cpp: undefined reference to `SetTextColor@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x7cf):mian.cpp: undefined reference to `TextOutA@20'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x9c6):mian.cpp: undefined reference to `SelectObject@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0x9e8):mian.cpp: undefined reference to `SelectObject@8'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0xa01):mian.cpp: undefined reference to `DeleteDC@4'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccQtbaaa.o(.text+0xb0e):mian.cpp: undefined reference to `GetStockObject@4'collect2: ld returned 1 exit status
执行结束
2010年03月03日 04点03分
12
level 1
回复:12楼
ld要链接上user32.a gdi32.a两个库
2010年03月03日 07点03分
14
level 6
回复:14楼
成功了,就是窗口太小了,而且不能最大化,建立win控制台程序就自动连接的
2010年03月03日 08点03分
15
level 5
int WINAPI WinMain(...) 函数里的
wcex.hbrBackground = GetStockObject(BLACK_BRUSH);
srand((unsigned int)time(NULL));
这两行的编译不过。。。
2010年03月16日 04点03分
17
level 6
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/13.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
2010年03月16日 10点03分
19
level 1
这种问题都已经懒得说了,既然是WinMain入口点,肯定是Win32 Application了,建立Win32 Application工程,或者直接写MAKEFILE /subsystem:windows
2010年03月16日 10点03分
20
level 0
32位win7下实现了,谢楼主,不过窗口有点小,窗口没有图标。另外请教楼主我WIN7下不兼容VC请问有办法吗?
2010年03月16日 12点03分
21