程序其它部分运行正常,添加了一个Custom Resources -- text1.bin应无误,text1.bin内有一行文本,无法用DrawTextA显示出来,为什么?谢谢#include <windows.h>
#include "resource.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;HINSTANCE hInst ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
WNDCLASS wndclass ;
TCHAR szAppName [16], szCaption [64], szErrMsg [64] ;
HWND hwnd ;
MSG msg ; hInst = hInstance;
LoadString (hInstance, IDS_APPNAME, szAppName,
sizeof (szAppName) / sizeof (TCHAR)) ; LoadString (hInstance, IDS_CAPTION, szCaption,
sizeof (szCaption) / sizeof (TCHAR)) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
LoadStringA (hInstance, IDS_APPNAME, (char *) szAppName,
sizeof (szAppName)) ;
LoadStringA (hInstance, IDS_ERRMSG, (char *) szErrMsg,
sizeof (szErrMsg)) ;
MessageBoxA (NULL, (char *) szErrMsg,
(char *) szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szCaption,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
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 (int)msg.wParam ;
}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
TEXTMETRIC tm ;
static char * pText ;
static HGLOBAL hResource ;
static int cxChar, cyChar, iNumLines;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE :
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
ReleaseDC (hwnd, hdc) ;
hResource = LoadResource (hInst,
FindResource (hInst, TEXT ("IDR_TEXT1"),
TEXT ("TEXT"))) ; /* pText = (char *) LockResource (hResource) ;
iNumLines = 0 ;
while (*pText != '\\' && *pText != '\0')
{
if (*pText == '\n')
iNumLines ++ ;
pText = AnsiNext (pText) ;
}
*pText = '\0' ;*/
return 0; case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
pText = (char *) LockResource (hResource) ; GetClientRect (hwnd, &rect) ;
/*rect.left += cxChar ;
rect.top += cyChar * (1 - iPosition) ;*/ DrawTextA (hdc,pText, -1, &rect, DT_EXTERNALLEADING) ; EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
FreeResource (hResource) ; PostQuitMessage (0) ;
return 0 ;
} return DefWindowProc (hwnd, message, wParam, lParam) ;
}
/////////////////////////////////////////////////////////////////////////////
//
// TEXT
//
IDR_TEXT1 TEXT "text1.bin"
/////////////////////////////////////////////////////////////////////////////
//
// String Table
//
#include "resource.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;HINSTANCE hInst ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
WNDCLASS wndclass ;
TCHAR szAppName [16], szCaption [64], szErrMsg [64] ;
HWND hwnd ;
MSG msg ; hInst = hInstance;
LoadString (hInstance, IDS_APPNAME, szAppName,
sizeof (szAppName) / sizeof (TCHAR)) ; LoadString (hInstance, IDS_CAPTION, szCaption,
sizeof (szCaption) / sizeof (TCHAR)) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
LoadStringA (hInstance, IDS_APPNAME, (char *) szAppName,
sizeof (szAppName)) ;
LoadStringA (hInstance, IDS_ERRMSG, (char *) szErrMsg,
sizeof (szErrMsg)) ;
MessageBoxA (NULL, (char *) szErrMsg,
(char *) szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szCaption,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
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 (int)msg.wParam ;
}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
TEXTMETRIC tm ;
static char * pText ;
static HGLOBAL hResource ;
static int cxChar, cyChar, iNumLines;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE :
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
ReleaseDC (hwnd, hdc) ;
hResource = LoadResource (hInst,
FindResource (hInst, TEXT ("IDR_TEXT1"),
TEXT ("TEXT"))) ; /* pText = (char *) LockResource (hResource) ;
iNumLines = 0 ;
while (*pText != '\\' && *pText != '\0')
{
if (*pText == '\n')
iNumLines ++ ;
pText = AnsiNext (pText) ;
}
*pText = '\0' ;*/
return 0; case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
pText = (char *) LockResource (hResource) ; GetClientRect (hwnd, &rect) ;
/*rect.left += cxChar ;
rect.top += cyChar * (1 - iPosition) ;*/ DrawTextA (hdc,pText, -1, &rect, DT_EXTERNALLEADING) ; EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
FreeResource (hResource) ; PostQuitMessage (0) ;
return 0 ;
} return DefWindowProc (hwnd, message, wParam, lParam) ;
}
/////////////////////////////////////////////////////////////////////////////
//
// TEXT
//
IDR_TEXT1 TEXT "text1.bin"
/////////////////////////////////////////////////////////////////////////////
//
// String Table
//
iNumLines = 0 ;
while (*pText != '\\' && *pText != '\0')
{
if (*pText == '\n')
iNumLines ++ ;
pText = AnsiNext (pText) ;
}
*pText = '\0' ;*/
code]
干嘛注释掉?这分明是从bin文件里面找出字符串嘛.显然这个bin文件里面不是以字符串开头的。你调试的时候看一下pText里面是否有正确的值。
hResource = LoadResource (hInst,
FindResource (hInst, MAKEINTRESOURCE(IDR_TEXT1),
RT_STRING)) ;
FindResource (hInst, MAKEINTRESOURCE(IDR_TEXT1),
TEXT ("TEXT"))) ;即可,不知为何
倒不是没找到结束符,用原语句,跟踪hResource ,是0x0000000,这里就调用失败了