Results 1 to 2 of 2

Thread: Creating a Screen Saver for Windows

  1. #1
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Creating a Screen Saver for Windows

    Hi everybody,
    I've seen this theme discussed before but I never found a complete solution, so I wrote one myself and plan to put it on the wiki. There is just one little kink to be worked out first.

    A windows screen saver is just an exe renamed to .scr, which can be started in three modes, depending on the command line flags:
    1. "-c" Configuration mode: The Application should pop up a config dialog, save changed properties to the registry and quit.
    2. "-s" Screen saver mode: The Application should start in Full screen.
    3. "-p XXXX" Preview mode: The Application should start as a child window to the "Windows Display Properties" dialog. The window ID is given as a second argument.


    As could be expected I have no problems with the first two modes; the third mode is giving me a headache, though.
    here is the relevant code:
    Qt Code:
    1. ...
    2. int main(int argc, char* argv[])
    3. {
    4. QApplication app(argc, argv);
    5.  
    6. // parse the commandline:
    7. ...
    8.  
    9. ScreenSaverWidget* widget = new ScreenSaverWidget;
    10. widget->setAttribute(Qt::WA_DeleteOnClose);
    11. QObject::connect(widget, SIGNAL(destroyed()), &app, SLOT(quit()));
    12. switch(mode)
    13. {
    14. case ConfigurationMode:
    15. configure();
    16. return 0;
    17.  
    18. case FullScreenMode:
    19. new ScreenSaverEventFilter(widget);
    20. widget->showFullScreen();
    21. break;
    22.  
    23. case PreviewMode:
    24. {
    25. widget->setWindowFlags(Qt::FramelessWindowHint|Qt::SubWindow);
    26. ::SetParent(widget->winId(), parent);
    27. RECT parentRect;
    28. ::GetClientRect(parent, &parentRect);
    29. widget->move(0,0);
    30. widget->resize(parentRect.right, parentRect.bottom);
    31. widget->show();
    32. }
    33. }
    34. return app.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

    The good thing about the code above is that it is unobtrusive. I.e. you can replace ScreenSaverWidget with any other widget, without having to change its code.
    In full screen mode I install an event filter that intercepts mouse movements and key presses.
    In preview mode I reparent the widget using win32 API calls and that's where something goes wrong. The widget shows up allright, but it doesn't seem to be embedded properly. closeEvent() and the dtor never gets called when I close the dialog and the application never shuts down properly.

    I'm hoping some one has an idea what I have to do.

    I attach the complete code. To test it you have to right click on ScreenSaver.scr and select "install".

    thanks
    Attached Files Attached Files

  2. #2

    Default Re: Creating a Screen Saver for Windows

    try this

    Qt Code:
    1. ...
    2. int main(int argc, char* argv[])
    3. {
    4. QApplication app(argc, argv);
    5.  
    6. // parse the commandline:
    7. ...
    8.  
    9. ScreenSaverWidget* widget = new ScreenSaverWidget;
    10. widget->setAttribute(Qt::WA_DeleteOnClose);
    11. QObject::connect(widget, SIGNAL(destroyed()), &app, SLOT(quit()));
    12. switch(mode)
    13. {
    14. case ConfigurationMode:
    15. configure();
    16. return 0;
    17.  
    18. case FullScreenMode:
    19. new ScreenSaverEventFilter(widget);
    20. widget->showFullScreen();
    21. return app.exec();
    22.  
    23. case PreviewMode:
    24. {
    25. widget->setWindowFlags(Qt::FramelessWindowHint|Qt::SubWindow);
    26. ::SetParent(widget->winId(), parent);
    27. RECT parentRect;
    28. ::GetClientRect(parent, &parentRect);
    29. widget->move(0,0);
    30. widget->resize(parentRect.right, parentRect.bottom);
    31. widget->show();
    32. }
    33. }
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.