如何在预览窗口关闭后完成SDL屏幕保护程序应用程序?

时间:2016-06-01 12:54:23

标签: c++ windows sdl screensaver

当windows使用/ p参数启动我的程序时,它会在该小预览监视器的HWND上初始化SDL窗口。问题是,当预览窗口关闭时,我的进程会继续运行。我试图使用SDL_SYSWMEVENT捕获WM_DESTROY,但它不起作用:它捕获WM_SETCURSOR,WM_NCHITTEST和其他一些,但不会破坏。如何解决这个问题?
这是我的代码。

#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include <windows.h>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>

char *lowercase(const char *str) {
    char *ans=new char[strlen(str)+1]; int i;
    for (i=0;str[i]>0;i++) ans[i]=tolower(str[i]);
    ans[i]=0; return ans;
}

int main(int argc,char **argv) {
    if (argc>1&&(strcmp(lowercase(argv[1]),"/c")==0||strcmp(lowercase(argv[1]),"-c")==0)) {
        MessageBox(NULL,"Config should be there, WIP!","Screensaver",MB_OK);
        return 0;
    }
    FILE *f;
    SDL_Init(SDL_INIT_VIDEO);
    int SCREEN_WIDTH,SCREEN_HEIGHT;
    SDL_Window *wnd;
    bool preview;
    if (preview=(argc>2&&(strcmp(lowercase(argv[1]),"/p")==0||strcmp(lowercase(argv[1]),"-p")==0))) {
        wnd=SDL_CreateWindowFrom((HWND)atoi(argv[2]));
        SDL_GetWindowSize(wnd,&SCREEN_WIDTH,&SCREEN_HEIGHT);
    } else {
        SDL_DisplayMode tmp;
        SDL_GetCurrentDisplayMode(0,&tmp);
        SCREEN_WIDTH=tmp.w;SCREEN_HEIGHT=tmp.h;
        wnd=SDL_CreateWindow("",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN|SDL_WINDOW_FULLSCREEN);
    }
    SDL_Renderer *rnd=SDL_CreateRenderer(wnd,-1,SDL_RENDERER_ACCELERATED);
    SDL_DisableScreenSaver();
    bool quit=false;
    SDL_Event evt; 
    SDL_EventState(SDL_SYSWMEVENT,SDL_ENABLE);
    while (SDL_PollEvent(&evt)); //clear event queue
    while (!quit) {
        while (SDL_PollEvent(&evt)) switch (evt.type) {
            /* does not work
            case SDL_SYSWMEVENT:  {
                if (evt.syswm.msg->msg.win.msg==WM_DESTROY) quit=true;
                break;
            }
            */
            case SDL_QUIT:
                quit=true;
                break;
            case SDL_KEYDOWN:
            case SDL_MOUSEBUTTONDOWN:
            case SDL_MOUSEMOTION:
                if (!preview) quit=true;
                break;
        }
        SDL_RenderClear(rnd);
        SDL_SetRenderDrawColor(rnd,255,255,255,255);
        SDL_RenderDrawLine(rnd,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
        SDL_SetRenderDrawColor(rnd,0,0,0,255);
        SDL_RenderPresent(rnd);
        SDL_Delay(20);
    }
    SDL_Quit();
    return 0;
}

2 个答案:

答案 0 :(得分:0)

查看this文章,您正在寻找的消息组合

WM_SYSCOMMAND SC_CLOSE

答案 1 :(得分:0)

好的,经过长时间的研究,我找到了答案。感谢that guy

if (!IsWindow(previewHwnd)) quit=true;