/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Mouse Position Tips") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
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.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("Mouse Position Tips"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
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)
{
HDC hdc ;
POINT pt,ptToDraw;
RECT rect;
char szBuffer[10];
static POINT ptPrevious={0,0};
static int iTextLength;
static BOOL bIsTipExist=FALSE;
switch (message)
{
case WM_MOUSEMOVE:
SetTimer(hwnd,1,1000,NULL);
//Erase Previous Mouse Tips
if(bIsTipExist)
{
bIsTipExist=FALSE;
hdc=GetDC(hwnd);
SelectObject(hdc,GetStockObject(WHITE_PEN));
Rectangle(hdc,ptPrevious.x,ptPrevious.y-18,ptPrevious.x+iTextLength*8-2,ptPrevious.y+1);
DeleteObject(SelectObject(hdc,GetStockObject(BLACK_PEN)));
ReleaseDC(hwnd,hdc);
}
return 0;
case WM_TIMER:
bIsTipExist=TRUE;
hdc=GetDC(hwnd);
SelectObject(hdc,CreateSolidBrush(RGB(255,255,192)));
//Get Current Mouse Position
GetCursorPos(&pt);
ScreenToClient(hwnd,&pt);
//Decide where to draw
GetClientRect(hwnd,&rect);
iTextLength=sprintf(szBuffer,"%d,%d",pt.x,pt.y);
ptToDraw=pt;
if(pt.y-18<0)
{
ptToDraw.y+=18;
ptToDraw.x-=iTextLength*8-2;
if(ptToDraw.x<0)
ptToDraw.x+=iTextLength*8+10;
}
else if(pt.x+iTextLength*8-2>rect.right)
ptToDraw.x-=iTextLength*8-2;
//Draw Rectangle
Rectangle(hdc,ptToDraw.x,ptToDraw.y-18,ptToDraw.x+iTextLength*8-2,ptToDraw.y+1);
//Draw Text
SetTextAlign(hdc,TA_BOTTOM|TA_LEFT);
SetBkMode(hdc,TRANSPARENT);
TextOut(hdc,ptToDraw.x+1,ptToDraw.y,szBuffer,iTextLength);
ptPrevious=ptToDraw;
//Kill Timer and Delete GDI Objects
KillTimer(hwnd,1);
DeleteObject(SelectObject(hdc,GetStockObject(WHITE_BRUSH)));
ReleaseDC(hwnd,hdc);
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}