请教一个消息传递的问题(30分)

  • 主题发起人 主题发起人 awfigsk
  • 开始时间 开始时间
A

awfigsk

Unregistered / Unconfirmed
GUEST, unregistred user!
const<br> &nbsp; WM_MYMSG=WM_USER+100;<br>type<br> &nbsp; TForm1=class(TForm)<br> &nbsp; &nbsp;...<br> &nbsp; public<br> &nbsp; &nbsp; &nbsp;procedure WMMYMSG(var msg:TMessage); message WM_MYMSG;<br> &nbsp; end;<br><br>implementions<br><br>procedure TForm1.WMMYMSG(var Msg: TMessage);<br>var<br> &nbsp;p:TPoint;<br>begin<br> &nbsp; case [red]msg.LParam [/red]of<br> &nbsp; &nbsp; &nbsp;WM_RBUTTONDOWN:<br>....<br> &nbsp; &nbsp; &nbsp;WM_MOUSEMOVE:<br> ....<br> &nbsp; &nbsp; &nbsp;WM_LBUTTONUP:<br>....<br> &nbsp; end;<br>end;<br><br>我不明的是为何在case语句中是用msg.Lparam的值来判断?而不是用msg.msg或msg.WParam来进行判断是什么消息?如何才知道消息的值是放在msg的哪个字段里面?
 
通过<br>const<br> &nbsp; WM_MYMSG=WM_USER+100;<br>知道你这个消息是自定义的<br><br>至于判断msg.Lparam 要看发送的代码是怎么配置 光靠瞪眼法恐怕看不出来吧
 
能举个例子吗?<br><br>我是想在窗口缩小时将图标放到托盘中,托盘数据结构的中回调消息就是WM_MYMSG。<br>按您上面所述“要看发送的代码是怎么配置”,如何来知道是用MSG的哪个字段来判断消息值?
 
原来是做托盘啊<br><br>实际上你托盘向进程<br>发送 类似这样的消息<br>Sendmessage(Self.Handle, WM_MYMSG, 0 ,WM_MOUSEMOVE);<br><br>为什么判断msg.Lparam 实际是微软实现定义好的 具体你可以查看MSDN
 
但鼠标移动到托盘上的图标时,会自动触发wm_mymessage消息进行回调,但为何是用msg的lparam字段来判断消息值,为何不是msg.msg或msg.wparam字段呢?<br>我看了MSDN,上面只是解释了这三个字段的意思,并没说到类似上面的问题。<br><br>uCallbackMessage<br>Application-defined message identifier. The system uses this identifier to send notifications to the window identified in hWnd. These notifications are sent when a mouse event occurs in the bounding rectangle of the icon, or when the icon is selected or activated with the keyboard. [red]The wParam parameter of the message contains the identifier of the taskbar icon in which the event occurred. The lParam parameter holds the mouse or keyboard message associated with the event.[/red] For example, when the pointer moves over a taskbar icon, lParam is set to WM_MOUSEMOVE. See the Taskbar guide chapter for further discussion. <br><br>我看Shell_notify这个API函的参考时,看到回调消息的解释。其中WParam的参数是在事件发生时包含任务栏图标的标识?这是什么意思是指应用程序在托盘中图标的标识?也就是那个UID吗?<br>uID<br>Application-defined identifier of the taskbar icon. The Shell uses hWnd and uID to identify which icon to operate on when Shell_NotifyIcon is invoked. You can have multiple icons associated with a single hWnd by assigning each a different uID. <br>这个uID起个什么作用呢?
 
理论:<br>系统托盘是指任务条中的一个方形区域,在该区域中可以放入一些小图标,通常您可以在此处看到系统提供的最新时间。您自己当然也可以把快捷小图标放到此处。下面是这么做的步骤: <br>设置NOTIFYICONDATA型的结构体变量的成员变量的值: <br>cbSize 该结构体的大小。 <br>hwnd 窗口的句柄。当鼠标滑过该小图标时,该窗口将接收到相关的消息。 <br>uID 小图标的ID号。您可以取任意值,只是当您的应用程序有不止一个小图标时,您要能够区分出到底是那一个小图标接收到了鼠标的消息,也即ID号必须唯一。 <br>uFlags 指定该结构体变量的那些成员变量有效。 <br>NIF_ICON 有效。 <br>NIF_MESSAGE 有效。 <br>NIF_TIP 有效。 <br>uCallbackMessage 自定义的消息。当鼠标对小图标动作时,WINDOWS外壳将把该消息发送到您的应用程序。该消息的值您可以自己定义。 <br>hIcon 放入系统托盘中的图标的句柄。 <br>szTip 64字节的缓冲区,它用来放入提示字符串,当鼠标停留在小图标上时,就会显示该字符串。 <br>调用Shell_NotifyIcon函数。该函数在shell32.inc中定义,其原型如下: <br><br>Shell_NotifyIcon PROTO dwMessage:DWORD ,pnid:DWORD <br><br>dwMessage 是发送到WINDOWS外壳的消息:<br>NIM_ADD 把小图标加到系统托盘区。<br>NIM_DELETE 从系统托盘中删除小图标。<br>NIM_MODIFY 修改小图标。 <br>pnid 是指向NOTIFYICONDATA型结构体变量的指针。<br>如果您想要加入一个小图标就用NIM_ADD,删除时使用NIM_DELETE消息。<br><br>基本上的消息就是这些。但是大多数的情况下,您不会仅仅满足把一个小图标放到那里。您还必须要对鼠标事件作出适当的反应。您可以在NOTIFYICONDATA型的结构体变量的成员变量uCallbackMessage 中设置您要处理的消息,然后WINDOWS外壳将在发生这些事件时通知您的应用程序。随着消息传送的参数wParam和lParam的值如下: <br>wParam 小图标的ID号。它和您在NOTIFYICONDATA型结构体变量中的成员变量uID中设置的值一样。 <br>lParam 低字包含鼠标消息。譬如,用户在小图标上按下了右键时,lParam中将包含WM_RBUTTONDOWN消息。 <br>大多数的系统托盘中的小图标,在用户用鼠标右击时都会弹出一个菜单以方便用户选择。我们可先创建菜单,然后调用TrackPopupMenu函数来显示它。步骤如下: <br>调用CreatePopupMenu函数来创建菜单。该函数创建一个空的菜单。如果成功,将在eax中返回该菜单的句柄。 <br>调用AppendMenu, InsertMenu 或 InsertMenuItem来向菜单中加入菜单项。 <br>当您想在当前鼠标位置显示该菜单时,调用GetCursorPosition函数来得到鼠标当前的屏幕位置,然后调用TrackPopupMenu来显示菜单。当用户从弹出式菜单中选择了一个菜单项时,WINDOWS将发送WM_COMMAND消息给您应用程序的消息处理过程,这和通常的菜单选择是一样的。. <br>注意:当您使用系统托盘中的小图标时有两件比较讨厌的事: <br>该菜单可能不会像通常那样马上消失掉。这是因为从弹出式接收消息的窗口必须是前景窗口。调用SetForegroundWindow函数就可以纠正该错误; <br>在调用了SetForegroundWindow函数后,您会发现第一次该弹出式菜单会正常弹出而且工作的很好。但是随后,该菜单只是一弹出就立即消失。根据MSDN,这么做是故意的。为了使得弹出菜单保持住,必须要求下一个切换到的是程序的主窗口。您可以通过邮寄任何消息给该程序的窗口来强行进行任务切换。注意要使用PostMessage而不是SendMessage。
 
大侠,有QQ号或MSN吗?我的是21200151<br>有空还想向你请教一些问题?不知是否方便?<br><br>另外,您上面所述的“应用程序有不止一个小图标时?”是指应用程序在托盘中有多个图标吗?
 
我不明的是为何在case语句中是用msg.Lparam的值来判断?<br>具体用什么来判断要看具体是什么消息,<br>给你个例子既用到了wparam,又用到了lparam<br>建议看一看&lt;&lt;Windows程序设计&gt;&gt;这本书,我是初学者,我也正在看,上面写得很清楚.<br>还要看MSDN,或者Delphi自带的SDK帮助.<br>给你个例子既用到了wparam,又用到了lparam<br><br>program &nbsp;SysMets4;<br><br>uses<br> &nbsp;Windows, Messages, Math, SysMets;<br><br> // 窗体消息处理回调函数<br>function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;<br>const<br>{$J+}<br> &nbsp;cxChar: Integer = 0; &nbsp; &nbsp; &nbsp; // 小写字母宽度<br> &nbsp;cxCaps: Integer = 0; &nbsp; &nbsp; &nbsp; // 大写字母宽度<br> &nbsp;cyChar: Integer = 0; &nbsp; &nbsp; &nbsp; // 小写字母高度<br> &nbsp;cxClient: Integer = 0; &nbsp; &nbsp; // 窗体客户区宽度<br> &nbsp;cyClient: Integer = 0; &nbsp; &nbsp; // 窗体客户区高度<br> &nbsp;iMaxWidth: Integer = 0; &nbsp; &nbsp;// 文字输出行宽度<br>{$J-} &nbsp;<br>var<br> &nbsp;X, Y, J, L, iVertPos, iHorzPos, iPaintBeg, iPaintEnd: Integer;<br> &nbsp;Buffer: array [0..5] of Char; // 输出缓冲区 <br> &nbsp;hWndDc: HDC;<br> &nbsp;PS: TPaintStruct;<br> &nbsp;TM: TTextMetric;<br> &nbsp;SI: TScrollInfo;<br>begin<br> &nbsp;Result := 0;<br> &nbsp;case Msg of<br> &nbsp; &nbsp;WM_CREATE &nbsp; : begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hWndDc := GetDC(hWnd);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetTextMetrics(hWndDc, TM); // 取得字体度量信息<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cxChar := TM.tmAveCharWidth;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (TM.tmPitchAndFamily and $1) = 0 // 宽度固定?<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;then cxCaps := cxChar<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else cxCaps := cxChar * 3 div 2;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cyChar := TM.tmHeight + TM.tmExternalLeading;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMaxWidth := 40 * cxChar + 22 * cxCaps;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReleaseDC(hWnd, hWndDc);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;WM_SIZE &nbsp; &nbsp; : begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cxClient := LOWORD(lParam);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cyClient := HIWORD(lParam);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 设置竖直卷动条范围和单页大小<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.cbSize := SizeOf(SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.fMask &nbsp;:= SIF_RANGE or SIF_PAGE;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.nMin := 0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.nMax := NUMLINES - 1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.nPage := cyClient div cyChar;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetScrollInfo(hWnd, SB_VERT, SI, TRUE);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 设置横向卷动条范围和单页大小<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.cbSize := SizeOf(SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.fMask := SIF_RANGE or SIF_PAGE;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.nMin := 0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.nMax := 2 + iMaxWidth div cxChar;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.nPage := cxClient div cxChar;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetScrollInfo(hWnd, SB_HORZ, SI, TRUE);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp;WM_VSCROLL &nbsp;: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 取得竖直卷动条全部信息<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.cbSize := SizeOf(SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.fMask := SIF_ALL;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetScrollInfo(hWnd, SB_VERT, SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 暂存竖直卷动条位置信息<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iVertPos := SI.nPos;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 区分不同的卷动类型<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case LOWORD(wParam) of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_TOP &nbsp; &nbsp; &nbsp; : SI.nPos := SI.nMin;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_BOTTOM &nbsp; &nbsp;: SI.nPos := SI.nMax;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_LINEUP &nbsp; &nbsp;: SI.nPos := SI.nPos-1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_LINEDOWN &nbsp;: SI.nPos := SI.nPos+1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_PAGEUP &nbsp; &nbsp;: SI.nPos := SI.nPos-SI.nPage;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_PAGEDOWN &nbsp;: SI.nPos := SI.nPos+SI.nPage;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_THUMBTRACK: SI.nPos := SI.nTrackPos;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.fMask := SIF_POS;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetScrollInfo(hWnd, SB_VERT, SI, TRUE);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetScrollInfo(hWnd, SB_VERT, SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 与以前不同则上下滚动(产生无效区域)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (SI.nPos &lt;&gt; iVertPos) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ScrollWindow(hWnd, 0, cyChar * (iVertPos - SI.nPos), &nbsp;nil, nil);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UpdateWindow(hWnd);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;WM_HSCROLL &nbsp;: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 取得横向卷动条全部信息<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.cbSize := SizeOf(SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.fMask := SIF_ALL;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetScrollInfo(hWnd, SB_HORZ, SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 暂存横向卷动条位置信息<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iHorzPos := SI.nPos;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 区分不同的卷动类型<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case LOWORD(wParam) of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_LINELEFT &nbsp;: SI.nPos := SI.nPos-1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_LINERIGHT : SI.nPos := SI.nPos+1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_PAGELEFT &nbsp;: SI.nPos := SI.nPos-SI.nPage;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_PAGERIGHT : SI.nPos := SI.nPos+SI.nPage;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_THUMBPOSITION: SI.nPos := SI.nTrackPos;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.fMask := SIF_POS;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetScrollInfo(hWnd, SB_HORZ, SI, TRUE);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetScrollInfo(hWnd, SB_HORZ, SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 与以前不同则左右滚动(产生无效区域)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (SI.nPos &lt;&gt; iHorzPos) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ScrollWindow(hWnd, cxChar * (iHorzPos - SI.nPos), 0, nil, nil);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UpdateWindow(hWnd);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;WM_KEYDOWN &nbsp;: begin // 转发滚动消息<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case wParam of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VK_HOME &nbsp;: &nbsp;SendMessage(hWnd, WM_VSCROLL, SB_TOP, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VK_END &nbsp; : &nbsp;SendMessage(hWnd, WM_VSCROLL, SB_BOTTOM, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VK_PRIOR : &nbsp;SendMessage(hWnd, WM_VSCROLL, SB_PAGEUP, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VK_NEXT &nbsp;: &nbsp;SendMessage(hWnd, WM_VSCROLL, SB_PAGEDOWN, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VK_UP &nbsp; &nbsp;: &nbsp;SendMessage(hWnd, WM_VSCROLL, SB_LINEUP, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VK_DOWN &nbsp;: &nbsp;SendMessage(hWnd, WM_VSCROLL, SB_LINEDOWN, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VK_LEFT &nbsp;: &nbsp;SendMessage(hWnd, WM_HSCROLL, SB_PAGEUP, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VK_RIGHT : &nbsp;SendMessage(hWnd, WM_HSCROLL, SB_PAGEDOWN, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;WM_PAINT &nbsp; &nbsp;: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hWndDc := BeginPaint(hWnd, PS);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.cbSize := SizeOf(SI) ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI.fMask := SIF_POS;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 取得竖直卷动条位置<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetScrollInfo(hWnd, SB_VERT, SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iVertPos := SI.nPos ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 取得横向卷动条位置<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetScrollInfo(hWnd, SB_HORZ, SI);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iHorzPos := SI.nPos;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 计算需要重绘的范围(行)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iPaintBeg := Max(0, iVertPos + PS.rcPaint.Top div cyChar);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iPaintEnd := Min(NUMLINES - 1, iVertPos + PS.rcPaint.Bottom div cyChar);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 依次绘制各行<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for J := iPaintBeg to iPaintEnd do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;X := cxChar * (1 - iHorzPos); // 本行横坐标(左上角)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Y := cyChar * (J - iVertPos); // 本行纵坐标(左上角)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextOut(hWndDc, X, Y, SysMetrics[J].szLabel, lstrlen(SysMetrics[J].szLabel));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextOut(hWndDc, X + 22 * cxCaps, Y, SysMetrics[J].szDesc, lstrlen(SysMetrics[J].szDesc));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetTextAlign(hWndDc, TA_RIGHT or TA_TOP); &nbsp;// 右上对齐<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L := GetSystemMetrics(SysMetrics[J].Index);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextOut(hWndDc, X + 22 * cxCaps + 40 * cxChar , Y, Buffer, wvsprintf(Buffer, '%5d', @L));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetTextAlign(hWndDc, TA_LEFT or TA_TOP); // 左上对齐<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EndPaint(hWnd, PS);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp;WM_DESTROY &nbsp;: PostQuitMessage(0); // 退出循环<br><br> &nbsp; &nbsp;else Result := DefWindowProc(hWnd, Msg, wParam, lParam); // 默认处理<br> &nbsp;end;<br>end;<br><br><br>const<br> &nbsp;AppName = 'SysMets4';<br> &nbsp;<br>var<br> &nbsp;hWnd: LongWord;<br> &nbsp;Msg: TMsg;<br> &nbsp;WndClass: TWndClass;<br><br>begin<br> // 填充结构体<br> &nbsp;WndClass.style := CS_HREDRAW or CS_VREDRAW;<br> &nbsp;WndClass.lpfnWndProc := @WndProc;<br> &nbsp;WndClass.cbClsExtra := 0;<br> &nbsp;WndClass.cbWndExtra := 0;<br> &nbsp;WndClass.hInstance := hInstance;<br> &nbsp;WndClass.hIcon := LoadIcon(0, IDI_APPLICATION);<br> &nbsp;WndClass.hCursor := LoadCursor(0, IDC_ARROW);<br> &nbsp;WndClass.hbrBackground := GetStockObject(WHITE_BRUSH);<br> &nbsp;WndClass.lpszMenuName := nil;<br> &nbsp;WndClass.lpszClassName := AppName;<br><br> // 注册窗体类<br> &nbsp;if (RegisterClass(WndClass) = 0) then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,'Program requires Windows NT!', AppName, MB_ICONERROR);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br> // 建立窗体<br> &nbsp;hWnd := CreateWindow(AppName, 'Get System Metrics No. 4',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WS_OVERLAPPEDWINDOW or WS_VSCROLL or WS_HSCROLL,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CW_USEDEFAULT, CW_USEDEFAULT,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CW_USEDEFAULT, CW_USEDEFAULT,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, 0, hInstance, nil);<br><br> // 显示窗体<br> &nbsp;ShowWindow(hWnd, CmdShow);<br> &nbsp;UpdateWindow(hWnd);<br> &nbsp;<br> // 消息循环<br> &nbsp;while GetMessage(Msg, 0, 0, 0) do<br> &nbsp;begin<br> &nbsp; &nbsp;TranslateMessage(Msg);<br> &nbsp; &nbsp;DispatchMessage(Msg);<br> &nbsp;end;<br>end.
 
欣赏楼主的钻研精神 呵呵
 
有不对的地方,楼下大虾扶正
 
呵。。。很感谢二位富翁的指点,用哪个字段来判断,看样子还是要多看MSDN上的帮助了,比如WM_SYSCOMMAND这个消息,在处理时,就要用WPARAM这个字段来进行判断,在MSDN上说的很详细了,我开始也没有认真看,因为全是英文的,后来在AVALON大侠提到看MSDN,我才又仔细查看了一下,果真上面对托盘回调消息讲得比较清楚了。呵。。看样子,还是得多多学习英文呀,,《windows程序设计》这本书我也看了,但还是有些地方写得不太详细,自己在编程时对消息处理起来还是觉得有些吃力,有空还请二位大侠多多指点,谢谢!
 
后退
顶部