Windows-Timer von Plapperkatze am 1.Februar 2006 um 17:35 zurück zur Kategorie "Tutorials"
Dieses Programm erzeugt mit einem Windows-Timer ein "fliegendes Fenster". Um einen solchen Timer zu verwenden, muss er mit SetTimer(HWND hWnd,UINT_PTR nIDEvent,UINT uElapse,TIMERPROC lpTimerFunc) erzeugt werden. Wird keine timerproc angegeben (also NULL), erhält das Hauptfenster die WM_TIMER Nachrichten. Dort wird mit GetWindowRect() und MoveWindow() das Fenster bewegt.
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
const char szAppName[] = ";)";
#define speed=10
int xdir=speed;
int ydir=speed;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wc.lpszClassName = szAppName;
wc.lpszMenuName = NULL;
RegisterClass(&wc);
hWnd = CreateWindow( szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
100,100,100,100,
NULL,
NULL,
hInstance,
NULL);
// timer initialisieren
SetTimer(hWnd, // handle to main window
1, // timer identifier
5, // ms interval
(TIMERPROC) NULL); // no timer callback
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)
{
switch (message)
{
case WM_TIMER:
{
RECT r;
GetWindowRect(hWnd,&r);
if(r.right>1024)xdir=-speed;
if(r.left<=0)xdir=speed;
if(r.bottom>768)ydir=-speed;
if(r.top<=0)ydir=speed;
MoveWindow(hWnd,r.left+xdir,r.top+ydir,100,100,TRUE);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
} |
grüsse, die plapperkatz
zurück zur Kategorie "Tutorials" [0 Kommentare]
|