windows程序设计中的WM_SIZE问题,求助!

yiruirui0507 2010-12-07 10:05:22
char buf1[15];
char buf2[15];
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
itoa(cxClient,buf1,10);
itoa(cyClient,buf2,10);
MessageBox(hwnd,(LPCTSTR)buf1,(LPCTSTR)buf2,MB_YESNO);
这里指贴出了主要的代码,经过测试得出两个结果:
1.WM_SIZE被调用了2次,因为输出2次。
2。输入乱码。
自己调试了一下,发现第一次 cxClient=935,cyClient=503
第二次 cxClient=935,cyClient=520;不知道什么原因。而且为什么调用了两次也不知道原因。
另外我已经用了itoa函数了,按理说应该输出字符串“935”之类的才对啊,为什么是乱码呢?

这里附上全部代码吧,方便大家。
/*----------------------------------------------------
SYSMETS3.C -- System Metrics Display Program No. 3
(c) Charles Petzold, 1998
----------------------------------------------------*/

#define WINVER 0x0500
#include <windows.h>
#include "sysmets.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SysMets3") ;
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 ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 3"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
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 cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth ;
HDC hdc ;
int i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd ;
PAINTSTRUCT ps ;
SCROLLINFO si ;
TCHAR szBuffer[10] ;
TEXTMETRIC tm ;
char buf1[15];
char buf2[15];
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;

GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;

ReleaseDC (hwnd, hdc) ;

// Save the width of the three columns

iMaxWidth = 40 * cxChar + 22 * cxCaps ;
return 0 ;

case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
itoa(cxClient,buf1,10);
itoa(cyClient,buf2,10);
MessageBoxW(hwnd,(LPCTSTR)buf1,(LPCTSTR)buf2,MB_YESNO);
// Set vertical scroll bar range and page size

si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = 0 ;
si.nMax = NUMLINES - 1 ;
si.nPage = cyClient / cyChar ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;

// Set horizontal scroll bar range and page size

si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = 0 ;
si.nMax = 2 + iMaxWidth / cxChar ;
si.nPage = cxClient / cxChar ;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
return 0 ;

case WM_VSCROLL:
// Get all the vertial scroll bar information

si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;
GetScrollInfo (hwnd, SB_VERT, &si) ;

// Save the position for comparison later on

iVertPos = si.nPos ;

switch (LOWORD (wParam))
{
case SB_TOP:
si.nPos = si.nMin ;
break ;

case SB_BOTTOM:
si.nPos = si.nMax ;
break ;

case SB_LINEUP:
si.nPos -= 1 ;
break ;

case SB_LINEDOWN:
si.nPos += 1 ;
break ;

case SB_PAGEUP:
si.nPos -= si.nPage ;
break ;

case SB_PAGEDOWN:
si.nPos += si.nPage ;
break ;

case SB_THUMBTRACK:
si.nPos = si.nTrackPos ;
break ;

default:
break ;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it may not be the same as the value set.

si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
GetScrollInfo (hwnd, SB_VERT, &si) ;

// If the position has changed, scroll the window and update it

if (si.nPos != iVertPos)
{
ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos),
NULL, NULL) ;
UpdateWindow (hwnd) ;
}
return 0 ;

case WM_HSCROLL:
// Get all the vertial scroll bar information

si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;

// Save the position for comparison later on

GetScrollInfo (hwnd, SB_HORZ, &si) ;
iHorzPos = si.nPos ;

switch (LOWORD (wParam))
{
case SB_LINELEFT:
si.nPos -= 1 ;
break ;

case SB_LINERIGHT:
si.nPos += 1 ;
break ;

case SB_PAGELEFT:
si.nPos -= si.nPage ;
break ;

case SB_PAGERIGHT:
si.nPos += si.nPage ;
break ;

case SB_THUMBPOSITION:
si.nPos = si.nTrackPos ;
break ;

default :
break ;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it may not be the same as the value set.

si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
GetScrollInfo (hwnd, SB_HORZ, &si) ;

// If the position has changed, scroll the window

if (si.nPos != iHorzPos)
{
ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0,
NULL, NULL) ;
}
return 0 ;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;

// Get vertical scroll bar position

si.cbSize = sizeof (si) ;
si.fMask = SIF_POS ;
GetScrollInfo (hwnd, SB_VERT, &si) ;
iVertPos = si.nPos ;

// Get horizontal scroll bar position

GetScrollInfo (hwnd, SB_HORZ, &si) ;
iHorzPos = si.nPos ;

// Find painting limits

iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar) ;
iPaintEnd = min (NUMLINES - 1,
iVertPos + ps.rcPaint.bottom / cyChar) ;

for (i = iPaintBeg ; i <= iPaintEnd ; i++)
{
x = cxChar * (1 - iHorzPos) ;
y = cyChar * (i - iVertPos) ;

TextOut (hdc, x, y,
sysmetrics[i].szLabel,
lstrlen (sysmetrics[i].szLabel)) ;

TextOut (hdc, x + 22 * cxCaps, y,
sysmetrics[i].szDesc,
lstrlen (sysmetrics[i].szDesc)) ;

SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;

TextOut (hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer,
wsprintf (szBuffer, TEXT ("%5d"),
GetSystemMetrics (sysmetrics[i].iIndex))) ;

SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
}

EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
...全文
204 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiruirui0507 2010-12-09
  • 打赏
  • 举报
回复
这个问题没人晓得的吗?看来我又是发现此问题的人。。。。。。。。。。。。。
wltg2001 2010-12-08
  • 打赏
  • 举报
回复
上面的问题请给出一些解释,滚动条到底属不属于客户区?这里的大小又如何解释呢?
=============
滚动条不属于客户区。
对于大小不同的问题,我觉得可能在WM_CREATE时并没有加入滚动条,而SetScrollInfo才加入的。当然这个只是猜测,具体原因不太好说。
yiruirui0507 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wltg2001 的回复:]
找到原因了,是在case WM_SIZE:
里调用:SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
和SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;造成的。
[/Quote]

恩,确实是这个原因。但是测试过程中又发现了一个问题:两次客户区宽度分别是520,503,而520-503=17也刚好是我电脑上滚动条的宽度。
问题:调试过程中2次调用WM_SIZE追踪结果,第一次也有滚动条,是在WM_CREATE之后调用的,因为CREATEWINDOW中我加入了滚动条风格,但是这里客户区宽度却是503,如果这里去掉滚动条则为520,这个如何解释?
第二次调用WM_SIZE是因为SetScrollInfo造成的,这里的窗口也是有滚动条的,大小却是520.只不过这里把滚动条给具体划分了一下罢了。
上面的问题请给出一些解释,滚动条到底属不属于客户区?这里的大小又如何解释呢?
为何两次都有滚动条但是客户区宽度却不相等呢?
wltg2001 2010-12-07
  • 打赏
  • 举报
回复
找到原因了,是在case WM_SIZE:
里调用:SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
和SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;造成的。
wltg2001 2010-12-07
  • 打赏
  • 举报
回复
我没有用你的代码,而是重新建了一个工程
yiruirui0507 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wltg2001 的回复:]
但是两次调用WM_SIZE却大小不等?又没有执行什么操作啊?
===========
我建了一个SDK工程,也试了一下,只发了一个WM_SIZE消息啊
[/Quote]

你的神马编译器?我的VS08,VC6,0都是2个啊、
wltg2001 2010-12-07
  • 打赏
  • 举报
回复
但是两次调用WM_SIZE却大小不等?又没有执行什么操作啊?
===========
我建了一个SDK工程,也试了一下,只发了一个WM_SIZE消息啊
yiruirui0507 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wltg2001 的回复:]
MessageBoxW(hwnd,(LPCTSTR)buf1,(LPCTSTR)buf2,MB_YESNO);
===============
你前面定义缓冲区是char 这里却用MessageBoxW,肯定乱码啊,用MessageBoxA吧
[/Quote]

恩,对的。都是只能提示把我连累了,哈哈。
但是两次调用WM_SIZE却大小不等?又没有执行什么操作啊?
wltg2001 2010-12-07
  • 打赏
  • 举报
回复
MessageBoxW(hwnd,(LPCTSTR)buf1,(LPCTSTR)buf2,MB_YESNO);
===============
你前面定义缓冲区是char 这里却用MessageBoxW,肯定乱码啊,用MessageBoxA吧

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧