1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Main.cpp ; 2/7/2011
// By: Rafael Perez-Santana
/* This program opens a file and allows you to scroll up and down. Side
scrolling is also supported if the file width is larger than the window
view. Only a vertical scroll bar is implemented; click the arrows or drag
the thumb track to scroll text on-demmand.
The following lines of text are from the Win32/API, I inserted them here because
they will be bigger than the window view width, causing this application to activate
side scrolling to demonstrate this functionality.
////////////////////////////////////////////////////////////////////////////
Note that the WM_VSCROLL message carries only 16 bits of scroll box position data. Thus, applications
that rely solely on WM_VSCROLL (and WM_HSCROLL) for scroll position data have a practical maximum
position value of 65,535.
However, because the SetScrollPos, SetScrollRange, GetScrollPos, and GetScrollRange functions support
32-bit scroll bar position data, there is a way to circumvent the 16-bit barrier of the WM_HSCROLL
and WM_VSCROLL messages. See GetScrollPos for a description of the technique and its limits.
////////////////////////////////////////////////////////////////////////////
I implemented code that will draw a "»" to the line that exceeds the window view
width, neat little feature I believe. I didn't wanted to implement a horizontal
scroll bar. I don't know if its just me but they look ugly.
To keep this example less complicated than it already is, I did not opt
to implement a open file dialog box, to open the file you want, scroll
down until you see WM_CREATE: and look for
const char*FileName="Main.cpp"; // The file to open.
Change "Main.cpp" to the file you want to open, for example, if you wanted
to open a file called readme.txt, replace "Main.cpp" with "readme.txt". If
the file is not in the current directory you can use slashes the following
way; "c:/somefolder/documents/sales.txt" do not use "\" C++ reserves that
for special uses such as \n \a or \r the compiler will simply get confused
so use "/" instead.
You can open just about any file with this thing, even garbage files. Be
warned, it does not check to see if you have exceeded system memory when
loading a file, so you may get a run-time error if memory gets maxed out.
Also, some files may not side scroll correctly since I didn't invest much
time to debug this feature.
This is intended to serve as a learning/tutorial experience for those
learning windows programming, how to load files, how to create scrollbars,
etc. Enjoy!
*/
// RANDOM: I looked at my code and comments, then realized that I may be
// a perfectionist?! I wonder if thats a good thing or a bad thing. O_o
#include <windows.h>
#include <vector>
#include <string>
#include <fstream>
// Namespaces to use.
using std::vector;
using std::string;
using std::ifstream;
struct VIEWDATA
// Structure for scrolling the file data.
{
UINT Offset_X; // Horizontal array offset.
UINT Offset_Y; // Vertical array offset.
UINT Width; // Number of columns to display.
UINT Height; // Number of rows to display.
UINT FileWidth; // Largest string found.
UINT FileLength; // Total number of lines in file.
COLORREF Background; // Viewport background.
COLORREF Foreground; // Viewport foreground.
vector<string>Text; // File data is stored here.
};
// Declare function proto-types.
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
void TxtOut(HDC,int,int,LPCSTR,COLORREF);
// Program entry point.
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int)
// Note: Anyone care to explain how this WinMain function works with no
// variables specified for the 2nd,3rd & 4th parameters? I didn't need them
// for this program but how in the world does the compiler allow this?
{
WNDCLASSEX WinClassEx; // Data structure for the window class.
// Window structure.
WinClassEx.hInstance=hInstance;
WinClassEx.lpszClassName="WindowClassEx";
WinClassEx.lpfnWndProc=WinProc; // Pointer to window procedure.
WinClassEx.style=CS_HREDRAW|CS_VREDRAW;
WinClassEx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WinClassEx.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
WinClassEx.hCursor=LoadCursor(NULL,IDC_ARROW);
WinClassEx.lpszMenuName=NULL;
WinClassEx.cbClsExtra=0;
WinClassEx.cbWndExtra=4; // Extra bytes for pView pointer.
WinClassEx.hbrBackground=CreateSolidBrush(RGB(0,0,200));
WinClassEx.cbSize=sizeof(WNDCLASSEX);
if(!RegisterClassEx(&WinClassEx))
{ // If there was a failure, alert the user.
MessageBox(NULL,"Failed to register window class.","Error",MB_ICONERROR);
return 0; // Quit program.
}
// Now we specify width and height of window.
UINT iWidth=800,iHeight=600,iWinX=0,iWinY=0; // iWinX & iWinY are 0 for now.
// Create the application window.
HWND hWnd=CreateWindowEx
(0, // Extended window style.
WinClassEx.lpszClassName, // Window class name.
NULL, // I will set during WM_CREATE.
WS_EX_LAYERED, // Look it up, sorry.
iWinX,iWinY, // Window position.
iWidth,iHeight, // Window dimensions.
HWND_DESKTOP, // Window is a child-window to desktop.
NULL, // No menu for this one.
WinClassEx.hInstance, // Application instance.
NULL); // No window creation data.
if(!hWnd)
{ // If there was a failure, alert the user.
MessageBox(NULL,"Failed to create window.","Error",MB_ICONERROR);
return 0; // Quit program.
}
// Adjust size of the window client area, we want exact width & height.
RECT rect;GetClientRect(hWnd,&rect); // Get our client area dimensions.
iWidth+=(iWidth-rect.right);iHeight+=(iHeight-rect.bottom); // Add the difference.
// Also center the window in the desktop area.
SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0); // Get desktop minus taskbar area.
iWinX=(rect.right-iWidth)/2;iWinY=(rect.bottom-iHeight)/2; // Calculate center.
// Update window position and size.
MoveWindow(hWnd,iWinX,iWinY,iWidth,iHeight,NULL);
ShowWindow(hWnd,SW_SHOWNORMAL); // Display the window.
MSG Msg; // Save window messages here.
// Start the message pump.
while(GetMessage(&Msg,NULL,0,0)) // Get it.
{
TranslateMessage(&Msg); // Translate it.
DispatchMessage(&Msg); // Pump it.
}
return 0; // End.
}
|