#include <Windows.h>
#include "on_paint.h"
#include<wingdi.h>
#include<WindowsX.h>
struct decode_unit main_message_proc[]={
WM_DESTROY,on_destory,
WM_PAINT,on_paint,
};
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
int WinMain(HINSTANCE hInstance,
HINSTANCE prehInstance,
LPTSTR lpCmdLine,
int nShow)
{
//1.创建一个窗体类
WNDCLASS ws;
ws.cbClsExtra =0;
ws.cbWndExtra =0;
ws.hbrBackground =(HBRUSH)
GetStockObject(WHITE_BRUSH);
ws.hCursor =NULL;
ws.hIcon =NULL;
ws.hInstance =hInstance;
ws.lpfnWndProc =WndProc;
ws.lpszClassName =TEXT("Hello");
ws.lpszMenuName =NULL;
ws.style =CS_VREDRAW |CS_HREDRAW;
//2.注册窗体类
if(!RegisterClass(&ws)) return -1;
//3.创建窗体类
HWND hwnd=CreateWindow(TEXT("Hello"),TEXT("My First Windows"),WS_VISIBLE | WS_BORDER| WS_SYSMENU | WS_MINIMIZEBOX |WS_MAXIMIZEBOX |WS_CAPTION,
10,10,400,300,NULL,NULL,hInstance,NULL
);
//4.更新窗体内容
UpdateWindow(hwnd);
ShowWindow(hwnd,nShow);
MSG msg;
//5.获取系统消息
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 1;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
for(int i=0;i<dim(main_message_proc);i++)
{
if(main_message_proc[i].message == message)
(*main_message_proc[i].fun)(hwnd,message,wParam,lParam);
}
return DefWindowProc(hwnd,message,wParam,lParam);
/*switch(message)
{
case WM_DESTROY:
on_destory(hwnd,message,wParam,lParam);
break;
case WM_PAINT:
on_paint(hwnd,message,wParam,lParam);
break;
default:
DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;*/
}
LONG on_destory(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(1);
return TRUE;
}
LONG on_paint(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
HPEN hpen;
GetClientRect(hwnd,&rect);
hdc=BeginPaint(hwnd,&ps);
hpen = CreatePen(PS_SOLID,2,RGB(0XFF,0X00,0X00));
//hpen=GetStockPen(BLACK_PEN);//是对getstockobject函数的封装
HGDIOBJ oldpen=SelectObject(hdc,hpen);//放到设备环境中
Ellipse(hdc,10,10,200,200);//一个黑色画笔的圆
SelectObject(hdc,oldpen);
DeleteObject(hpen);
DrawText(hdc,TEXT("HELLO WORLD"),-1,&rect,DT_VCENTER |DT_CENTER);
EndPaint(hwnd,&ps);
return 0;
}