导航
  • 主页
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

关于SDK的问题小弟求教了

mickeycao 2004-04-01 11:49:26
#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("KeyView2");
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"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(!hwnd)
MessageBox(NULL, TEXT("Window Create Error"), szAppName, MB_ICONERROR);


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 DWORD dwCharSet = DEFAULT_CHARSET ;
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[2] = {
TEXT("% -13s %3d % - 15s%c%6u %4d %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[128], szKeyName [32] ;
TEXTMETRIC tm;




switch (message)
{
case WM_INPUTLANGCHANGE:
dwCharSet = wParam ;




case WM_CREATE:
case WM_DISPLAYCHANGE:
//get maximum size of client area
cxClientMax = GetSystemMetrics (SM_CXMAXIMIZED) ;
cyClientMax = GetSystemMetrics (SM_CYMAXIMIZED) ;
//get maximum size of fixed-pitch font
hdc = GetDC(hwnd);



SelectObject (hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight ;



DeleteObject( SelectObject (hdc, GetStockObject(SYSTEM_FONT)));
ReleaseDC ( hwnd, hdc) ;
//Allocate memory for display lines
if (pmsg)
free (pmsg) ;

cLinesMax = cyClientMax / cyChar ;
pmsg = (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);


if ( message == WM_INPUTLANGCHANGE)
return 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[i] = pmsg[i - 1] ;

} //Store new message
pmsg[0].hwnd = hwnd ;
pmsg[0].message = message ;
pmsg[0].wParam = wParam ;
pmsg[0].lParam =lParam ;



cLines = min (cLines + 1, cLinesMax) ;
//Scroll up the dislplay
ScrollWindow ( hwnd , 0 ,- cyChar, &rectScroll, &rectScroll) ;
break; //ie , call DefWindowProc so Ss Messages work



case WM_PAINT:
hdc = BeginPaint( hwnd , &ps) ;
SelectObject (hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
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[i].message == WM_CHAR ||
pmsg[i].message == WM_SYSCHAR ||
pmsg[i].message == WM_DEADCHAR ||
pmsg[i].message == WM_SYSDEADCHAR;


GetKeyNameText ( pmsg[i].lParam, szKeyName,
sizeof (szKeyName) /sizeof(TCHAR)) ;


TextOut(hdc, 0, (cyClient / cyChar - 1 - i) * cyChar, szBuffer,
wsprintf(szBuffer, szFormat[iType],
szMessage[pmsg[i].message - WM_KEYFIRST],
pmsg[i].wParam,
(PTSTR)(iType ? TEXT(" ") : szKeyName),
(TCHAR)(iType ? pmsg[i].wParam : ' '),
LOWORD(pmsg[i].lParam),
HIWORD(pmsg[i].lParam) & 0xFF,
0x01000000 & pmsg[i].lParam ? szYes : szNo,
0x2000000 & pmsg[i].lParam ? szYes : szNo,
0x40000000 & pmsg[i].lParam ? szDown : szUp,
0x80000000 & pmsg[i].lParam ? szUp : szDown) ) ;



}
DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;

EndPaint(hwnd,&ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;



}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
keyview2.exe 中的 0x77df76d3 处最可能的异常: 0xC0000005: 读取位置 0x00000074 时发生访问冲突 。
keyview2.exe 中的 0x77df76d3 处未处理的异常: 0xC0000005: 读取位置 0x00000074 时发生访问冲突 。
为什么啊
...全文
14 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mickeycao 2004-04-02
谢谢fvv大哥了
回复
fvv 2004-04-02
static TCHAR * szFormat[2] = {
TEXT("% -13s %3d % - 15s%c%6u %4d %3s %4s %4s"),
TEXT("% -13s 0x%04X%1s%c %6u %4d %3s %3s %4s %4s")};

%和类型之间不能有空格.这样TEXT("%-13s ......)
你把-13 和-15前面的空格去了就行了.
回复
名牌大灰狼 2004-04-02
我也关注.你为什么要自己写窗体呀? :>
回复
关注
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告

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