July 2 2009




 

Maarten's Blogs

The life of a Windows Mobile Developer and Windows Embedded Evangelist.

Search Blog Entries:

 
Thursday, July 2, 2009
Scrolling Large Images on Windows Mobile Devices

In preparation for my attempt to provide a managed solution for one of the Windows Mobile 6.5 DTK samples I needed a simple solution to scroll a large picture inside a managed application. My first attempt, painting the image directly on the Main Form and creating Scroll Bars manually, is working but rather complex. It involves changing the style of the Main Form, which after all is a Window, by P/Invoking SetWindowLong to add Scroll Bars to the form, making use of the SetScrollInfo and GetScrollInfo APIs to initialize the Scroll Bars properly and to retrieve the current position of the Scroll Bars. Besides that, it also involves dealing with Scroll Bar messages and calling the ScrollWindowEx API, not the most P/Invoke friendly API in the world. However, there is a much simpler solution, making use of a PictureBox control.

Large Bitmap displayed in a PictureBox on a Form

In this picture, you will see an ordinary Windows Form with a PictureBox on it and two menu entries that simulate programmatically scrolling of the picture (something I need for one of the Windows Mobile 6.5 DTK Gesture samples).

Initializing and Resizing a PictureBox Control hosted on a Form

During application initialization, the PictureBox is initialized with a Bitmap, and its size is adjusted to hold the entire Bitmap. You need to make sure that the Docking property of the PictureBox is set to DockStyle.None. As you can see in the first figure, fully functional Scroll Bars are displayed to be able to scroll through the picture. To programmatically scroll through the picture, it is possible to make use of a number of Windows Messages, as shown in the first code snippet.

The Bitmap scrolled to the right through Windows Messages

The important thing here is that the Scroll Bars are not reachable through the PictureBox, but through the Main Form, so the Windows Messages are in fact being sent to the Main Form. Since the Main Form has scrolling logic implemented (because the Scroll Bars do react upon user input), it is also safe to send WM_HSCROLL and WM_VSCROLL messages to it programmatically. The performance is pretty good and it hardly takes any code in this way. This is the complete code for this simple example (assuming you have a Bitmap available as a resource):

Scrolling a Bitmap using Windows Messages

Something similar can be done by displaying a Bitmap inside a Panel Control, but in that case you are responsible for painting the image yourself, a bit more work with no real added value.



   What is this?18:45 PM
Blog Ref URL:  http://www.dotnetfordevices.com/forum.html#375










SpiralFX Web Development
www.spiralfx.com

:: ::