三部分:
WinMain(主函数)
WinProc(消息处理函数)
...(自定义的函数)
不知道你是什么编译器
如果跟我一样,欢迎追问告诉你创建步骤!
另外附一个小程序(需要用到外部库、文件、函数,请勿尝试编译)
#include
#include
#include
#include
#pragma comment(lib,"msimg32")
#define Window_FullWidth 864
#define Window_FullHeight 540
#define Screen_FullWidth GetSystemMetrics(SM_CXSCREEN)
#define Screen_FullHeight GetSystemMetrics(SM_CYSCREEN)
#define Game_Menu
#define Game_Load
#define Game_Play
#define Game_Pause
#define Game_Over
HDC g_hdc,m_hdc,t_hdc;
HBITMAP bm,bk,bg,background;
HFONT hfont;
BLENDFUNCTION bf;
FILE *fp;
POINT p;
int bTrans,bTrans_add=1;
void game_Init(HWND hwnd);
void game_Play(HWND hwnd);
void game_check(HWND hwnd);
void game_Paint(HWND hwnd);
void game_reset(HWND hwnd);
void game_Clear(HWND hwnd);
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(hInstance,"DICO");
wc.hIconSm = LoadIcon(hInstance,"DICO");
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","",WS_VISIBLE|WS_OVERLAPPEDWINDOW^WS_THICKFRAME^WS_MINIMIZEBOX^WS_MAXIMIZEBOX,
(Screen_FullWidth-Window_FullWidth)/2,(Screen_FullHeight-Window_FullHeight)/2,Window_FullWidth,Window_FullHeight,NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
game_Init(hwnd);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
srand(time(NULL));
game_reset(hwnd);
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
int start_clock_count=clock();
game_Play(hwnd);
game_Paint(hwnd);
while(clock()-start_clock_count<30);
}
}
return msg.wParam;
}
void game_Init(HWND hwnd)
{
char s[20]; HINSTANCE hInstance;
t_hdc = GetDC(hwnd);
g_hdc = CreateCompatibleDC(t_hdc);
m_hdc = CreateCompatibleDC(g_hdc);
background = CreateCompatibleBitmap(t_hdc,Window_FullWidth,Window_FullHeight);
bg = (HBITMAP) LoadImage(GetModuleHandle(0),"res\\day.bmp",IMAGE_BITMAP,0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
bk = (HBITMAP) LoadImage(GetModuleHandle(0),"res\\black.bmp",IMAGE_BITMAP,0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = bTrans;
bf.AlphaFormat = 0;
}
void game_Paint(HWND hwnd)
{
SelectObject(g_hdc,background);
SelectObject(m_hdc,bg);
BitBlt(g_hdc,0,0,100,100,m_hdc,0,0,SRCCOPY);
SelectObject(m_hdc,bk);
BitBlt(g_hdc,0,0,300,300,m_hdc,0,0,SRCCOPY);
BitBlt(t_hdc,0,0,Window_FullWidth,Window_FullHeight,g_hdc,0,0,SRCCOPY);
}
void game_Play(HWND hwnd)
{
if(bTrans==255) bTrans_add=-1; if(bTrans==0) bTrans_add=1;
bTrans+=5*bTrans_add; bf.SourceConstantAlpha = bTrans;
}
void game_check(HWND hwnd)
{
}
void game_reset(HWND hwnd)
{
}
void game_Clear(HWND hwnd)
{
ReleaseDC(hwnd,t_hdc);
DeleteObject(g_hdc);
DeleteObject(hfont);
DeleteObject(m_hdc);
DeleteObject(bm);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
p.x=LOWORD(lParam); p.y=HIWORD(lParam);
switch(Message) {
case WM_DESTROY: {
game_Clear(hwnd);
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}