wolaicici 发表于 2013-10-14 03:41:31

新手请教Windows编程"KEYVIEW"程序

# include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
   PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("KeyView");
HWND      hwnd;
MSG       msg;
WNDCLASSwndclass;

    wndclass.cbClsExtra    = 0;
wndclass.cbWndExtra    = 0;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hInstance   = hInstance;
wndclass.lpfnWndProc   = WndProc;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName= NULL;
wndclass.style         = CS_HREDRAW | CS_VREDRAW;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
   szAppName, MB_ICONERROR);
return 0;
}

hwnd = CreateWindow (szAppName, TEXT ("Keyboard Message Viewer #1"), 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 PMSGpmsg;
static RECTrectScroll;
static TCHAR szTop[] = TEXT ("Message Key Char *")
                     TEXT ("Repeat Scan Ext ALT Prev Tran");
static TCHAR szUnd[] = TEXT ("------- --- ------")
                     TEXT ("------ ---- --- --- ---- ----");
static TCHAR * szFormat = {
TEXT ("%-13s %3d %-15s%c%6u %4d %3s %3s %4s %4s"),
TEXT ("%-13s            0x%04X%1s%c %6u %4d %ss %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;
PAINTSTRUCTps;
TCHAR      szBuffer, szKeyName;
TEXTMETRICtm;
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));
cLines = 0;
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);

}

--------------------Configuration: KEYVIEW1 - Win32 Debug--------------------
Compiling...
KEYVIEW1.CPP
D:\Program Files\Microsoft Visual Studio\MyProjects\KEYVIEW1\KEYVIEW1.CPP(100) : error C2440: '=' : cannot convert from 'void *' to 'struct tagMSG *'
      Conversion from 'void*' to pointer to non-'void' requires an explicit cast
执行 cl.exe 时出错.
KEYVIEW1.OBJ - 1 error(s), 0 warning(s)

大家好,以上是新手自己从Window书上抄的代码,研究了半天但还是看不懂错误提示的意思。请高手指点一下,非常感谢!

liufei_vip 发表于 2013-10-14 09:27:14

pmsg =(struct tagMSG *)malloc (cLinesMax * sizeof (MSG));
PS:你给的代码不全吧。。。

苹果沃珂 发表于 2013-10-14 09:39:15

使用malloc分配的动态内存是要指定数据类型:
pmsg =(PMSG)malloc (cLinesMax * sizeof (MSG));

风之残月 发表于 2013-10-14 18:55:42

pmsg = malloc (cLinesMax * sizeof (MSG))改为pmsg = (PMSG)malloc (cLinesMax * sizeof (MSG))试试

wolaicici 发表于 2013-10-15 03:04:42

原来是这样的,可以运行了,谢谢楼上几位高手{:1_1:}
页: [1]
查看完整版本: 新手请教Windows编程"KEYVIEW"程序