// C++ source code in BrowserVS.cpp
//
// Victor Santos -2009-
//
// BrowserVS.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
#define MAX_ADDRESSTR 256
#define MAX_BUFFER 10000
#define xBorder 20
#define yBorder 120
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// My Global Vars.
char HttpAdd[MAX_ADDRESSTR];
char Buffer[MAX_BUFFER];
RECT Crct;
RECT AddRt= {xBorder, 2, xBorder+65, 20};
HWND hwndEdit1, hwndEdit2, hwndButtGo;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BROWSERVS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BROWSERVS);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BROWSERVS);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_BROWSERVS;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, /*nCmdShow*/ SW_SHOWMAXIMIZED);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HINTERNET hOpenHandle, hFile;
CHAR tbuf[1024], tadd[MAX_ADDRESSTR];
DWORD dwRead;
int cnt;
switch (message)
{
case WM_CREATE:
GetWindowRect(GetDesktopWindow(), &Crct);
hwndEdit1=CreateWindow("Edit", "", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
ES_LEFT,AddRt.right, 0, Crct.right-AddRt.right-xBorder-30, AddRt.bottom, hWnd, (HMENU)IDC_ADDEDIT, hInst, NULL);
hwndEdit2=CreateWindow("Edit", "", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_READONLY |
ES_AUTOVSCROLL | WS_VSCROLL | ES_LEFT,
xBorder, AddRt.bottom, Crct.right-2*xBorder, Crct.bottom-yBorder, hWnd, (HMENU)IDC_HTMLEDIT, hInst, NULL);
hwndButtGo=CreateWindow("Button", "GO", WS_VISIBLE | WS_CHILD ,Crct.right-xBorder-30, 0,
30, AddRt.bottom, hWnd, (HMENU)IDC_BUTTON_GO, hInst, NULL);
SetFocus(hwndEdit1);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDC_BUTTON_GO:
GetDlgItemText(hWnd, IDC_ADDEDIT, tadd, MAX_ADDRESSTR-10);
if(strlen(tadd)==0) {
SetFocus(hwndEdit1);
break;
}
_strlwr_s(tadd);
if(strstr(tadd, "http://")==NULL)
strcpy_s(HttpAdd, "http://");
else strcpy_s(HttpAdd, "");
strcat_s(HttpAdd, tadd);
// Internet Comm ///////////////////////////////////////////////////////
hOpenHandle = InternetOpen("BrowserVS", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hOpenHandle) {
hFile = InternetOpenUrl(hOpenHandle, (LPCSTR)HttpAdd, NULL, 0, 0, 0);
if(hFile) {
strcpy_s(Buffer,"");
cnt=0;
while (InternetReadFile( hFile, tbuf, 1023, &dwRead )) {
if (dwRead == 0) break;
cnt+=dwRead;
if(cnt>=MAX_BUFFER-1024) break;
tbuf[dwRead] = 0;
strcat_s(Buffer, tbuf);
}
InternetCloseHandle(hFile);
}
else strcpy_s(Buffer, "ERROR !!! Unable to open URL.");
InternetCloseHandle(hOpenHandle);
}
else strcpy_s(Buffer, "ERROR !!! Unable to open internet connection.");
////////////////////////////////////////////////////////////////////////
SetDlgItemText(hWnd, IDC_HTMLEDIT, Buffer);
SetFocus(hwndEdit1);
UpdateWindow(hWnd);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, "Address:", 8, &AddRt, DT_LEFT);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}