基于win32应用程序的电梯调度程序

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <queue>
using namespace std;

queue<int> enterFloor; //电梯输入队列
queue<int> moveFloor;  //电梯运行队列

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    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_HREDRAW | CS_VREDRAW;;                 /* 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 colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* 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 */
           _T("elevator"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           400,       /* Windows decides the position */
           50,       /* where the window ends up on the screen */
           400,                 /* The programs width */
           600,                 /* 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, nCmdShow);

    /* 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;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK textprom (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            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;
}

//画楼层边框和门
void drawLine(HDC hdc, int width, int length, HPEN pen, HWND hwnd)
{
    pen = CreatePen(PS_DOT,3,RGB(0,0,0));
    SelectObject(hdc, pen);

    RECT rect;
    GetClientRect(hwnd, &rect);

    char f[3]="0F";
    //6条横线
    for(int i=0; i<=5; i++)
    {
        MoveToEx(hdc, width, length/5*i, (LPPOINT) NULL);
        LineTo(hdc, 0, length/5*i);
    }
//   printf("width=%d length=%d\n", width, length);
    //2条竖线
    for(int i=0; i<2; i++)
    {
        MoveToEx(hdc, width*i, length, (LPPOINT) NULL);
        LineTo(hdc, width*i, 0);
    }
    //5扇门
    for(int i=0; i<5; i++)
    {
        Rectangle(hdc, width/2-60, (i+1)*length/5-80, width/2+60, (i+1)*length/5);

        MoveToEx(hdc, width/2, (i+1)*length/5-80, (LPPOINT) NULL);
        LineTo(hdc, width/2, (i+1)*length/5);
    }


    for(int i=0; i<5; i++)
    {
        rect.top = i*length/5+10;
    //    rect.bottom += i*length/5;
        f[0]='0'+(5-i);
        SetTextColor(hdc, RGB(0,0,0));
        DrawText(hdc,f,strlen(f), &rect, DT_CENTER);
    }

    DeleteObject(pen);
}

//画移动中电梯
void drawMoveElevator(HDC hdc, int x, int y, HPEN pen)
{
    pen = CreatePen(PS_DOT,3,RGB(112,128,144));
    SelectObject(hdc, pen);

    Rectangle(hdc, x-60, y-80, x+60, y);

    MoveToEx(hdc, x, y-80, (LPPOINT) NULL);
    LineTo(hdc, x, y);

    DeleteObject(pen);
}

//画打开中电梯
void drawOpenElevator(HDC hdc, int x, int y, int l, HPEN pen)
{
    pen = CreatePen(PS_DOT,3,RGB(112,128,144));
    SelectObject(hdc, pen);

    Rectangle(hdc, x-60, y-80, x+60, y);

    MoveToEx(hdc, x-l, y-80, (LPPOINT) NULL);
    LineTo(hdc, x-l, y);
    MoveToEx(hdc, x+l, y-80, (LPPOINT) NULL);
    LineTo(hdc, x+l, y);

    DeleteObject(pen);
}
//电梯调度的算法,其实也没有什么算法
void analyze(int curFloor, int dir)
{
    int arr[5]={0};
    for(; !enterFloor.empty(); enterFloor.pop())
    {
        arr[enterFloor.front()] = 1;
    }

    if(dir==-1) //往上走
    {
        for(int i=curFloor; i<5; i++)
        {
            if(arr[i]) moveFloor.push(i);
        }

        for(int i=curFloor-1; i>=0; i--)
        {
            if(arr[i]) moveFloor.push(i);
        }
    }

    if(dir==1) //往下走
    {
        for(int i=curFloor; i>=0; i--)
        {
            if(arr[i]) moveFloor.push(i);
        }

        for(int i=curFloor+1; i<5; i++)
        {
            if(arr[i]) moveFloor.push(i);
        }
    }
}


LRESULT CALLBACK WindowProcedure (
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
    //界面相关
    HDC hdc, hdcBuffer;
    PAINTSTRUCT ps;
    RECT rect;
    HBITMAP hBmp, oldBmp;
    HPEN pen;
    HBRUSH newbrush, oldbrush;
    int wmId, wmEvent, length, width;
    //电梯相关
    char floorName[3]="0F";
    static bool open = false;
    static bool move = false;

    GetClientRect(hwnd, &rect);             //获取客户区的大小
    length = (int)(rect.bottom-rect.top);   //窗口的高度
    width = (int)(rect.right-rect.left);    //窗口的宽度

    static int col=0;            //水平上坐标
    static int row=0;            //垂直上坐标
    static int len=0;            //电梯门打开大小
    static int currentFloor=0;   //电梯停靠楼层
    static int targetFloor=0;    //电梯目标楼层
    static int evelatorDir = -1; //电梯运行方向
    static int doorDir = 1;      //电梯门运行方向

    switch(uMsg)
    {
    //重绘事件
    case WM_PAINT:
        hdc=BeginPaint(hwnd,&ps);

        //创建缓冲DC
        hdcBuffer = CreateCompatibleDC(hdc);

        //创建位图资源,将所有东西都画在这上面
        hBmp = CreateCompatibleBitmap (hdc, width, length);
        oldBmp = (HBITMAP)SelectObject(hdcBuffer, hBmp);

        //把背景刷成蓝色的,不然黑的好丑
        newbrush=CreateSolidBrush(RGB(0xB0,0xC4,0xDE));
        oldbrush=(HBRUSH)SelectObject(hdcBuffer,&newbrush);
        FillRect(hdcBuffer,&rect,newbrush);

        //画电梯楼层等
        drawLine(hdcBuffer, width, length, pen, hwnd);

        if(move) drawMoveElevator(hdcBuffer, row, col, pen);

        if(open) drawOpenElevator(hdcBuffer, row, col, len, pen);

        //把画好的内容输出到客户区
        BitBlt(hdc, 0, 0, width, length, hdcBuffer, 0, 0, SRCCOPY);

        //电梯处于运动状态,不断重绘界面
        if(open || move)
        {
            Sleep(20);                       //线程休眠,看到移动的效果
            InvalidateRect(hwnd,NULL,FALSE); //促使客户区循环重绘,选为false不重绘背景
        }
        //电梯处于上下运动状态,改变坐标
        if(move)
        {
            row = width/2;
            col += evelatorDir;
            if((5-targetFloor)*length/5 == col)
            {
                move = false;
                open = true;
                currentFloor = targetFloor;
            }
        }
        //电梯在打开关闭状态
        if(open)
        {
            row = width/2;
            col = (5-currentFloor)*length/5;
            len += doorDir;

            if(doorDir<0 && len==0)
            {
                if(!moveFloor.empty())
                {
                    targetFloor = moveFloor.front();
                    moveFloor.pop();
                }

                open = false;
                doorDir = 1;
                if(targetFloor != currentFloor)
                {
                    move = true;
                    evelatorDir = (targetFloor-currentFloor)>0?-1:1;
                }
                else if(!moveFloor.empty())
                {
                    open = true;
                }
            }

            if(doorDir>0 && len==60)
            {
                Sleep(1500);
                doorDir = -1;
            }
        }

        //释放资源
        SelectObject(hdcBuffer, oldBmp);
        SelectObject(hdcBuffer,oldbrush);

        DeleteObject(newbrush);
        DeleteDC(hdcBuffer);
        EndPaint(hwnd,&ps);

        return 0;
        break;

    //添加按钮
    case WM_CREATE:
        for(int i=0; i<5; i++)
        {
            floorName[0]='0'+5-i;
            CreateWindow(TEXT("button"),    //必须为:button
                        TEXT(floorName),    //按钮上显示的字符
                        WS_CHILD | WS_VISIBLE,
                        20+i%2*50, 4*length/5+10+i/2*30, 40, 20,
                        hwnd,(HMENU)(i),
                        ((LPCREATESTRUCT)lParam)->hInstance,NULL);
        }

        CreateWindow(TEXT("button"),
                    TEXT("run"),        //run按钮
                    WS_CHILD | WS_VISIBLE,
                    20+5%2*50, 4*length/5+10+5/2*30, 40, 20,
                    hwnd,(HMENU)(5),
                    ((LPCREATESTRUCT)lParam)->hInstance,NULL);
        break;

    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // 分析菜单选择:
        switch (wmId)
        {
            case 0:
                enterFloor.push(4);
                break;
            case 1:
                enterFloor.push(3);
                break;
            case 2:
                enterFloor.push(2);
                break;
            case 3:
                enterFloor.push(1);
                break;
            case 4:
                enterFloor.push(0);
                break;
            case 5:

                col = (5-currentFloor)*length/5;
                //楼梯调度
                analyze(currentFloor, evelatorDir);

                if(!moveFloor.empty())
                {
                    targetFloor = moveFloor.front();
                    moveFloor.pop();

                    if(currentFloor!=targetFloor)
                    {
                        move = true;
                        evelatorDir = (targetFloor-currentFloor)>0?-1:1;
                    }
                    else open = true;
                }
                InvalidateRect(hwnd,NULL,TRUE);//促使客户区循环重绘

                break;
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

效果图:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值