看了《Windows图形编程》,新建一个Win 32空项目——Hello2,添加一个Hello2.cpp,把第二个例子的源码敲进Hello2.cpp,代码如下:
//-----------------------------------------------------------------------------------//
// Windows Graphics Programming: Win32 GDI and DirectDraw //
// ISBN 0-13-086985-6 //
// //
// Written by Yuan, Feng www.fengyuan.com //
// Copyright (c) 2000 by Hewlett-Packard Company www.hp.com //
// Published by Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com //
// //
// FileName : hello2.cpp //
// Description: Hellow World Demo 2, full screen display, Chapter 1 //
// Version : 1.00.001, July 26, 2000 //
//-----------------------------------------------------------------------------------//#define STRICT
#define WIN32_LEAN_AND_MEAN#include <windows.h>
#include <tchar.h>
#include <assert.h>void CenterText(HDC hDC, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point)
{
HFONT hFont = CreateFont(- point * GetDeviceCaps(hDC, LOGPIXELSY) / 72,
0, 0, 0, FW_BOLD, TRUE, FALSE, FALSE,
ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, VARIABLE_PITCH, szFace);
// assert(hFont); HGDIOBJ hOld = SelectObject(hDC, hFont); SetTextAlign(hDC, TA_CENTER | TA_BASELINE); SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(0, 0, 0xFF));
TextOut(hDC, x, y, szMessage, _tcslen(szMessage)); SelectObject(hDC, hOld);
DeleteObject(hFont);
}const TCHAR szMessage[] = _T("Hello, World");
const TCHAR szFace[] = _T("Times New Roman");#pragma comment(linker, "-merge:.rdata=.text")
#pragma comment(linker, "-align:512")extern "C" void WinMainCRTStartup()
{
HDC hDC = GetDC(NULL);
// assert(hDC); CenterText(hDC, GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CYSCREEN) / 2,
szFace, szMessage, 72); ReleaseDC(NULL, hDC);
ExitProcess(0);
}
然后编译生成Hello2.exe。双击Hello2.exe运行,提示Hello2.exe不是有效的Win32程序。请问如何把这个工程生成有效的Win32程序呢?
//-----------------------------------------------------------------------------------//
// Windows Graphics Programming: Win32 GDI and DirectDraw //
// ISBN 0-13-086985-6 //
// //
// Written by Yuan, Feng www.fengyuan.com //
// Copyright (c) 2000 by Hewlett-Packard Company www.hp.com //
// Published by Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com //
// //
// FileName : hello2.cpp //
// Description: Hellow World Demo 2, full screen display, Chapter 1 //
// Version : 1.00.001, July 26, 2000 //
//-----------------------------------------------------------------------------------//#define STRICT
#define WIN32_LEAN_AND_MEAN#include <windows.h>
#include <tchar.h>
#include <assert.h>void CenterText(HDC hDC, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point)
{
HFONT hFont = CreateFont(- point * GetDeviceCaps(hDC, LOGPIXELSY) / 72,
0, 0, 0, FW_BOLD, TRUE, FALSE, FALSE,
ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, VARIABLE_PITCH, szFace);
// assert(hFont); HGDIOBJ hOld = SelectObject(hDC, hFont); SetTextAlign(hDC, TA_CENTER | TA_BASELINE); SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(0, 0, 0xFF));
TextOut(hDC, x, y, szMessage, _tcslen(szMessage)); SelectObject(hDC, hOld);
DeleteObject(hFont);
}const TCHAR szMessage[] = _T("Hello, World");
const TCHAR szFace[] = _T("Times New Roman");#pragma comment(linker, "-merge:.rdata=.text")
#pragma comment(linker, "-align:512")extern "C" void WinMainCRTStartup()
{
HDC hDC = GetDC(NULL);
// assert(hDC); CenterText(hDC, GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CYSCREEN) / 2,
szFace, szMessage, 72); ReleaseDC(NULL, hDC);
ExitProcess(0);
}
然后编译生成Hello2.exe。双击Hello2.exe运行,提示Hello2.exe不是有效的Win32程序。请问如何把这个工程生成有效的Win32程序呢?
这个我知道。源码是VC 6的,现在我想把它在VS 2005上运行。请问如何设置呢?
#pragma comment(linker, "-align:512")
_________________________________________________
all : Hello2.exeHello2.obj : hello2.cpp
cl -MD hello2.cpphello2.exe : hello2.obj
link /entry:WinMainCRTStartup hello2.obj kernel32.lib gdi32.lib user32.lib 添加你需要的lib库文件clean:
del *.obj
del *.exe执行:nmake /a /f hello2.mak
兄弟,不是这个问题的。/ALIGN 选项指定程序线性地址空间中每一节的对齐方式。number 参数以字节为单位,并且必须是 2 的幂。默认值是 4K (4096)。如果对齐方式产生无效的图像,则链接器发出警告。除非正在编写诸如设备驱动程序的应用程序,否则应不需要修改对齐方式。可以用 /SECTION 选项的对齐参数修改特定节的对齐方式。指定的对齐值不能小于最大的节对齐。
#pragma comment(linker, "-align:512")去掉了。然后再命令行下编译能成功。