本蒟蒻改编的一个小小的蹦迪程序
  • 板块灌水区
  • 楼主123456789qaz
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/5/26 20:50
  • 上次更新2023/10/28 00:34:43
查看原帖
本蒟蒻改编的一个小小的蹦迪程序
727398
123456789qaz楼主2022/5/26 20:50

本蒟蒻根据Dev C++ 工具里提供的源代码改编的小程序

虽然本蒟蒻啥也没看懂,只是把现成代码改了改

上代码 c++ TDM-GCC 4.9.2:

#include <windows.h>
#include<cstdio>
#include<cstring>
void movew(int *x,int *y,int *xk,int *yk,bool *ux,bool *uy,bool *uxk,bool *uyk,int k){
    //x变化 
        if(*uxk){
            *xk=*xk+k;
        }
        else{
            *xk=*xk-k;
        }
        if(*xk>=1366-150){
            *uxk=false;
        }
        else if(*xk<=0){
            *uxk=true;
        }
        //y变化 
        if(*uyk){
            *yk=*yk+k;
        }
        else{
            *yk=*yk-k;
        }
        if(*yk>=768-150){
            *uyk=false;
        }
        else if(*yk<=0){
            *uyk=true;
        }
        //x高度变化 
        if(*ux){
            *x=*x+k;
        }
        else{
            *x=*x-k;
        }
        if(*x>=1366){
            *ux=false;
        }
        else if(*x<=150){
            *ux=true;
        }
        //y高度变化 
        if(*uy){
            *y=*y+k;
        }
        else{
            *y=*y-k;
        }
        if(*y>=768){
            *uy=false;
        }
        else if(*y<=150){
            *uy=true;
        }
}
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    switch(Message) {

        /* Upon destruction, tell the main thread to stop */
        case WM_DESTROY: {
            PostQuitMessage(0);
            break;
        }

        /* All other messages (a lot of them) are processed using default procedures */
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wc; /* A properties struct of our window */
    HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
    MSG msg; /* A temporary location for all messages */
    HWND hwnd1;
    /* zero out the struct and set the stuff we want to modify */
    memset(&wc,0,sizeof(wc));
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc; /* This is where we will send messages to */
    wc.hInstance     = hInstance;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

    /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

    if(!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }

    //以上是基础,误删! 
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Begin",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        500, /* width */
        500, /* height */
        NULL,NULL,hInstance,NULL);

    MessageBox(hwnd, "请关掉Begin窗口","开始",
        MB_ICONWARNING |MB_OK );

    if(hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }
    /*
        This is the heart of our program where all input is processed and 
        sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
        this loop will not produce unreasonably high CPU usage
    */
    while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
        TranslateMessage(&msg); /* Translate key codes to chars if present */
        DispatchMessage(&msg); /* Send it to WndProc */
    }

    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","哈哈哈",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        150, /* width */
        150, /* height */
        NULL,NULL,hInstance,NULL);
    hwnd1 = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","哈哈哈",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        150, /* width */
        150, /* height */
        NULL,NULL,hInstance,NULL);
    int x1,y1,xk1,yk1;
    bool ux1=true;
    bool uy1=true;
    bool uxk1=true;
    bool uyk1=true;
    int x2,y2,xk2,yk2;
    bool ux2=true;
    bool uy2=true;
    bool uxk2=true;
    bool uyk2=true;
    x1=y1=150;
    xk1=yk1=1;
    x2=y2=150;
    xk2=600;
    yk2=30;
    int k=5;
    while(true){
        MoveWindow(hwnd,xk1,yk1,x1,y1,false);
        MoveWindow(hwnd1,xk2,yk2,x2,y2,false);
        movew(&x1,&y1,&xk1,&yk1,&ux1,&uy1,&uxk1,&uyk1,k);
        movew(&x2,&y2,&xk2,&yk2,&ux2,&uy2,&uxk2,&uyk2,k);
        Sleep(1);

    }
    return msg.wParam;
}

效果:两个空白窗口在屏幕上蹦迪

问题:开始蹦迪之后,如果点击任意一处,就会死机,目前还不知道怎么解决,望各位大佬们看看。

关闭方法:关掉黑色窗口即可

本蒟蒻的程序变量名很迷惑,麻烦各位大佬了

各位大佬们,如果有懂得,麻烦看看那个问题,谢谢了

2022/5/26 20:50
加载中...