Читать «Применение Windows API» онлайн - страница 47

А. И. Легалов

Здесь показано, как можно определить клиентское сообщение.

// Reserved by Reliable Software Library

const UINT MSG_RS_LIBRARY = WM_USER + 0x4000;

// wParam = new position wrt parent's left edge

const UINT MSG_MOVESPLITTER = MSG_RS_LIBRARY + 1;

В заключение представлен фрагмент из очень полезного класса HWnd, который инкапсулирует многие базисных функции API Windows, имеющие дело с окнами. В частности, рассмотрите методы MoveDelayPaint и ForceRepaint, который мы использовали в перерисовке полоски расщепителя.

class HWnd {

public:

 void Update() {

  ::UpdateWindow(_hwnd);

 }

 // Moving

 void Move(int x, int y, int width, int height) {

  ::MoveWindow(_hwnd, x, y, width, height, TRUE);

 }

 void MoveDelayPaint(int x, int y, int width, int height) {

  ::MoveWindow(_hwnd, x, y, width, height, FALSE);

 }

 // Repainting

 void Invalidate() {

  ::InvalidateRect(_hwnd, 0, TRUE);

 }

 void ForceRepaint() {

  Invalidate();

  Update();

 }

 private:

 HWND _hwnd;

};

Как обычно, Вы можете загрузить полный исходный текст приложения, которое использовалось в этом примере.

Далее: Следующая обучающая программа рассказывает о растрах.

Bitmaps

In this tutorial we'll learn how to load bitmaps from resources and from files, how to pass them around and blit them to the screen. We'll also see how to create and use an empty bitmap as a canvas, draw a picture on it and then blit it to the screen. Finally, we'll combine these techniques to write a simple program that uses double-buffering and timer messages to show a simple animation involving sprites.

First of all, in most cases Windows provides storage for bitmaps and takes care of the formatting of bits. The programmer gets access to the bitmap through a handle, whose type is HBITMAP. (Remember to set the STRICT flag when compiling Windows programs, to make sure HBITMAP is a distinct type, rather than just a pointer to void.)

Since a bitmap is a resource (in the Resource Management sense), the first step is to encapsulate it in a “strong pointer” type of interface. Notice the transfer semantics of the constructor and the overloaded assignment operator, characteristic of a resource that can have only one owner at a time.

We instruct Windows to release the resources allocated to the bitmap by calling DeleteObject.

class Bitmap {

public:

 Bitmap() : _hBitmap (0) {}

 // Transfer semantics

 Bitmap(Bitmap& bmp) : _hBitmap(bmp.Release()) {}

 void operator=(Bitmap& bmp) {

  if (bmp._hBitmap != _hBitmap) {

   Free();

  _hBitmap = bmp.Release();

  }

 }

 HBITMAP Release() {

 HBITMAP h = _hBitmap;

  _hBitmap = 0;

  return h;

 }

 ~Bitmap() {

  Free();

 }

 // implicit conversion for use with Windows API

 operator HBITMAP() {

  return _hBitmap;

 }

protected:

 Bitmap(HBITMAP hBitmap) : _hBitmap(hBitmap) {}

 void Free() {

  if (_hBitmap) ::DeleteObject(_hBitmap);