#include <windows.h>
#include <string>
using namespace std;
//----------函數聲明---------------
void init(HWND hWnd,HINSTANCE hInstance);
void pix(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,PAINTSTRUCT &Ps);//畫點,也就是像素輸出
void rect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,PAINTSTRUCT &Ps);//----畫矩形----
void fillrect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps);//填充矩形
void linerect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps);//線框矩形
void triangle(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,POINT Pt[3],PAINTSTRUCT &Ps);//三角形
void ellipse(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps);//橢圓
void DrawContent(HWND hWnd);
//-----回調函數-----------------
LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndCls;
MSG Msg;
WndCls.cbSize = sizeof(WndCls);
WndCls.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
WndCls.lpfnWndProc = WindProcedure;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hInstance = hInstance;
WndCls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndCls.hCursor = LoadCursor(NULL, IDC_ARROW);
WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndCls.lpszMenuName = NULL;
WndCls.lpszClassName = TEXT("MAIN");
WndCls.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
RegisterClassEx(&WndCls);
HWND hWnd=CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
TEXT("MAIN"),TEXT( "陳琦"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 820, 820,
NULL, NULL, hInstance, NULL);
init(hWnd,hInstance);
UpdateWindow(hWnd);
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage( &Msg);
}
return static_cast<int>(Msg.wParam);
}
void init(HWND hWnd,HINSTANCE hInstance)
{
CreateWindow( TEXT("BUTTON"), TEXT("畫點"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 0, 150,80, hWnd,NULL, hInstance, NULL);
CreateWindow( TEXT("BUTTON"), TEXT("矩形"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 90, 150,80, hWnd,NULL, hInstance, NULL);
CreateWindow( TEXT("BUTTON"), TEXT("填充矩形"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 180, 150,80, hWnd,NULL, hInstance, NULL);
CreateWindow( TEXT("BUTTON"), TEXT("線框矩形"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 270, 150,80, hWnd,NULL, hInstance, NULL);
CreateWindow( TEXT("BUTTON"), TEXT("三角形"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 360, 150,80, hWnd,NULL, hInstance, NULL);
CreateWindow( TEXT("BUTTON"), TEXT("橢圓"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 450, 150,80, hWnd,NULL, hInstance, NULL);
}
LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT Ps;
HBRUSH NewBrush;
RECT r;
POINT Pt[3];
switch(Msg)
{
case WM_COMMAND:
HWND h;
h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("畫點"));
if(DWORD(lParam)==int(h))
{
pix(hWnd,hDC,NewBrush,Ps);
break;
}
h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("矩形"));
if(DWORD(lParam)==int(h))
{
rect(hWnd,hDC,NewBrush,Ps);
break;
}
h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("填充矩形"));
if(DWORD(lParam)==int(h))
{
fillrect(hWnd,hDC,NewBrush,r,Ps);
break;
}
h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("線框矩形"));
if(DWORD(lParam)==int(h))
{
linerect(hWnd,hDC,NewBrush,r,Ps);
break;
}
h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("三角形"));
if(DWORD(lParam)==int(h))
{
triangle(hWnd,hDC,NewBrush,Pt,Ps);
break;
}
h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("橢圓"));
if(DWORD(lParam)==int(h))
{
ellipse(hWnd,hDC,NewBrush,r,Ps);
break;
}
case WM_PAINT:
DrawContent(hWnd);//為什麼加了這一句後開始按鈕就會顯示,而不加的話按鈕需要重繪以後顯示
break;
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
//畫點
void pix(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,PAINTSTRUCT &Ps)
{
//hDC = BeginPaint(hWnd, &Ps);//用BeginPaint的話消息一定要WM_PAINT響應,但是WM_PAINT裡面隻有DrawContent(hWnd);
//是以用getDC
hDC=GetDC(hWnd);
NewBrush=CreateSolidBrush(RGB(255,0,0));
//SelectObject(hDC, NewBrush);
for(int x=500;x<600;x++)//由于一個點太小了,是以這裡花了100個點構成線段
SetPixel(hDC,x,50,RGB(255,0,0));
//DeleteObject(NewBrush);
//EndPaint(hWnd, &Ps);
ReleaseDC(hWnd,hDC);
}
//矩形
void rect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,PAINTSTRUCT &Ps)
{
//hDC = BeginPaint(hWnd, &Ps);
hDC=GetDC(hWnd);
NewBrush=CreateSolidBrush(RGB(255,0,0));
SelectObject(hDC, NewBrush);
Rectangle(hDC,400,400,500,500);
DeleteObject(NewBrush);
ReleaseDC(hWnd,hDC);
//EndPaint(hWnd, &Ps);
}
//填充矩形
void fillrect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps)
{
//hDC = BeginPaint(hWnd, &Ps);
hDC=GetDC(hWnd);
NewBrush = CreateSolidBrush(RGB(25, 25, 5));
SelectObject(hDC, NewBrush);
SetRect (&r, 200, 200,250, 250);
FillRect(hDC, &r, NewBrush);
DeleteObject(NewBrush);
//EndPaint(hWnd, &Ps);
ReleaseDC(hWnd,hDC);
}
//線框矩形
void linerect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps)
{
//hDC = BeginPaint(hWnd, &Ps);
hDC=GetDC(hWnd);
NewBrush = CreateSolidBrush(RGB(25, 25, 5));
SelectObject(hDC, NewBrush);
SetRect (&r, 250, 250,400, 400);
FrameRect(hDC, &r, NewBrush);
DeleteObject(NewBrush);
DeleteObject(NewBrush);
//EndPaint(hWnd, &Ps);
ReleaseDC(hWnd,hDC);
}
//三角形
void triangle(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,POINT Pt[3],PAINTSTRUCT &Ps)
{
//hDC = BeginPaint(hWnd, &Ps);
hDC=GetDC(hWnd);
NewBrush = CreateSolidBrush(RGB(50, 50, 50));
SelectObject(hDC, NewBrush);
Pt[0].x = 425; Pt[0].y = 40;
Pt[1].x = 395; Pt[1].y = 70;
Pt[2].x = 455; Pt[2].y = 70;
Polygon(hDC, Pt, 3);
DeleteObject(NewBrush);
NewBrush = CreateSolidBrush(RGB(0, 255, 0));
SelectObject(hDC, NewBrush);
Pt[0].x = 365; Pt[0].y = 110;
Pt[1].x = 395; Pt[1].y = 80;
Pt[2].x = 395; Pt[2].y = 140;
Polygon(hDC, Pt, 3);
DeleteObject(NewBrush);
NewBrush = CreateSolidBrush(RGB(255, 0, 0));
SelectObject(hDC, NewBrush);
Pt[0].x = 485; Pt[0].y = 110;
Pt[1].x = 455; Pt[1].y = 80;
Pt[2].x = 455; Pt[2].y = 140;
Polygon(hDC, Pt, 3);
DeleteObject(NewBrush);
NewBrush = CreateSolidBrush(RGB(0, 0, 255));
SelectObject(hDC, NewBrush);
Pt[0].x = 425; Pt[0].y = 180;
Pt[1].x = 455; Pt[1].y = 150;
Pt[2].x = 395; Pt[2].y = 150;
Polygon(hDC, Pt, 3);
DeleteObject(NewBrush);
//EndPaint(hWnd, &Ps);
ReleaseDC(hWnd,hDC);
}
//橢圓
void ellipse(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps)//橢圓
{
//hDC = BeginPaint(hWnd, &Ps);
hDC=GetDC(hWnd);
NewBrush = CreateSolidBrush(RGB(255, 0, 0));
SelectObject(hDC, NewBrush);
Ellipse(hDC,500,500,600,600);
DeleteObject(NewBrush);
//EndPaint(hWnd, &Ps);
ReleaseDC(hWnd,hDC);
}
void DrawContent(HWND hWnd)
{
HDC hDC;
PAINTSTRUCT Ps;
hDC = BeginPaint(hWnd, &Ps);//用BeginPaint的話消息一定要WM_Paint響應
EndPaint(hWnd, &Ps);
}