| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 357 人关注过本帖, 1 人收藏
标题:写了api程序窗口一闪而过,不知怎么回事?求大侠帮忙
只看楼主 加入收藏
一个人的孤独
Rank: 2
等 级:论坛游民
帖 子:45
专家分:56
注 册:2012-10-15
结帖率:87.5%
收藏(1)
已结贴  问题点数:20 回复次数:1 
写了api程序窗口一闪而过,不知怎么回事?求大侠帮忙
#include<windows.h>
#include<time.h>
#define LEFT    0X01
#define RIGHT    0X02
#define UP        0x03
#define DOWN    0x04
#define C_W        600
#define C_H        600
typedef struct SNAKE
{
    unsigned char head_x;
    unsigned char head_y;
    unsigned char tail_x;
    unsigned char tail_y;
    unsigned char h_index;
    unsigned char t_index;
    unsigned char s_state;
    unsigned char foodstate;
    unsigned char score;
}Snake;
Snake snake;
typedef struct CNT
{
    unsigned char direction;
    unsigned char count;
}Count;
Count count[30];
unsigned char map[60][60]={'0'};
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   PSTR szCmdLine,
                   int iCmdshow)
{
    snake.head_x=1;
    snake.head_y=0;
    snake.tail_x=0;
    snake.tail_y=0;
    snake.foodstate=1;
    snake.s_state=1;
    snake.score=0;
    count[snake.h_index].count=2;
    count[snake.h_index].direction=RIGHT;
    count[snake.t_index].count=2;
    count[snake.t_index].direction=RIGHT;
    static TCHAR szName[]=TEXT("tcs_me");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground    =(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.hCursor        =LoadCursor(NULL,IDC_ARROW);
    wndclass.hIcon            =LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hInstance        =hInstance;
    wndclass.lpfnWndProc    =WndProc;
    wndclass.lpszClassName    =szName;
    wndclass.lpszMenuName    =NULL;
    wndclass.style            =CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndclass);//注册窗口类
    hwnd=CreateWindow(    szName,
                        TEXT("贪吃蛇"),
                        WS_OVERLAPPEDWINDOW,
                        0,
                        0,
                        C_W,
                        C_H,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);
    ShowWindow(hwnd,iCmdshow);
    HDC hdc;
    RECT rect;
    hdc=GetDC(hwnd);
    HBRUSH hbrush;
    hbrush=CreateSolidBrush(RGB(255,0,0));
    rect.left    =0;
    rect.top    =0;
    rect.right    =20;
    rect.bottom    =10;
    FillRect(hdc,&rect,hbrush);
    UpdateWindow(hwnd);
    ReleaseDC(hwnd,hdc);
    SetTimer(hwnd,1,100,NULL);
    srand((int)time(0));
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    TCHAR szScore[10];
    TCHAR f_x,f_y;
    HDC hdc;
    hdc=GetDC(hwnd);
//    RECT rect;
//    HBRUSH hbrush;
    switch(msg)
    {
    case WM_KEYDOWN:
        {
            if(wParam>=VK_LEFT&&wParam<=VK_DOWN)
            {
                if(snake.s_state==1)snake.s_state=0;
                else snake.s_state=1;
            }
            else if(wParam==VK_LEFT)
            {
                snake.h_index=(snake.h_index+1)%30;
                count[snake.h_index].direction=LEFT;
                count[snake.h_index].count=1;
            }
            else if(wParam==VK_RIGHT)
            {
                snake.h_index=(snake.h_index+1)%30;
                count[snake.h_index].direction=RIGHT;
                count[snake.h_index].count=1;
            }
            else if(wParam==VK_UP)
            {
                snake.h_index=(snake.h_index+1)%30;
                count[snake.h_index].direction=UP;
                count[snake.h_index].count=1;
            }
            else if(wParam==VK_DOWN)
            {
                snake.h_index=(snake.h_index+1)%30;
                count[snake.h_index].direction=DOWN;
                count[snake.h_index].count=1;
            }
            break;
        }
    case WM_TIMER:
        time_t t;
        HDC hdc;
        hdc=GetDC(hwnd);
        HBRUSH hbrush;//画刷
        RECT rect;//矩形左上角和右下角左标
        //head
HEAD:    switch(count[snake.h_index].direction)
        {
        case LEFT:
            {
                if(snake.head_x>0)snake.head_x--;
                else snake.head_x=60;
            }
        case RIGHT:
            {
                if(snake.head_x<60)snake.head_x++;
                else snake.head_x=0;
            }
        case UP:
            {
                if(snake.head_y>0)snake.head_y--;
                else snake.head_y=60;
            }
        case DOWN:
            {
                if(snake.head_y>0)snake.head_y--;
                else snake.head_y=60;
            }
        default:break;
        }
        if(map[snake.head_y][snake.head_y]==0)
        {
            map[snake.head_y][snake.head_y]=1;
        }
        else if(map[snake.head_y][snake.head_x]==1)
        {
            wsprintf(szScore,TEXT("%d"),snake.score);
            MessageBox(NULL,szScore,TEXT("GAMEOVER!!!"),0);
        }
        else if(map[snake.head_y][snake.head_x]==2)
        {
            snake.foodstate=0;
            snake.score++;
            map[snake.head_y][snake.head_x]=1;
            goto HEAD;
        }
        //画蛇头
        count[snake.h_index].count++;
        hbrush=CreateSolidBrush(RGB(255,0,0));
        rect.left    =snake.head_x*10;
        rect.top    =snake.head_y*10;
        rect.right    =rect.left     +10;
        rect.bottom    =rect.top     +10;
        FillRect(hdc,&rect,hbrush);
        //蛇尾
        if(count[snake.t_index].count==2)
        {   
            map[snake.tail_y][snake.tail_x]=0;
            count[snake.t_index].count=1;
            hbrush=CreateSolidBrush(RGB(255,255,255));
            rect.left    =snake.tail_x*10;
            rect.top    =snake.tail_y*10;
            rect.right    =rect.left     +10;
            rect.bottom    =rect.top     +10;
            FillRect(hdc,&rect,hbrush);
        }
        //蛇尾坐标
        switch(count[snake.t_index].direction)
        {
        case LEFT:    snake.tail_x--;
        case RIGHT:    snake.tail_x++;
        case UP:    snake.tail_y--;
        case DOWN:    snake.tail_y++;
        default:break;
        }
        //食物
        if(snake.s_state==1)
        {
            do
            {
                f_x=rand()%3600%60;
                f_y=rand()%3600%60;
            }while(map[f_x][f_y]==1);
            hbrush=CreateSolidBrush(RGB(150,200,10));
            rect.left    =f_x*10;
            rect.top    =f_y*10;
            rect.right    =rect.left+10;
            rect.bottom    =rect.top +10;
            FillRect(hdc,&rect,hbrush);
            srand((unsigned) time(&t));
        }
        ReleaseDC(hwnd,hdc);
    case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
    case WM_DESTROY:
            PostQuitMessage(0);
            break;   
    default:return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    return 0;
}
搜索更多相关主题的帖子: include 
2012-12-29 01:34
yuccn
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:20 
楼主大意了,在
case WM_TIMER: 的那个分支你忘记加break;了,导致继续执行了WM_CLOSE分支就把窗口关闭了

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-12-29 10:24
快速回复:写了api程序窗口一闪而过,不知怎么回事?求大侠帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026145 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved