#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//-------------------以下初始化窗口类---------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass; //定义窗口类
char lpszClassName[] = "哈哈,for your smile!"; //窗口类名
char lpszTitle[] = "MZone---我的地盘我作主!"; //窗口标题名
//初始化窗口类
wndclass.style = 0; //窗口类型为默认类型
wndclass.lpfnWndProc = WndProc; //窗口处理函数为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 = lpszClassName; //窗口的类名
//---------------以下进行窗口类的注册---------------------------------------------------
if (!RegisterClass(&wndclass))
{
MessageBeep(0); //如果注册失败则发出警告声音
return false;
}
//创建窗口
hwnd = CreateWindow(
lpszClassName, //窗口类名
lpszTitle, //窗口实例的标题名
WS_OVERLAPPEDWINDOW, //窗口的风格
CW_USEDEFAULT, //窗口左上角坐标为默认
CW_USEDEFAULT, //窗口右上角坐标为默认
CW_USEDEFAULT, //窗口宽度为默认
CW_USEDEFAULT, //窗口高度为默认
NULL, //此窗口无父窗口
NULL, //此窗口无主菜单
hInstance, //创建此窗口应用程序的当前句柄
NULL //不使用一个传递给窗口的参数值的指针
);
ShowWindow(hwnd, nCmdShow); //显示窗口
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;
int i,x,y;
COLORREF cr;
POINT pn[5];
RECT r = { 50,100,100,200 };//创建矩形区域并初始化
switch (message)
{
case WM_LBUTTONDOWN:
hdc = GetDC(hwnd);
cr = GetPixel(hdc, LOWORD(lParam), HIWORD(lParam));
for (i = 0; i < 50; i++)
{
SetPixel(hdc, 80 + i, 80 + i, cr);
}
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for ( i = 0; i < 100; i++)
{
SetPixel(hdc, 50 + i, 50 + i, RGB(222, 0, 0));
}
MoveToEx(hdc, 44, 44, NULL);
LineTo(hdc, 222, 22);//画直线
pn[0].x = 5;
pn[0].y = 5;
pn[1].x = 22;
pn[1].y = 22;
pn[2].x = 11;
pn[2].y = 22;
pn[3].x = 33;
pn[3].y = 33;
pn[4].x = 44;
pn[4].y = 444;
Polyline(hdc, pn, 5);//画多边线
SelectObject(hdc, CreatePen(PS_DASH, 5, RGB(222, 111, 22)));//创建画笔对象
//设置边框颜色,(画笔类型(solid实线,dot点线,dash,虚线),画笔宽度,画笔颜色)
Arc(hdc, 10, 100, 80, 200, 70, 120, 70, 120);//逆时针画弧
SelectObject(hdc, GetStockObject(BLACK_BRUSH));//取系统画刷填充下面的椭圆
Ellipse(hdc, 200, 20, 250, 50);//画椭圆
SelectObject(hdc, CreateSolidBrush(RGB(222, 222, 22)));//创建画刷对象,//使用RGB颜色填充圆角矩形
RoundRect(hdc, 100, 200, 200, 350, 80, 110);//画圆角矩形
SelectObject(hdc, CreateHatchBrush(HS_CROSS, RGB(255, 255, 0)));//创建画刷对象,
//具有特殊形状并且带有颜色的画刷,使用RGB颜色填充下面的矩形
Rectangle(hdc, 222, 121, 122, 177);//画矩形
FillRect(hdc, &r, CreateSolidBrush(RGB(255, 255, 0)));//在矩形(RECT r)区域填充颜色
FrameRect(hdc, &r, (HBRUSH)GetStockObject(BLACK_BRUSH));//填充(RECT r)区域边框
//FrameRect(HDC,const RECT *lprc,HBRUSH hbr )需要的是HBRUSH类型,
//GetStockObject返回的是HGDIOBJ类型
InvertRect(hdc, &r);//翻转(RECT r)区域颜色,即把FillRect(RGB)FrameRect(BLACK_BRUSH)颜色翻转
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0); //向应用程序发出WM_QUIT消息,请求退出
default:
return DefWindowProc(hwnd, message, wParam, lParam); //DefWindowProc 系统默认处理过程,
} //以确保所有发送到该窗口的消息均被处理
return (0);
}