I am creating an application to display a slideshow of images. My application starts and runs successfully, but when I click somewhere else on my screen and change the window focus, the app becomes unresponsive. I am unsure if it is a problem with my event loop or something else.
Below is my main function and I have my event loop inside the main loop.
int WINAPI WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
EnumDisplayMonitors(NULL, NULL, MyInfoEnumProc, 0);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(wcex.hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T(""),
NULL);
return 1;
}
hInst = hInstance;
HWND hWnd = createWindow(hInst);
DWORD Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;
HWND View1 = CreateWindow(L"STATIC", NULL, Style, 0, 0, windowWidth, windowHeight, hWnd, NULL, hInst, NULL);
SFMLView1.create(View1);
SFMLView1.setMouseCursorVisible(false);
SFMLView1.setFramerateLimit(10);
SFMLView1.setVerticalSyncEnabled(true);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T(""),
NULL);
return 1;
}
DEVMODE fullscreenSettings;
bool isChangeSuccessful;
EnumDisplaySettings(NULL, 0, &fullscreenSettings);
fullscreenSettings.dmPelsWidth = 1080;
fullscreenSettings.dmPelsHeight = 720;
fullscreenSettings.dmBitsPerPel = 8;
fullscreenSettings.dmDisplayFrequency = 30;
fullscreenSettings.dmFields = DM_PELSWIDTH |
DM_PELSHEIGHT |
DM_BITSPERPEL |
DM_DISPLAYFREQUENCY;
SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);
SetWindowLongPtr(hWnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
SetWindowPos(hWnd, HWND_BOTTOM, windowX, windowY, windowWidth, windowHeight, SWP_SHOWWINDOW); // Z-order set to the bottom so other windows can be displayed above it
isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;
ShowWindow(hWnd, SW_MAXIMIZE);
DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
dwExStyle &= ~WS_EX_APPWINDOW;
dwExStyle |= WS_EX_TOOLWINDOW;
SetWindowLong(hWnd, GWL_EXSTYLE, dwExStyle);
ShowWindow(hWnd,
nCmdShow);
UpdateWindow(hWnd);
std::vector<std::string> imagePaths = populateImages(hWnd);
if (imagePaths.size() == 0) {
exit(2);
}
std::cout << imagePaths.at(0) << "\n";
//MSG msg;
int index = 0;
sf::Image image;
sf::FileInputStream stream;
sf::Texture texture;
sf::Vector2f targetSize(windowWidth, windowHeight);
std::vector<sf::Sprite> sprites;
sf::Sprite sprite;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
while (SFMLView1.isOpen()) {
sf::Event event;
while (SFMLView1.pollEvent(event))
{
if (event.type == sf::Event::Closed)
SFMLView1.close();
}
stream.open(imagePaths.at(index));
texture.loadFromStream(stream);
sf::Sprite sprite(texture);
sprite.setScale(
targetSize.x / sprite.getLocalBounds().width,
targetSize.y / sprite.getLocalBounds().height
);
SFMLView1.clear();
SFMLView1.draw(sprite);
SFMLView1.display();
if (index == imagePaths.size() - 1) {
index = 0;
}
else {
index += 1;
}
std::this_thread::sleep_for(std::chrono::seconds(2));
}
return 0;
}
Thanks