Win32 API 绘图与文本输出

初学Visual C++,自己写的代码,供以后参考

#include
#include

HWND hWndMain;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
 MSG Msg;
 if(!InitWindowsClass(hInstance))
  return FALSE;
 if(!InitWindows(hInstance,nCmdShow))
  return FALSE;

 ShowWindow(hWndMain,nCmdShow);
 UpdateWindow(hWndMain);

 while(GetMessage(&Msg,NULL,0,0))
 {
  TranslateMessage(&Msg);
  DispatchMessage(&Msg);
 }
 return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 static long nXChar,nYChar;
 HDC hdc;
 short x;
 TEXTMETRIC tm;
 PAINTSTRUCT ps;
 COLORREF color;
 HFONT font;
 static char* textbuf[]=
 {
  "This is the 1 Line",
  "This is the 2 Line",
  "This is the 3 Line",
  "This is the 4 Line",
  "This is the 5 Line",
  "This is the 6 Line",
  "This is the 7 Line",
  "This is the 8 Line",
  "This is the 9 Line",
  "This is the 10 Line",
  "This is the 11 Line",
  "This is the 12 Line",
  "This is the 13 Line",
  "This is the 14 Line",
  "This is the 15 Line",
  "This is the 16 Line",
 };
 short LineCount=16;
 switch(message)
 {
 case WM_CREATE:
  hdc=GetDC(hwnd);
  GetTextMetrics(hdc,&tm);
  nXChar=tm.tmAveCharWidth;
  nYChar=tm.tmHeight;
  ReleaseDC(hwnd,hdc);
  return 0;
 case WM_PAINT:
  hdc=BeginPaint(hwnd,&ps);
  for(x=0;x  {
   color=RGB(255-x*15,x*15,x*29);
   SetTextColor(hdc,color);
   font=CreateFont(
    11+x,0,0,0,
    FW_NORMAL,x%2,x%2,x%2,
    ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
    NULL,
    "myfont"
   );
   SelectObject(hdc,font);
   GetTextMetrics(hdc,&tm);
   nYChar=tm.tmHeight;
   TextOut(hdc,nXChar,nYChar*(1+x),textbuf[x],strlen(textbuf[x]));
  }
  EndPaint(hwnd,&ps);
  return 0;
 case WM_DESTROY:
  PostQuitMessage(0);
  return 0;
 default:
  return DefWindowProc(hwnd,message,wParam,lParam);
 
 }

 /*
 HDC hdc;
 PAINTSTRUCT ps;
 HBRUSH hB1,hB2;
 HPEN hP1;
 COLORREF color;
 int nMode=MM_TEXT;
 int i;
 switch(message)
 {
 case WM_CREATE:
  nMode=MM_TEXT;
  break;
 case WM_PAINT:
  hdc=BeginPaint(hwnd,&ps);
  SetMapMode(hdc,nMode);
  SetWindowExtEx(hdc,150,150,NULL);
  SetViewportExtEx(hdc,150,100,NULL);
  SetViewportOrgEx(hdc,0,0,NULL);
  hB1=(HBRUSH)GetStockObject(WHITE_BRUSH);
  hB2=(HBRUSH)GetStockObject(BLACK_BRUSH);
  color=RGB(255,0,0);
  hP1=CreatePen(PS_DOT,1,color);
  SelectObject(hdc,hP1);
  SelectObject(hdc,hB1);
  RoundRect(hdc,0,0,150,150,30,30);
  SelectObject(hdc,hB2);
  Ellipse(hdc,0,10,150,140);
  for(i=0;i<35;i++)
  {
   hP1=CreatePen(PS_NULL,0,color);
     SelectObject(hdc,hP1);
   LineTo(hdc,0,0);
   hP1=CreatePen(PS_DOT,2,color);
     SelectObject(hdc,hP1);
   LineTo(hdc,800,i*60);
  }
  EndPaint(hwnd,&ps);
  break;
 case WM_DESTROY:
  DeleteObject(hB1);
  DeleteObject(hB2);
  DeleteObject(hP1);
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hwnd,message,wParam,lParam);
  
 }
 return 0;
 */

 /*
 HDC hDC;
 HBRUSH hBrush;
 HPEN hPen;
 PAINTSTRUCT PtStr;

 switch(message)
 {
 case WM_PAINT:
  hDC=BeginPaint(hwnd,&PtStr);
  SetMapMode(hDC,MM_ANISOTROPIC);
  hPen=(HPEN)GetStockObject(BLACK_PEN);
  hBrush=(HBRUSH)GetStockObject(DKGRAY_BRUSH);
  SelectObject(hDC,hBrush);
  SelectObject(hDC,hPen);
  RoundRect(hDC,50,120,100,200,15,15);
  EndPaint(hwnd,&PtStr);
  return 0;
 case WM_DESTROY:
  PostQuitMessage(0);
  return 0;
 default:
  return DefWindowProc(hwnd,message,wParam,lParam);
 }
 */
}

BOOL InitWindowsClass(HINSTANCE hInstance)
{
 WNDCLASS wndclass;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
 wndclass.hIcon=LoadIcon(NULL,"END");
 wndclass.hInstance=hInstance;
 wndclass.lpfnWndProc=WndProc;
 wndclass.lpszClassName="Windows Fill";
 wndclass.lpszMenuName=NULL;
 wndclass.style=CS_HREDRAW|CS_VREDRAW;
 return(RegisterClass(&wndclass));
}

BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
 HWND hWnd;
 hWnd=CreateWindow(
  "Windows Fill",
  "填充实例程序",
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT,
  0,
  CW_USEDEFAULT,
  0,
  NULL,
  NULL,
  hInstance,
  NULL
  );
 if(!hWnd)
  return FALSE;
 hWndMain=hWnd;
 ShowWindow(hWnd,nCmdShow);
 UpdateWindow(hWnd);
 return TRUE;
}

你可能感兴趣的:(Win32,API/MFC,api,null,hp,callback,winapi,windows)