为什么书中的程序在win10下不正常
本帖最后由 superbe 于 2019-11-11 19:39 编辑《windows程序设计》第14章bitblt程序,运行结果应该是客户区铺满系统菜单图标,但在xp下正常,而在win10下运行要么空白,要么显示奇怪的东西。
编译生成没有报错。代码是从随书源代码里复制粘贴过来的。
win10下运行截图:
不知道是什么原因。
PS:源代码见 4 楼。
win10下测试了下,是这种效果呀,并非一片空白。
最好贴一下你的代码和程序。 《Windows程序设计》这本书历史久远,所以往往调试的时候会有各种问题,主要排查思路:
1. IDE最好用VC6.0+VC assitant,VS或其他编译器会遇到更多的问题
2. 查看编码环境是否是Unicode,不是你包含的库文件,而是你的编译器环境
3. windows 程序的编译有独特的入口顺序,查看函数的次序是否排列得当
4. 针对图中的问题,大致在你获取“客户区”的cxClient、cyClient和cxChar、cyChar的设置不正确
本帖最后由 superbe 于 2019-11-11 14:46 编辑
源代码如下,请大神看看如何修改才能在win10下正常运行。测试无论环境是否UNICODE都不正常。
/*---------------------------------------
BITBLT.C -- BitBlt Demonstration
(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 ("BitBlt") ;
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_INFORMATION) ;
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 ("BitBlt Demo"),
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 intcxClient, cyClient, cxSource, cySource ;
HDC hdcClient, hdcWindow ;
int x, y ;
PAINTSTRUCT ps ;
switch (message)
{
case WM_CREATE:
cxSource = GetSystemMetrics (SM_CXSIZEFRAME) +
GetSystemMetrics (SM_CXSMICON) ;
cySource = GetSystemMetrics (SM_CYSIZEFRAME) +
GetSystemMetrics (SM_CYCAPTION) ;
return 0 ;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_PAINT:
hdcClient = BeginPaint (hwnd, &ps) ;
hdcWindow = GetWindowDC (hwnd) ;
for (y = 0 ; y < cyClient ; y += cySource)
for (x = 0 ; x < cxClient ; x += cxSource)
{
BitBlt (hdcClient, x, y, cxSource, cySource,
hdcWindow, 0, 0, SRCCOPY) ;
}
ReleaseDC (hwnd, hdcWindow) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
我的WIN10 执行正常
页:
[1]