如何在预览 window 关闭后完成 SDL 屏保应用程序?
How to finish SDL screensaver app after preview window closes?
当 windows 使用 /p 参数启动我的程序时,它会通过那个小预览监视器的 HWND 初始化 SDL window。问题是,当预览 window 关闭时,我的进程保持 运行。我尝试使用 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;
}
查看 this 文章,您要查找的消息组合似乎是
WM_SYSCOMMAND SC_CLOSE
好的,经过长时间的研究,我找到了答案。感谢 that guy.
if (!IsWindow(previewHwnd)) quit=true;
当 windows 使用 /p 参数启动我的程序时,它会通过那个小预览监视器的 HWND 初始化 SDL window。问题是,当预览 window 关闭时,我的进程保持 运行。我尝试使用 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;
}
查看 this 文章,您要查找的消息组合似乎是
WM_SYSCOMMAND SC_CLOSE
好的,经过长时间的研究,我找到了答案。感谢 that guy.
if (!IsWindow(previewHwnd)) quit=true;