📄 mainframe.c
字号:
static void mainFrame_OnSize (HWND hwnd, UINT state, int cx, int cy)
{
HWND hwndDisk ;
/***************************************/
/* The size of the window has changed. */
/* Map the logical coordinate system */
/* to the new extent of the window and */
/* move the disks to their appropriate */
/* position relative to the new size. */
/***************************************/
/****************************/
/* Set new scaling factors. */
/****************************/
mainFrame_SetHanoiMappingMode (hwnd, cx, cy) ;
/**********************************/
/* (Re)position each disk window. */
/**********************************/
hwndDisk = GetFirstChild (hwnd) ;
while (hwndDisk) {
disk_PositionDiskWindow (hwndDisk) ;
hwndDisk = GetNextSibling (hwndDisk) ;
}
}
//
// void mainFrame_OnSysColorChange (HWND hwnd)
//
// hwnd Handle of window to which this message applies
//
// PURPOSE: Forward the WM_SYSCOLORCHANGE message to all child
// windows of this top-level window, as well as to all
// children of the child windows, if any.
//
// COMMENTS: Windows sends the WM_SYSCOLORCHANGE message to all
// top-level windows after a change has been made to
// the system color settings. Your top level window
// must forward the WM_SYSCOLORCHANGE message to its
// common controls; otherwise the controls will not be
// notified of the color change.
//
static void
mainFrame_OnSysColorChange (HWND hwnd)
{
EnumChildWindows (hwnd, mainFrame_SysColorChangeNotification, (LPARAM) NULL) ;
}
//
// void mainFrame_SysColorChangeNotification (HWND hwnd, LPARAM lParam)
//
// hwnd Handle of child window
// lParam Application-defined value specified as the last
// parameter on the EnumChildWindows function call.
//
// PURPOSE: Forward a WM_SYSCOLORCHANGE message to the
// specified child window.
//
// COMMENTS:
//
BOOL CALLBACK
mainFrame_SysColorChangeNotification (HWND hwnd, LPARAM lParam)
{
// Forward the message to a child window
FORWARD_WM_SYSCOLORCHANGE (hwnd, SendMessage) ;
// Keep on enumerating...
return TRUE ;
lParam ; // Parameter not referenced
}
//
// void mainFrame_OnUserChanged (HWND hwnd)
//
// hwnd Handle of window to which this message applies
//
// PURPOSE: This is the appropriate place to reload any cached
// user-specific information.
//
// COMMENTS: Windows sends the WM_USERCHANGED message to all windows
// after a user has logged on or off. When a user logs on
// or off, the system updates the user-specific settings.
// Windows sends the WM_USERCHANGED message immediately
// after updating the user-specific settings.
//
// VERSION NOTES: The WM_USERCHANGED message is implemented on Windows 95
// but not currently on the Windows NT.
//
static void
mainFrame_OnUserChanged (HWND hwnd)
{
}
//
// BOOL mainFrame_DisplayContextMenu (HWND hwnd, POINT pt)
//
// hwnd Handle of window to which this message applies
// pt Location of mouse click in client coordinates
//
// PURPOSE: Display the appropriate context menu for the object
// located at 'pt' in the main frame window.
//
// COMMENTS:
//
BOOL mainFrame_DisplayContextMenu (HWND hwnd, POINT pt)
{
HMENU hmenuBar, hmenuPopup ;
int nItems ;
// Determine the appropriate context menu to display.
// generally using the application state and mouse click coordinates.
// Bring up the 'Help' popup menu for lack of a better solution
// Get main menu handle
hmenuBar = GetMenu (hwnd) ;
ASSERT (NULL != hmenuBar) ;
// Get the count of items on the main menu
nItems = GetMenuItemCount (hmenuBar) ;
ASSERT (-1 != nItems) ;
// ASSUMPTION: Help menu is the rightmost on the menu bar
hmenuPopup = GetSubMenu (hmenuBar, nItems - 1) ;
ASSERT (NULL != hmenuPopup) ;
// Convert click location to screen coordinates
ClientToScreen (hwnd, &pt) ;
// Display the floating popup menu at the mouse click location
// Track the right mouse as this function is called during
// WM_CONTEXTMENU message processing.
return TrackPopupMenu (hmenuPopup,
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
pt.x, pt.y, 0, hwnd, NULL) ;
}
/*************************************************************/
/* */
/* SetHanoiMappingMode */
/* */
/* Map a logical coordinate system of the form: */
/* */
/* +2048 y */
/* +------------------------------------| */
/* | ^ | */
/* | | | */
/* | W i n | d o w | */
/* | | | */
/* | | | */
/* |<----------------|----------------->| */
/* -1024 x (0,0) +1023 x */
/* */
/* to the current physical size of the client area. */
/* */
/*************************************************************/
static void
mainFrame_SetHanoiMappingMode (HWND hwnd, int cxClient, int cyClient)
{
HDC hdc ;
/***************************************************/
/* (Re)initialize the device context mapping mode. */
/***************************************************/
hdc = GetDC (hwnd) ;
// Use the anisotropic mapping mode.
// Arbitrarily scaled x- and y-axes.
SetMapMode (hdc, MM_ANISOTROPIC) ;
// The origin of the window is (still) at point (0,0).
SetWindowOrgEx (hdc, 0, 0, NULL) ;
/************************************************************/
/* Map the window origin to the point that is on the bottom */
/* of the window vertically and in the middle of the window */
/* horizontally. Once the y-axis is inverted, the window */
/* displays quadrant one and two of a cartesian coordinate */
/* system. */
/************************************************************/
SetViewportOrgEx (hdc, cxClient / 2, cyClient, NULL) ;
/********************************************************/
/* Set the logical extent of the window to 2,048 units, */
/* both vertically and horizontally. */
/********************************************************/
SetWindowExtEx (hdc, 2048, 2048, NULL) ;
/******************************************************************/
/* Map the 2,048 logical units to the current size of the window. */
/* The negative y-extent parameter inverts the y-axis. */
/******************************************************************/
SetViewportExtEx (hdc, cxClient, -cyClient, NULL) ;
ReleaseDC (hwnd, hdc) ;
}
/*******************************************************************/
/* mainFrame_IntersectRect */
/* This performs the same function as the Windows IntersectRect() */
/* except that this function expects the vertical axis to increase */
/* upwardly. Windows' function expects it to increase downwardly */
/* and doesn't correctly calculate the intersection. */
/*******************************************************************/
static BOOL
mainFrame_IntersectRect (LPRECT rect1, LPRECT rect2)
{
int nHighestLeft, nLowestRight ;
int nHighestBottom, nLowestTop ;
nHighestLeft = max (rect1->left, rect2->left) ;
nLowestRight = min (rect1->right, rect2->right) ;
if (nHighestLeft >= nLowestRight)
return FALSE ;
nHighestBottom = max (rect1->bottom, rect1->bottom) ;
nLowestTop = min (rect1->top, rect2->top) ;
return (nHighestBottom >= nLowestTop ? FALSE : TRUE) ;
}
/*****************************************/
/* mainFrame_ResetDisks */
/* Place all disks back on the left peg. */
/*****************************************/
static void
mainFrame_ResetDisks (HWND hwnd)
{
HWND hwndDisk ;
int i, nPostPos ;
HowMany [FIRST] = NUMDISKS ;
HowMany [SECOND] = 0 ;
HowMany [THIRD] = 0 ;
// Move all disks to the left peg.
hwndDisk = GetFirstChild (hwnd) ;
while (hwndDisk) {
disk_SetPostNum (hwndDisk, FIRST) ;
nPostPos = NUMDISKS - disk_GetDiskSize (hwndDisk) ;
WhosOn [FIRST][nPostPos] = hwndDisk ;
hwndDisk = GetNextSibling (hwndDisk) ;
}
for (i = 0; i < NUMDISKS; i++)
disk_PositionDiskWindow (WhosOn [FIRST][i]) ;
}
/*****************************************/
/* mainFrame_SolveProblem */
/* Move the disks in the correct order. */
/*****************************************/
static void
mainFrame_SolveProblem (HWND hwnd)
{
/***************************************************************/
/* Protect the current execution environment. We raise an */
/* exception when the user closes the window while in the nd */
/* second level message loop. We then terminate the program. */
/***************************************************************/
__try {
mainFrame_ResetDisks (hwnd) ;
disk_MoveTower (NUMDISKS, FIRST, SECOND, THIRD) ;
}
__except (GetExceptionCode() == STATUS_TERMINATION_REQUEST) {
PostQuitMessage (0) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -