#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName [] = "Windows";
char lpszTitle [] = "Windows Key Demo";
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = lpszClassName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
return 0;
}
hwnd = CreateWindow(lpszClassName,
lpszTitle,
WS_SYSMENU, // 应用程序可以被关闭,通过点击大叉
50, 50,
450, 450,
NULL, NULL,
hInstance,
NULL);
ShowWindow(hwnd, nShowCmd);
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;
PAINTSTRUCT ps;
TEXTMETRIC tm;
HBRUSH hbrush;
HPEN hpen;
HCURSOR hcursor;
char str[15];
static int x, y;
switch(message)
{
case WM_MOUSEMOVE:
x = LOWORD(lParam);
y = HIWORD(lParam);
if(x>50 && x<150 && y>50 && y<150) //设置十字光标显示区域
{
hcursor = LoadCursor(NULL, IDC_CROSS); //装载十字光标
SetCursor(hcursor);
}
if(x>150 && x<250 && y>50 && y<150)
{
hcursor = LoadCursor(NULL, IDC_SIZEALL);
SetCursor(hcursor);
}
if(x>250 && x<350 && y>50 && y<150)
{
hcursor = LoadCursor(NULL, IDC_SIZENESW);
SetCursor(hcursor);
}
if(x>50 && x<150 && y>150 && y<250)
{
hcursor = LoadCursor(NULL, IDC_IBEAM);
SetCursor(hcursor);
}
if(x>150 && x<250 && y>150 && y<250)
{
hcursor = LoadCursor(NULL, IDC_WAIT);
SetCursor(hcursor);
}
if(x>250 && x<350 && y>150 && y<250)
{
hcursor = LoadCursor(NULL, IDC_UPARROW);
SetCursor(hcursor);
}
if(x>50 && x<150 && y>250 && y<350)
{
hcursor = LoadCursor(NULL, IDC_SIZEWE);
SetCursor(hcursor);
}
if(x>150 && x<250 && y>250 && y<350)
{
hcursor = LoadCursor(NULL, IDC_SIZENWSE);
SetCursor(hcursor);
}
if(x>250 && x<350 && y>250 && y<350)
{
hcursor = LoadCursor(NULL, IDC_SIZENS);
SetCursor(hcursor);
}
if(x>350 || y>350)
{
hcursor = LoadCursor(NULL, IDC_HELP);
SetCursor(hcursor);
}
if(x<50 || y<50)
{
hcursor = LoadCursor(NULL, IDC_APPSTARTING);
SetCursor(hcursor);
}
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
hbrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
SelectObject(hdc, hbrush);
hpen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hdc, hpen);
Rectangle(hdc, 5, 5, tm.tmAveCharWidth*8, tm.tmHeight+3);
sprintf(str, "%d, %d\0", x, y);
TextOut(hdc, 5,5,str, lstrlen(str));
EndPaint(hwnd, &ps);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hpen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hdc, hpen);
Rectangle(hdc, 50,50,350,350);
MoveToEx(hdc, 50,150,NULL);
LineTo(hdc, 350,150);
MoveToEx(hdc, 50,250,NULL);
LineTo(hdc, 350,250);
MoveToEx(hdc, 150,50,NULL);
LineTo(hdc, 150,350);
MoveToEx(hdc, 250,50,NULL);
LineTo(hdc, 250,350);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}