Windows程序设计034和035的示例程序的WM_SIZE消息的上面为什么没有break或return
第34讲示例程序/* -------------------------------------------------------------------
MyWindows.c -- 基本窗口模型
《Windows 程序设计(SDK)》视频教程
--------------------------------------------------------------------*/
#include <windows.h>
#include <strsafe.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("鱼C工作室"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar;
static int cLinesMax, cLines;
static PMSG pmsg;
static RECT rectScroll;
static TCHAR szTop[] = TEXT("Message Key Char Repeat Scan Ext ALT Prev Tran");
static TCHAR szUnd[] = TEXT("_______ ___ ____ ______ ____ ___ ___ ____ ____");
static TCHAR *szFormat = {
TEXT("%-13s %3d %-15s%c%6u %4d %3s %3s %4s %4s"),
TEXT("%-13s 0x%04X%1s%c %6u %4d %3s %3s %4s %4s")
};
static TCHAR *szYes = TEXT("Yes");
static TCHAR *szNo = TEXT("No");
static TCHAR *szDown = TEXT("Down");
static TCHAR *szUp = TEXT("Up");
static TCHAR *szMessage[] = {
TEXT("WM_KEYDOWN"), TEXT("WM_KEYUP"), TEXT("WM_CHAR"), TEXT("WM_DEADCHAR"),
TEXT("WM_SYSKEYDOWN"), TEXT("WM_SYSKEYUP"), TEXT("WM_SYSCHAR"), TEXT("WM_SYSDEADCHAR")
};
HDC hdc;
int i, iType;
PAINTSTRUCT ps;
TCHAR szBuffer, szKeyName;
TEXTMETRIC tm;
switch (message)
{
case WM_CREATE:
case WM_DISPLAYCHANGE:
// 获取客户区的最大尺寸
cxClientMax = GetSystemMetrics(SM_CXMAXIMIZED);
cyClientMax = GetSystemMetrics(SM_CYMAXIMIZED);
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT)); // 设置等宽字体
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight;
ReleaseDC(hwnd, hdc);
if (pmsg)
{
free(pmsg);
}
cLinesMax = cyClientMax / cyChar; // 计算客户区一屏最多可以容纳多少行
pmsg = malloc(cLinesMax * sizeof(MSG)); // 申请足够容纳 cLinesMax 行消息的内存
cLines = 0;
// 这里没有 break,没有 return。
case WM_SIZE:
if (message == WM_SIZE)
{
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
}
// 计算滚动的矩形
rectScroll.left = 0;
rectScroll.right = cxClient;
rectScroll.top = cyChar;
rectScroll.bottom = cyChar * (cyClient / cyChar);
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
case WM_DEADCHAR:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_SYSCHAR:
case WM_SYSDEADCHAR:
// 但凡接收到上述消息,页面向上滚动一行
for (i = cLinesMax - 1; i > 0; i--)
{
pmsg = pmsg;
}
pmsg.hwnd = hwnd;
pmsg.message = message;
pmsg.wParam = wParam;
pmsg.lParam = lParam;
cLines = min(cLines + 1, cLinesMax);
ScrollWindow(hwnd, 0, -cyChar, &rectScroll, &rectScroll);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 0, 0, szTop, lstrlen(szTop));
TextOut(hdc, 0, 0, szUnd, lstrlen(szUnd));
for (i = 0; i < min(cLines, cyClient / cyChar - 1); i++)
{
iType = pmsg.message == WM_CHAR ||
pmsg.message == WM_SYSCHAR ||
pmsg.message == WM_DEADCHAR ||
pmsg.message == WM_SYSDEADCHAR;
GetKeyNameText(pmsg.lParam, szKeyName, sizeof(szKeyName) / sizeof(TCHAR));
TextOut(hdc, 0, (cyClient / cyChar - 1 - i) * cyChar, szBuffer,
wsprintf(szBuffer, szFormat,
szMessage.message - WM_KEYFIRST], pmsg.wParam,
(PTSTR)(iType ? TEXT(" ") : szKeyName),
(TCHAR)(iType ? pmsg.wParam : ' '),
LOWORD(pmsg.lParam), HIWORD(pmsg.lParam) & 0xFF,
0x01000000 & pmsg.lParam ? szYes : szNo,
0x20000000 & pmsg.lParam ? szYes : szNo,
0x40000000 & pmsg.lParam ? szDown : szUp,
0x80000000 & pmsg.lParam ? szUp : szDown));
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
=======================================================================================
第35讲示例程序
/* -------------------------------------------------------------------
MyWindows.c -- 基本窗口模型
《Windows 程序设计(SDK)》视频教程
--------------------------------------------------------------------*/
#include <windows.h>
#define BUFFER(x, y) *(pBuffer + y * cxBuffer + x)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("鱼C工作室"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static DWORD dwCharSet = DEFAULT_CHARSET;
static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer, xCaret, yCaret;
static TCHAR *pBuffer = NULL;
HDC hdc;
int x, y, i;
PAINTSTRUCT ps;
TEXTMETRIC tm;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight;
ReleaseDC(hwnd, hdc);
// 此处木有返回,木有break
case WM_SIZE:
if (message == WM_SIZE)
{
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
}
cxBuffer = max(1, cxClient / cxChar);
cyBuffer = max(1, cyClient / cyChar);
if (pBuffer != NULL)
{
free(pBuffer);
}
pBuffer = (TCHAR *)malloc(cxBuffer * cyBuffer * sizeof(TCHAR));
for (y = 0; y < cyBuffer; y++)
{
for (x = 0; x < cxBuffer; x++)
{
BUFFER(x, y) = ' ';
}
}
// 将插入符号指向左上角
xCaret = 0;
yCaret = 0;
if (hwnd == GetFocus())
{
SetCaretPos(xCaret * cxChar, yCaret * cyChar);
}
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_SETFOCUS:
CreateCaret(hwnd, NULL, cxChar, cyChar);
SetCaretPos(xCaret * cxChar, yCaret * cyChar);
ShowCaret(hwnd);
return 0;
case WM_KILLFOCUS:
HideCaret(hwnd);
DestroyCaret();
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_HOME:
xCaret = 0;
break;
case VK_END:
xCaret = cxBuffer - 1;
break;
case VK_PRIOR:
yCaret = 0;
break;
case VK_NEXT:
yCaret = cyBuffer - 1;
break;
case VK_LEFT:
xCaret = max(xCaret - 1, 0);
break;
case VK_RIGHT:
xCaret = min(xCaret + 1, cxBuffer - 1);
break;
case VK_UP:
yCaret = max(yCaret - 1, 0);
break;
case VK_DOWN:
yCaret = min(yCaret + 1, cyBuffer - 1);
break;
case VK_DELETE:
for (x = xCaret; x < cxBuffer - 1; x++)
{
BUFFER(x, yCaret) = BUFFER(x + 1, yCaret);
}
BUFFER(cxBuffer - 1, yCaret) = ' ';
HideCaret(hwnd);
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
TextOut(hdc, xCaret * cxChar, yCaret * cyChar, &BUFFER(xCaret, yCaret), cxBuffer - xCaret);
ReleaseDC(hwnd, hdc);
ShowCaret(hwnd);
break;
}
SetCaretPos(xCaret * cxChar, yCaret * cyChar);
return 0;
case WM_CHAR:
for (i = 0; i < (int)LOWORD(lParam); i++)
{
switch (wParam)
{
case '\b':
if (xCaret > 0)
{
xCaret--;
SendMessage(hwnd, WM_KEYDOWN, VK_DELETE, 1);
}
break;
case '\t':
do
{
SendMessage(hwnd, WM_CHAR, ' ', 1);
} while (xCaret % 8 != 0);
break;
case '\n':
if (++yCaret == cyBuffer)
{
yCaret = 0;
}
break;
case '\r':
xCaret = 0;
if (++yCaret == cyBuffer)
{
yCaret = 0;
}
break;
case '\x1B': // 十六进制的1B,对应的ASCII字符是ESC
for (y = 0; y < cyBuffer; y++)
{
for (x = 0; x < cxBuffer; x++)
{
BUFFER(x, y) = ' ';
}
}
xCaret = 0;
yCaret = 0;
InvalidateRect(hwnd, NULL, FALSE);
break;
default:
BUFFER(xCaret, yCaret) = (TCHAR)wParam;
HideCaret(hwnd);
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
TextOut(hdc, xCaret * cxChar, yCaret * cyChar, &BUFFER(xCaret, yCaret), 1);
ReleaseDC(hwnd, hdc);
ShowCaret(hwnd);
if (++xCaret == cxBuffer)
{
xCaret = 0;
if (++yCaret == cyBuffer)
{
yCaret = 0;
}
}
break;
}
}
SetCaretPos(xCaret * cxChar, yCaret * cyChar);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
for (y = 0; y < cyBuffer; y++)
{
TextOut(hdc, 0, y * cyChar, &BUFFER(0, y), cxBuffer);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
} @小甲鱼 {:10_247:}{:10_247:}{:10_247:}{:10_247:} 人有失误,马有失蹄。 必须有 return 0; 别在纠结 当没有return 或break的时候 就是执行完这条 需要继续执行下一条 。原书中的代码没有错误; 你看wm_size 消息给了你if判断;就是判断是系统size消息来的 还是由于上边的create消息顺序执行下来的. 加不加都行 不过为了逻辑清楚和减少判断 还是加上比较好 不加程序也不会错 只是会继续往下走 再走几个判断而已,最终也是返回,进行消息队列下一个消息判断 wuyuan2011woain 发表于 2017-6-23 07:29
加不加都行 不过为了逻辑清楚和减少判断 还是加上比较好 不加程序也不会错 只是会继续往下走 再走几个判断 ...
哦,那这个作者岂不是闲的蛋疼 154433083 发表于 2017-6-23 07:55
哦,那这个作者岂不是闲的蛋疼
{:7_118:} wuyexinfei 发表于 2017-6-22 20:43
你看wm_size 消息给了你if判断;就是判断是系统size消息来的 还是由于上边的create消息顺序执行下来的.
这么说,上面两个程序的WM_SIZE上面去掉return没有什么意义,只是作者为了演示你所说的这样一种效果? 谢谢大家! 本帖最后由 wuyexinfei 于 2017-6-24 19:56 编辑
在create消息时候执行了size消息下的代码 .如果加了返回后在size下的代码就不会被执行...
这种处理消息的方法在后面的章节将经常被用到...比如处理一大串相似并列(比如颜色)菜单的时候 中间都是不加返回或调出命令的; wuyexinfei 发表于 2017-6-24 19:52
在create消息时候执行了size消息下的代码 .如果加了返回后在size下的代码就不会被执行...
这种处理消息的 ...
哦
页:
[1]