API简单绘图3

#include<windows.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain
(
 HINSTANCE hInstance,
 HINSTANCE hPrevInst,
 LPSTR lpszCmdLine,
 int nCmdShow
 )
{
 HWND hwnd;
 MSG Msg;
 WNDCLASS wndclass;
 char lpszClassName[]="基本绘图";
 char lpszTitle[]="My_Drawing";
 wndclass.style=0;
 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=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;
 HPEN hP;//定义画笔句柄
 HBRUSH hB;//定义画刷句柄

 switch(message)
 {
 case WM_PAINT:   //通过响应WM_PAINT消息完成绘图工作
   hP=CreatePen(PS_DASHDOT,1,RGB(0,255,0));//自定义一个画笔,该画笔为绿色,所画线条为点划线,宽度为1
   hB=CreateHatchBrush(HS_CROSS,RGB(255,0,0));//自定义一个红色网格状画刷
   hdc=BeginPaint(hwnd,&ps);//取得设备环境句柄
   SetMapMode(hdc,MM_TEXT);//设置映象模式,此程序使用默认模式,可省略
                           //使用当前默认画笔.画刷进行绘图
   Rectangle(hdc,130,60,270,200);//使用当前画笔绘制矩形,用当前画刷填充
   SelectObject(hdc,hB);//更新画刷,设置自定义画刷为当前画刷
   Ellipse(hdc,130,70,270,190);//使用当前画笔绘制一个椭圆,用当前画刷填充椭圆
   SelectObject(hdc,hP);//更新画笔,设置自定义画笔为当前画笔
   MoveToEx(hdc,100,130,NULL);//使用当前画笔绘制轴线
   LineTo(hdc,300,130);
   MoveToEx(hdc,200,30,NULL);
   LineTo(hdc,200,230);
   EndPaint(hwnd,&ps);//释放设备环境句柄
   break;
 case WM_DESTROY:
  DeleteObject(hP);//退出窗口时删除画笔
  DeleteObject(hB);//退出窗口时删除黑色画刷
  PostQuitMessage(0);
  break;

 default:
  return DefWindowProc(hwnd,message,wParam,lParam);
 }
 return 0;
}
 

你可能感兴趣的:(API简单绘图3)