📄 mdidemo.c
字号:
if (IsWindow (hwndChild))
SendMessage (hwndChild, WM_COMMAND, wParam, lParam) ;
break ; // ...and then to DefFrameProc
}
break ;
case WM_QUERYENDSESSION:
case WM_CLOSE: // Attempt to close all children
SendMessage (hwnd, WM_COMMAND, IDM_WINDOW_CLOSEALL, 0) ;
if (NULL != GetWindow (hwndClient, GW_CHILD))
return 0 ;
break ; // i.e., call DefFrameProc
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
// Pass unprocessed messages to DefFrameProc (not DefWindowProc)
return DefFrameProc (hwnd, hwndClient, message, wParam, lParam) ;
}
BOOL CALLBACK CloseEnumProc (HWND hwnd, LPARAM lParam)
{
if (GetWindow (hwnd, GW_OWNER)) // Check for icon title
return TRUE ;
SendMessage (GetParent (hwnd), WM_MDIRESTORE, (WPARAM) hwnd, 0) ;
if (!SendMessage (hwnd, WM_QUERYENDSESSION, 0, 0))
return TRUE ;
SendMessage (GetParent (hwnd), WM_MDIDESTROY, (WPARAM) hwnd, 0) ;
return TRUE ;
}
LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static COLORREF clrTextArray[] = { RGB (0, 0, 0), RGB (255, 0, 0),
RGB (0, 255, 0), RGB ( 0, 0, 255),
RGB (255, 255, 255) } ;
static HWND hwndClient, hwndFrame ;
HDC hdc ;
HMENU hMenu ;
PHELLODATA pHelloData ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
// Allocate memory for window private data
pHelloData = (PHELLODATA) HeapAlloc (GetProcessHeap (),
HEAP_ZERO_MEMORY, sizeof (HELLODATA)) ;
pHelloData->iColor = IDM_COLOR_BLACK ;
pHelloData->clrText = RGB (0, 0, 0) ;
SetWindowLong (hwnd, 0, (long) pHelloData) ;
// Save some window handles
hwndClient = GetParent (hwnd) ;
hwndFrame = GetParent (hwndClient) ;
return 0 ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDM_COLOR_BLACK:
case IDM_COLOR_RED:
case IDM_COLOR_GREEN:
case IDM_COLOR_BLUE:
case IDM_COLOR_WHITE:
// Change the text color
pHelloData = (PHELLODATA) GetWindowLong (hwnd, 0) ;
hMenu = GetMenu (hwndFrame) ;
CheckMenuItem (hMenu, pHelloData->iColor, MF_UNCHECKED) ;
pHelloData->iColor = wParam ;
CheckMenuItem (hMenu, pHelloData->iColor, MF_CHECKED) ;
pHelloData->clrText = clrTextArray[wParam - IDM_COLOR_BLACK] ;
InvalidateRect (hwnd, NULL, FALSE) ;
}
return 0 ;
case WM_PAINT:
// Paint the window
hdc = BeginPaint (hwnd, &ps) ;
pHelloData = (PHELLODATA) GetWindowLong (hwnd, 0) ;
SetTextColor (hdc, pHelloData->clrText) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, World!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_MDIACTIVATE:
// Set the Hello menu if gaining focus
if (lParam == (LPARAM) hwnd)
SendMessage (hwndClient, WM_MDISETMENU,
(WPARAM) hMenuHello, (LPARAM) hMenuHelloWindow) ;
// Check or uncheck menu item
pHelloData = (PHELLODATA) GetWindowLong (hwnd, 0) ;
CheckMenuItem (hMenuHello, pHelloData->iColor,
(lParam == (LPARAM) hwnd) ? MF_CHECKED : MF_UNCHECKED) ;
// Set the Init menu if losing focus
if (lParam != (LPARAM) hwnd)
SendMessage (hwndClient, WM_MDISETMENU, (WPARAM) hMenuInit,
(LPARAM) hMenuInitWindow) ;
DrawMenuBar (hwndFrame) ;
return 0 ;
case WM_QUERYENDSESSION:
case WM_CLOSE:
if (IDOK != MessageBox (hwnd, TEXT ("OK to close window?"),
TEXT ("Hello"),
MB_ICONQUESTION | MB_OKCANCEL))
return 0 ;
break ; // i.e., call DefMDIChildProc
case WM_DESTROY:
pHelloData = (PHELLODATA) GetWindowLong (hwnd, 0) ;
HeapFree (GetProcessHeap (), 0, pHelloData) ;
return 0 ;
}
// Pass unprocessed message to DefMDIChildProc
return DefMDIChildProc (hwnd, message, wParam, lParam) ;
}
LRESULT CALLBACK RectWndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hwndClient, hwndFrame ;
HBRUSH hBrush ;
HDC hdc ;
PRECTDATA pRectData ;
PAINTSTRUCT ps ;
int xLeft, xRight, yTop, yBottom ;
short nRed, nGreen, nBlue ;
switch (message)
{
case WM_CREATE:
// Allocate memory for window private data
pRectData = (PRECTDATA) HeapAlloc (GetProcessHeap (),
HEAP_ZERO_MEMORY, sizeof (RECTDATA)) ;
SetWindowLong (hwnd, 0, (long) pRectData) ;
// Start the timer going
SetTimer (hwnd, 1, 250, NULL) ;
// Save some window handles
hwndClient = GetParent (hwnd) ;
hwndFrame = GetParent (hwndClient) ;
return 0 ;
case WM_SIZE: // If not minimized, save the window size
if (wParam != SIZE_MINIMIZED)
{
pRectData = (PRECTDATA) GetWindowLong (hwnd, 0) ;
pRectData->cxClient = LOWORD (lParam) ;
pRectData->cyClient = HIWORD (lParam) ;
}
break ; // WM_SIZE must be processed by DefMDIChildProc
case WM_TIMER: // Display a random rectangle
pRectData = (PRECTDATA) GetWindowLong (hwnd, 0) ;
xLeft = rand () % pRectData->cxClient ;
xRight = rand () % pRectData->cxClient ;
yTop = rand () % pRectData->cyClient ;
yBottom = rand () % pRectData->cyClient ;
nRed = rand () & 255 ;
nGreen = rand () & 255 ;
nBlue = rand () & 255 ;
hdc = GetDC (hwnd) ;
hBrush = CreateSolidBrush (RGB (nRed, nGreen, nBlue)) ;
SelectObject (hdc, hBrush) ;
Rectangle (hdc, min (xLeft, xRight), min (yTop, yBottom),
max (xLeft, xRight), max (yTop, yBottom)) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
return 0 ;
case WM_PAINT: // Clear the window
InvalidateRect (hwnd, NULL, TRUE) ;
hdc = BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_MDIACTIVATE: // Set the appropriate menu
if (lParam == (LPARAM) hwnd)
SendMessage (hwndClient, WM_MDISETMENU, (WPARAM) hMenuRect,
(LPARAM) hMenuRectWindow) ;
else
SendMessage (hwndClient, WM_MDISETMENU, (WPARAM) hMenuInit,
(LPARAM) hMenuInitWindow) ;
DrawMenuBar (hwndFrame) ;
return 0 ;
case WM_DESTROY:
pRectData = (PRECTDATA) GetWindowLong (hwnd, 0) ;
HeapFree (GetProcessHeap (), 0, pRectData) ;
KillTimer (hwnd, 1) ;
return 0 ;
}
// Pass unprocessed message to DefMDIChildProc
return DefMDIChildProc (hwnd, message, wParam, lParam) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -