日期:2014-05-17  浏览次数:21248 次

win32鼠标事件,没有单击但是自动发出WM_LBUTTONDOWN,求解释
我在做鼠标事件的测试时,没有单击窗口,但是用于测试的控制台却显示有了一个WM_LBUTTONDOWN信息,
当我单击后却又多出了一个WM_LBUTTONDOWN信息,求大神解释原因,小弟不胜感谢。
代码如下:
// WinMouse.cpp : Defines the entry point for the application.
//

#include <windows.h>
#include <stdio.h>

HINSTANCE g_hInst = NULL;
HANDLE g_hStdOut = NULL;
int g_nXpos = 0;
int g_nYpos = 0;
int g_nXupperLeftCorner = 0;
int g_nYupperLeftCorner = 0;
int g_nXdownrightCorner = 0;
int g_nYdownrightCorner = 0;
void PrintLog(LPSTR pszLog)
{
WriteConsole(g_hStdOut,pszLog,strlen(pszLog),NULL,NULL);

void OnPaint(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
// CHAR szText[] = "Hello Paint";
PAINTSTRUCT ps = {0};
HDC hDc = BeginPaint(hWnd,&ps);
// TextOut(hDc,g_nXpos,g_nYpos,szText,strlen(szText));
// Rectangle(hDc,g_nXupperLeftCorner,g_nYupperLeftCorner,g_nXdownrightCorner,g_nYdownrightCorner);
EndPaint(hWnd,&ps);
}
LRESULT CALLBACK WndProc(HWND hWnd,
 UINT uMsg,
 WPARAM wParam,
 LPARAM lParam)
{
CHAR szText[100] = {0};
switch (uMsg)
{
case WM_PAINT:
OnPaint(hWnd,uMsg,wParam,lParam);
case WM_LBUTTONDOWN:
{
PrintLog("WM_LBUTTONDOWN\n");
g_nXupperLeftCorner = LOWORD(lParam);
g_nYupperLeftCorner = HIWORD(lParam);
sprintf(szText,"x1:%d y1:%d\n",g_nXupperLeftCorner,g_nYupperLeftCorner);
PrintLog(szText);
//  InvalidateRect(hWnd,NULL,TRUE);
// UpdateWindow(hWnd);
}
break;
case WM_LBUTTONUP:
{
PrintLog("WM_LBUTTONUP\n");
g_nXdownrightCorner = LOWORD(lParam);
g_nYdownrightCorner = HIWORD(lParam);
sprintf(szText,"x2:%d y2:%d\n",g_nXdownrightCorner,g_nYdownrightCorner);
PrintLog(szText);
InvalidateRect(hWnd,NULL,FALSE);
UpdateWindow(hWnd);
}

break;
case WM_RBUTTONUP:
PrintLog("WM_RBUTTONUP\n");
break;
case WM_RBUTTONDOWN:
PrintLog("WM_RBUTTONDOWN\n");
break;
case WM_MOUSEMOVE:
{
//  int nX = LOWORD(lParam);
//  int nY = HIWORD(lParam);
//  if(wParam & MK_LBUTTON)
//  {
// //  g_nXpos = nX;
// //  g_nYpos = nY;
//  g_nXdownrightCorner = LOWORD(lParam);
//  g_nYdownrightCorner = HIWORD(lParam);
//  InvalidateRect(hWnd,NULL,TRUE);
//  UpdateWindow(hWnd);
//  }

//  LPPOINT ptScreen = new POINT;
//  ptScreen->x = nX;
//  ptScreen->y = nY;
//  ClientToScreen(hWnd,ptScreen);
//  sprintf(szText,"x:%d(%d) y:%d(%d)",nX,ptScreen->x,nY,ptScreen->y);
//  PrintLog(szText);
//  PrintLog("WM_MOUSEMOVE\n");
//  delete(ptScreen);
//  if(wParam & MK_CONTROL)
//  {
//  PrintLog("WM_MOUSEMOVE: MK_CONTROL\n");
//  }
//  if(wParam & MK_LBUTTON)
//  {
//  PrintLog("WM_MOUSEMOVE: MK_LBUTTON\n");