读书人

刚学windows编程下面代码异常。不知

发布时间: 2012-04-01 17:23:46 作者: rapoo

刚学windows编程,下面代码错误。不知何解

C/C++ code
 
#include <windows.h>
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("xx");
HWND hwnd;
MSG msg;
WNDCLASSEX wndclassex = {0};
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = szAppName;
wndclassex.hIconSm = wndclassex.hIcon;

if (!RegisterClassEx (&wndclassex))
{
MessageBox (NULL, TEXT ("RegisterClassEx failed!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW,
szAppName,
TEXT ("good"),
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)
{
HDC hdc;
PAINTSTRUCT ps;
SCROLLINFOsi;
TEXTMETRIC tm;

static struct
{
int idStockFont;
char * szStockFont;
}
stockfont[] =
{
OEM_FIXED_FONT,"OEM_FIXED_FONT",
ANSI_FIXED_FONT, "ANSI_FIXED_FONT",
ANSI_VAR_FONT,"ANSI_VAR_FONT",
SYSTEM_FONT,"SYSTEM_FONT",
DEVICE_DEFAULT_FONT,"DEVICE_DEFAULT_FONT",
SYSTEM_FIXED_FONT, "SYSTEM_FIXED_FONT",
DEFAULT_GUI_FONT, "DEFAULT_GUI_FONT"
};
int i;
static int iFont, cFont = sizeof(stockfont)/sizeof(stockfont[0]);
static int cxChar, cyChar, cxClient, cyClient, iVerPos;


TCHAR szFaceName[LF_FACESIZE], szBuffer[LF_FACESIZE + 64];
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight+tm.tmExternalLeading;
GetTextMetrics(hdc, &tm);

ReleaseDC(hwnd, hdc);
return (0);
case WM_SIZE:

cyClient = LOWORD(lParam);
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_POS;

si.nMax = cFont-1;
si.nMin = 0;
si.nPage = cyClient/cyChar;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
return(0);

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);

for(i = 0; i < cFont; i++)
{
SelectObject(hdc, GetStockObject(stockfont[i].idStockFont));
GetTextFace(hdc, LF_FACESIZE, szFaceName);
GetTextMetrics(hdc, &tm);
TextOut(hdc, 0, i+3, szBuffer,
wsprintf(szBuffer, TEXT("%s: Face Name = %s, CharSet = %s"), stockfont[i].szStockFont,
szFaceName, tm.tmCharSet));
}

EndPaint (hwnd, &ps);
return (0);
case WM_VSCROLL:
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_VERT, &si);
iVerPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_TOP:
si.nPos = 0;
break;
case SB_BOTTOM:
si.nPos = cFont-1;
break;
case SB_PAGEUP:
si.nPos -= si.nPage;
break;
case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
case SB_THUMBPOSITION:
si.nPos = si.nTrackPos;
break;
default:
break;
}
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
si.fMask = SIF_POS;
GetScrollInfo(hwnd, SB_VERT, &si);
if( iVerPos != si.nPos)
{
ScrollWindow(hwnd, 0, (iVerPos - si.nPos)*cyChar, NULL, NULL);
UpdateWindow(hwnd);
}

return(0);

case WM_DESTROY:
PostQuitMessage (0);
return (0);
}
return DefWindowProc (hwnd, message, wParam, lParam);
}


// 发觉windows 程序调试和 C语言调试 大不一样,希望前辈能够 教我下如何 调试windows程序,跳入系统函数都是汇编代码,跳过又 进不了 窗口过程,。。。非常不解。。



[解决办法]
窗口过程

ctrl+F10
[解决办法]
设置断点后用F5 运行到断点
之后,可以F10按函数运行
也可以F11进入函数


[解决办法]
设置断点后用F5 运行到断点
之后,可以F10按函数运行
也可以F11进入函数
[解决办法]
1. 系统API你没办法看到源码的,跟踪进去也没用。
2. 由于windows程序是基于消息机制的,所以你也办法一口气跟踪到LRESULT CALLBACK WndProc 函数中,应该在此函数中设置断点后全速运行,等程序停在断点处再继续跟踪。CALLBACK这种类型的函数是系统帮你调用的,你只有等待它被调用后,才能跟踪下去。

读书人网 >C++

热点推荐