|
|
|
Navigation
0 users online | 0 Guests and 0 Registered
|
ID #1005
Eine Progressbar erstellen
// -------------------------------------------------------
//
// Demonstration der Anwendung einer ProgressBar
// Bitte einen beliebigen Dateinamen in das Editfeld eingeben
// Z.B. "C:\\Command.com"
// Nach Mausklick auf "OK" geht es dann los.
//
// -------------------------------------------------------
#include
#include
#pragma comment(lib,"Comctl32.lib")
#define ID_BN_OK 1
#define ID_BN_Cancel 2
#define ID_EDIT 3
LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);
BOOL readFile(HWND, LPSTR);
HINSTANCE hInst;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Progress Bar");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
hInst = hInstance;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProcedure;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Error: Unable to execute"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName, TEXT("PBar in Action"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
700, 100,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.lParam;
}
LRESULT CALLBACK WndProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndBN_OK, hwndBN_Cancel, hwndEdit, hwndPB;
static int cxChar, cyChar;
switch(message)
{
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
hwndBN_OK = CreateWindow(TEXT("Button"),
"OK",
WS_CHILD | WS_VISIBLE |BS_PUSHBUTTON,
cxChar, cyChar,
20 * cxChar, 7 * cyChar / 4,
hwnd,
(HMENU) ID_BN_OK,
((LPCREATESTRUCT) lParam)->hInstance,
NULL);
hwndEdit = CreateWindow(TEXT("Edit"),
NULL,
WS_CHILD | WS_VISIBLE |
WS_BORDER | ES_LEFT,
22 * cxChar, cyChar,
40 * cxChar, 7 * cyChar / 4,
hwnd,
(HMENU) ID_BN_Cancel,
((LPCREATESTRUCT) lParam)->hInstance,
NULL);
SetWindowText (hwndEdit, "C:\\\\Command.com");
hwndBN_OK = CreateWindow(TEXT("Button"),
"Cancel",
WS_CHILD | WS_VISIBLE |
BS_PUSHBUTTON,
64 * cxChar, cyChar,
20 * cxChar, 7 * cyChar / 4,
hwnd,
(HMENU) ID_BN_Cancel,
((LPCREATESTRUCT) lParam)->hInstance,
NULL);
// ProgressBar erzeugen
InitCommonControls();
hwndPB = CreateWindowEx(0, PROGRESS_CLASS,
"", WS_CHILD | WS_VISIBLE,
cxChar, 3 * cyChar,
20 * cxChar, cyChar,
hwnd, NULL,
hInst, NULL);
return 0;
case WM_SETFOCUS:
SetFocus(hwndEdit);
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == ID_BN_OK && HIWORD(wParam) == BN_CLICKED)
{
char szText[100];
GetWindowText(hwndEdit, szText, 100);
readFile(hwndPB, szText);
}
if(LOWORD(wParam) == ID_BN_Cancel && HIWORD(wParam) == BN_CLICKED)
{
PostQuitMessage(0);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
BOOL readFile(HWND hwndParent, LPSTR lpszFileName)
{
HANDLE hFile;
DWORD cb;
DWORD dwRead = 0;
char chRead;
int i = 0;
hFile = CreateFile(lpszFileName, GENERIC_READ,
FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
if (hFile == (HANDLE) INVALID_HANDLE_VALUE)
return FALSE;
cb = GetFileSize(hFile, (LPDWORD) NULL);
// Schrittläge einstellen
SendMessage(hwndParent, PBM_SETRANGE, 0, MAKELPARAM (0, 100));
SendMessage(hwndParent, PBM_SETSTEP, (WPARAM) 10, 0); // mit jedem Schritt 10% weiter
do {
ReadFile(hFile, &chRead, 1, &dwRead, (LPOVERLAPPED) NULL);
i++;
if (i >= cb / 10) // Haben wir wieder 1/10tel der Dateigröße eingelesen?
{
i = 0;
SendMessage(hwndParent, PBM_STEPIT, 0, 0); // Befehl um einen Schritt zu machen
}
} while (dwRead);
CloseHandle((HANDLE) hFile);
return TRUE;
}
Tags: - Related entries: - Last update: 2009-10-11 19:45 You can comment this FAQ |
Most popular FAQs
|