#include <windows.h>
#define ID_LIST 1
#define ID_TEXT 2
#define MAXREAD 8192
#define DIRATTR (DDL_READWRITE | DDL_READONLY | DDL_HIDDEN | \
DDL_SYSTEM | DDL_DIRECTORY | DDL_ARCHIVE | DDL_DRIVES)
#define DTFLAGS (DT_WORDBREAK | DT_EXPANDTABS | DT_NOCLIP | DT_NOPREFIX)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ListProc(HWND, UINT, WPARAM, LPARAM);
WNDPROC OldList;
int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, PSTR szCL, int iCS) {
static TCHAR szAppName[] = TEXT("Leitura de Disco");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hI;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetSysColorBrush(3);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Windows NT"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(
szAppName, TEXT("Leitura de Disco:"), 0xcf0000 | 0x2000000,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hI, NULL
);
ShowWindow(hwnd, iCS);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
static BOOL bValidFile;
static BYTE buffer[MAXREAD];
static hwndList, hwndText;
static RECT rect;
static TCHAR szFile[MAX_PATH+1];
HANDLE Arq;
HDC hdc;
int i, cxChar, cyChar;
PAINTSTRUCT ps;
TCHAR szBf[MAX_PATH+1];
switch(msg) {
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
rect.left = 20*cxChar;
rect.top = 3*cyChar;
hwndList = CreateWindow(
TEXT("listbox"), NULL, WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,
cxChar, cyChar*3, cxChar*13+GetSystemMetrics(2), cyChar*10,
hwnd, (HMENU) ID_LIST, (HINSTANCE) GetWindowLong(hwnd, -6), NULL
);
GetCurrentDirectory(MAX_PATH+1, szBf);
hwndText = CreateWindow(
TEXT("static"), szBf, WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
cxChar, cyChar, cxChar*MAX_PATH, cyChar,
hwnd, (HMENU) ID_TEXT, (HINSTANCE) GetWindowLong(hwnd, -6), NULL
);
OldList = (WNDPROC) SetWindowLong(hwndList, -4, (LPARAM) ListProc);
SendMessage(hwndList, LB_DIR, DIRATTR, (LPARAM) TEXT("*.*"));
return 0;
case WM_SIZE:
rect.right = LOWORD(lp);
rect.bottom = HIWORD(lp);
return 0;
case WM_SETFOCUS:
SetFocus(hwndList);
return 0;
case WM_COMMAND:
if(LOWORD(wp)==ID_LIST && HIWORD(wp)==LBN_DBLCLK) {
if(LB_ERR==(i=SendMessage(hwndList,LB_GETCURSEL,0,0)))
break;
SendMessage(hwndList, LB_GETTEXT, i, (LPARAM) szBf);
if(INVALID_HANDLE_VALUE!=(Arq=CreateFile(szBf,GENERIC_READ,0x00000001,NULL,3,0,NULL))) {
CloseHandle(Arq);
bValidFile = TRUE;
lstrcpy(szFile, szBf);
GetCurrentDirectory(MAX_PATH+1, szBf);
if(szBf[lstrlen(szBf) - 1] != '\\')
lstrcat(szBf, TEXT("\\"));
SetWindowText(hwndText, lstrcat(szBf, szFile));
}
else {
bValidFile = FALSE;
szBf[lstrlen(szBf) - 1] = '\0';
if(!SetCurrentDirectory(szBf + 1)) {
szBf[3] = ':';
szBf[4] = '\0';
SetCurrentDirectory(szBf + 2);
}
GetCurrentDirectory(MAX_PATH + 1, szBf);
SetWindowText(hwndText, szBf);
SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
SendMessage(hwndList, LB_DIR, DIRATTR, (LPARAM) TEXT("*.*"));
}
InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case WM_PAINT:
if(!bValidFile)
break;
if(INVALID_HANDLE_VALUE==(Arq=CreateFile(szFile,GENERIC_READ,0x00000001,NULL,3,0,NULL))) {
bValidFile = FALSE;
break;
}
ReadFile(Arq, buffer, MAXREAD, &i, NULL);
CloseHandle(Arq);
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
SetTextColor(hdc, GetSysColor(COLOR_BTNTEXT));
SetBkColor (hdc, GetSysColor(COLOR_BTNFACE));
DrawTextA(hdc, buffer, i, &rect, DTFLAGS);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wp, lp);
}
LRESULT CALLBACK ListProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
if(msg==WM_KEYDOWN && wp==VK_RETURN)
SendMessage(GetParent(hwnd), 0x0111, MAKELONG(1, LBN_DBLCLK), (LPARAM) hwnd);
return CallWindowProc(OldList, hwnd, msg, wp, lp);
}
Comentários
Postar um comentário