Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Scrollable Dialog in Pure Win32 API

0.00/5 (No votes)
29 Jan 2009 1  
Minimalistic example of scrollable dialog implemented in pure Win32 API

ScrollDialog

Image 1. Fully opened dialog window.

scroll_dialog_resize.png

Image 2. Scrolled dialog window.

Introduction

A popup dialog window is one of the most common GUI elements in the Windows world. Everybody knows how to create a dialog with DialogBox function call. Also, everybody knows that popup dialogs are neither resizable nor scrollable by default. Unfortunately there is no good example of simple resizable and scrollable dialog box implemented in pure Win32 API without MFC, ATL, WTL, etc. Most of examples require MFC or some other GUI framework. This article demonstrates how to implement scrollable dialog window in pure Win32 API with Windows Platform SDK.

Background

The idea behind scrollable dialog is extremely simple. All that one needs to do in order to get a scrollable dialog is summarized in the following steps:

  1. Specify resizable border for a dialog in resource editor or add WS_THICKFRAME style to dialog template by hand.
  2. Enable scroll bars for a dialog window or add WS_VSCROLL | WS_HSCROLL style(s) to dialog window.
  3. Handle WM_RESIZE, WM_HSCROLL and WM_VSCROLL messages.

Note: The dialog is resizable, however the current sample does not reposition dialog controls according to dialog size. If you need to stretch or shrink dialog contents according to window size, then you will need to do it by yourself.

Using the Code

The sample project for the article is created with Microsoft Visual C++ 2008. However, the code does not use MFC, ATL or any other VC++ specific framework or library. The code can be compiled with any decent C/C++ compiler and requires Windows Platform SDK only.

In order to better organize the source code, the project uses so-called message crackers. Windows message crackers are simple macros defined in WindowsX.h header file, which can be found in PSDK's include directory. Here is the excellent description of Windows message crackers: Message Cracker Wizard for Win32 SDK Developers.

Basically, the algorithm for scrollable dialog is as follows:

  1. In WM_INITDIALOG handler, set initial range (maximum and minimum values) and page size for a scrollbar. Make page size the same as the range, so scrollbars are hidden by default.
  2. In WM_SIZE handler, set new page size for scrollbars, so scrollbars will recalculate thumb size and redraw it accordingly. New page size equals client area width/height. Scroll window client area into a view if necessary.
  3. In WM_HSCROLL and WM_VSCROLL message handlers, scroll window client area by requested amount with ScrollWindow[Ex] call. Update current scrolling position.

The Windows will do the rest.

History

  • 29th January, 2009: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here