クリッピング領域の定義
ユーザーが [クリップ領域の定義] をクリックすると、 システムはWM_COMMAND メッセージを発行します。 このメッセージの wParam パラメーターには、ユーザーがメニューからこのオプションを選択したことを示す、アプリケーション定義の定数IDM_DEFINEが含まれています。 アプリケーションは、次のコード サンプルに示すように、ブール型フラグ fDefineRegion を設定して、この入力を処理します。
case WM_COMMAND:
switch (wParam)
{
case IDM_DEFINE:
fDefineRegion = TRUE;
break;
[ 領域の定義 ] をクリックすると、カーソルがアプリケーションのクライアント領域にある間にマウスをクリックしてドラッグすることで、四角形の描画を開始できます。
ユーザーが左ボタンを押すと、システムは WM_LBUTTONDOWN メッセージを発行します。 このメッセージの lParam パラメーターには、クリッピング領域の定義に使用される四角形の左上隅に対応するカーソル座標が含まれています。 アプリケーションは、 次のようにWM_LBUTTONDOWN メッセージを処理します。
// These variables are required for clipping.
static POINT ptUpperLeft;
static POINT ptLowerRight;
static POINT aptRect[5];
static POINT ptTmp;
static POINTS ptsTmp;
static BOOL fDefineRegion;
static BOOL fRegionExists;
static HRGN hrgn;
static RECT rctTmp;
int i;
switch (message)
{
case WM_LBUTTONDOWN:
if (fDefineRegion)
{
// Retrieve the new upper left corner.
ptsTmp = MAKEPOINTS(lParam);
ptUpperLeft.x = (LONG) ptsTmp.x;
ptUpperLeft.y = (LONG) ptsTmp.y;
}
if (fRegionExists)
{
// Erase the previous rectangle.
hdc = GetDC(hwnd);
SetROP2(hdc, R2_NOTXORPEN);
if (!Polyline(hdc, (CONST POINT *) aptRect, 5))
errhandler("Polyline Failed", hwnd);
ReleaseDC(hwnd, hdc);
// Clear the rectangle coordinates.
for (i = 0; i < 4; i++)
{
aptRect[i].x = 0;
aptRect[i].y = 0;
}
// Clear the temporary point structure.
ptTmp.x = 0;
ptTmp.y = 0;
// Clear the lower right coordinates.
ptLowerRight.x = 0;
ptLowerRight.y = 0;
// Reset the flag.
fRegionExists = FALSE;
fDefineRegion = TRUE;
// Retrieve the new upper left corner.
ptsTmp = MAKEPOINTS(lParam);
ptUpperLeft.x = (LONG) ptsTmp.x;
ptUpperLeft.y = (LONG) ptsTmp.y;
}
break;
}
ユーザーがマウスをドラッグすると、システムはメッセージ WM_MOUSEMOVE 発行し、新しいカーソル座標を lParam パラメーターに格納します。 アプリケーションは、新しい WM_MOUSEMOVE メッセージを受信するたびに、前の四角形 (存在する場合) を消去し、 ポリライン 関数を呼び出して新しい四角形を描画し、四角形の四隅の座標を渡します。 アプリケーションは、次のタスクを実行します。
// These variables are required for clipping.
static POINT ptUpperLeft;
static POINT ptLowerRight;
static POINT aptRect[5];
static POINT ptTmp;
static POINTS ptsTmp;
static BOOL fDefineRegion;
static BOOL fRegionExists;
static HRGN hrgn;
static RECT rctTmp;
int i;
switch (message)
{
case WM_MOUSEMOVE:
if (wParam & MK_LBUTTON && fDefineRegion)
{
// Get a window DC.
hdc = GetDC(hwnd);
if (!SetROP2(hdc, R2_NOTXORPEN))
errhandler("SetROP2 Failed", hwnd);
// If previous mouse movement occurred, store the original
// lower right corner coordinates in a temporary structure.
if (ptLowerRight.x)
{
ptTmp.x = ptLowerRight.x;
ptTmp.y = ptLowerRight.y;
}
// Get the new coordinates of the clipping region's lower
// right corner.
ptsTmp = MAKEPOINTS(lParam);
ptLowerRight.x = (LONG) ptsTmp.x;
ptLowerRight.y = (LONG) ptsTmp.y;
// If previous mouse movement occurred, erase the original
// rectangle.
if (ptTmp.x)
{
aptRect[0].x = ptUpperLeft.x;
aptRect[0].y = ptUpperLeft.y;
aptRect[1].x = ptTmp.x;
aptRect[1].y = ptUpperLeft.y;
aptRect[2].x = ptTmp.x;
aptRect[2].y = ptTmp.y;
aptRect[3].x = ptUpperLeft.x;
aptRect[3].y = ptTmp.y;
aptRect[4].x = aptRect[0].x;
aptRect[4].y = aptRect[0].y;
if (!Polyline(hdc, (CONST POINT *) aptRect, 5))
errhandler("Polyline Failed", hwnd);
}
aptRect[0].x = ptUpperLeft.x;
aptRect[0].y = ptUpperLeft.y;
aptRect[1].x = ptLowerRight.x;
aptRect[1].y = ptUpperLeft.y;
aptRect[2].x = ptLowerRight.x;
aptRect[2].y = ptLowerRight.y;
aptRect[3].x = ptUpperLeft.x;
aptRect[3].y = ptLowerRight.y;
aptRect[4].x = aptRect[0].x;
aptRect[4].y = aptRect[0].y;
if (!Polyline(hdc, (CONST POINT *) aptRect, 5))
errhandler("Polyline Failed", hwnd);
ReleaseDC(hwnd, hdc);
}
break;