16,472
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include "resource.h"
#define UNICODE
#define ID_SCROLL 1
HINSTANCE hInst;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
TCHAR szAppName[16], szCaption[64], szErrMsg[64];
HWND hwnd;
MSG msg;
LoadString (hInstance, IDS_APPNAME, szAppName, sizeof (szAppName) / sizeof (TCHAR));
LoadString (hInstance, IDS_CAPTION, szCaption, sizeof (szCaption) / sizeof (TCHAR));
LoadString (hInstance, IDS_ERRMSG, szErrMsg, sizeof (szErrMsg) / sizeof (TCHAR));
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 (hInstance, MAKEINTRESOURCE (IDI_ICON));
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, szErrMsg, szCaption, MB_ICONERROR);
return 0;
}
hInst = hInstance;
hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW,
szAppName,
szCaption,
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 int cxChar, cyChar, iNumLines, cyClient, iPosition;
static int xScroll;
static HWND hwndScroll;
static HGLOBAL hResource;
HDC hdc;
TCHAR * pText;
RECT rect;
PAINTSTRUCT ps;
TEXTMETRIC tm;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd);
GetTextMetrics (hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC (hwnd, hdc);
xScroll = GetSystemMetrics (SM_CXVSCROLL);
hwndScroll = CreateWindow (TEXT("scroll"), NULL,
WS_VISIBLE | WS_CHILD | SBS_VERT,
0, 0, 0, 0, hwnd,
(HMENU) ID_SCROLL, hInst, NULL);
hResource = LoadResource (hInst,
FindResource (hInst, MAKEINTRESOURCE (IDR_TXT), TEXT("TXT")));
pText = (TCHAR *) LockResource (hResource);
iNumLines = 0;
while ( * pText != '\\' && pText != '\0')
{
if ( * pText == '\n')
{
iNumLines++;
}
pText = CharNext(pText);
}
* pText = '\0';
SetScrollRange (hwndScroll, SB_CTL, 0, iNumLines, FALSE);
SetScrollPos (hwndScroll, SB_CTL, 0, FALSE);
return (0);
case WM_SIZE:
cyClient = HIWORD (lParam);
MoveWindow (hwndScroll, LOWORD (lParam) - xScroll, 0, xScroll, cyClient, TRUE);
SetFocus (hwnd);
return (0);
case WM_SETFOCUS:
SetFocus (hwndScroll);
return (0);
case WM_VSCROLL:
switch (LOWORD (wParam))
{
case SB_TOP:
iPosition = 0;
break;
case SB_BOTTOM:
iPosition = iNumLines;
break;
case SB_LINEUP:
iPosition -= 1;
break;
case SB_LINEDOWN:
iPosition += 1;
break;
case SB_PAGEUP:
iPosition -= cyClient / cyChar;
break;
case SB_PAGEDOWN:
iPosition += cyClient / cyChar;
break;
case SB_THUMBPOSITION:
iPosition = LOWORD (lParam);
break;
}
iPosition = max (0, min (iPosition, iNumLines));
if (iPosition != GetScrollPos (hwndScroll, SB_CTL))
{
SetScrollPos (hwndScroll, SB_CTL, iPosition, TRUE);
InvalidateRect (hwndScroll, NULL, TRUE);
}
return (0);
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
pText = (TCHAR *) LockResource (hResource);
GetClientRect (hwnd, &rect);
rect.left += cxChar;
rect.top += cyChar * (1 - iPosition);
DrawText (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);
}
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Chinese (P.R.C.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
#ifdef _WIN32
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
#pragma code_page(936)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Icon
//
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON ICON DISCARDABLE "icon.ico"
/////////////////////////////////////////////////////////////////////////////
//
// TXT
//
IDR_TXT TXT DISCARDABLE "xxx.txt"
/////////////////////////////////////////////////////////////////////////////
//
// String Table
//
STRINGTABLE DISCARDABLE
BEGIN
IDS_APPNAME "poepoem"
IDS_CAPTION """""Annabel Lee """" by Edgar Allan Poe"
IDS_ERRMSG "This program requires Windows NT!"
END
#endif // Chinese (P.R.C.) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED