付笑 发表于 2013-11-20 19:02:57

WM_SYSDEADCHAR 消息问题

代码如下:

/*--------------------------------------------------------
KEYVIEW1.C -- Displays Keyboard and Character Messages
(c) Charles Petzold, 1998
--------------------------------------------------------*/

#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 ("KeyView1") ;
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 ("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 PMSG pmsg ;
static RECT rectScroll ;
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 %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:

// Get maximum size of client area

cxClientMax = GetSystemMetrics (SM_CXMAXIMIZED) ;
cyClientMax = GetSystemMetrics (SM_CYMAXIMIZED) ;

// Get character size for fixed-pitch font

hdc = GetDC (hwnd) ;

SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight ;

ReleaseDC (hwnd, hdc) ;

// Allocate memory for display lines

if (pmsg)
free (pmsg) ;

cLinesMax = cyClientMax / cyChar ;
pmsg = malloc (cLinesMax * sizeof (MSG)) ;
cLines = 0 ;
// fall through
case WM_SIZE:
if (message == WM_SIZE)
{
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
}
// Calculate scrolling rectangle

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:

// Rearrange storage array

for (i = cLinesMax - 1 ; i > 0 ; i--)
{
pmsg = pmsg ;
}
// Store new message

pmsg.hwnd = hwnd ;
pmsg.message = message ;
pmsg.wParam = wParam ;
pmsg.lParam = lParam ;

cLines = min (cLines + 1, cLinesMax) ;

// Scroll up the display

ScrollWindow (hwnd, 0, -cyChar, &rectScroll, &rectScroll) ;

break ; // ie, call DefWindowProc so Sys messages work

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) ;
}

调试这个程序的时候发现,只要按下任意一个键 窗口过程就会收到WM_SYSDEADCHAR消息
这个消息不是要按Alt+某键 才会产生吗,为什么随便按一个键就会有WM_SYSDEADCHAR 这个消息呢
求解答

如梦幻泡影 发表于 2013-11-26 22:14:16

本帖最后由 如梦幻泡影 于 2013-11-26 22:15 编辑

WM_SYSDEADCHAR
The WM_SYSDEADCHAR message is sent to the window with the keyboard focus when a WM_SYSKEYDOWN message is translated by the TranslateMessage function. WM_SYSDEADCHAR specifies the character code of a system dead key — that is, a dead key that is pressed while holding down the alt key.

感觉跟参数有关系的,感觉而已啊==!
29 Specifies the context code. The value is 1 if the ALT key is down while the key is pressed; it is 0 if the WM_SYSKEYDOWN message is posted to the active window because no window has the keyboard focus.
//谷歌翻译:29指定上下文代码。该值是1 ,如果Alt键不放,同时按下键,否则,该值为0

判断是否有按下alt,还可以用GetKeyState这个来判断
页: [1]
查看完整版本: WM_SYSDEADCHAR 消息问题