文字加图形的基本实现

来源:互联网 发布:js url加密解密 编辑:程序博客网 时间:2024/06/05 01:05

#include<windows.h>
#include"String.h"
//声明WndProc()函数
LRESULT CALLBACK WndProc(
 HWND hwnd,UINT message,
 WPARAM wParam,
 LPARAM lParam);
//编写WinMain()主函数

int WINAPI WinMain(HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPSTR lpCmdLine,
     int nCmdShow)
{
 WNDCLASS wndclass;         //定义窗口类结构变量
 HWND hwnd;            //定义窗口句柄
 MSG msg;           //定义消息结构变量
 char lpszClassName[]="自己创建的窗口";

 //设计窗口类型
 wndclass.style = CS_HREDRAW|CS_VREDRAW;      //改变窗口大小则重画
 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))
 return FALSE;
 
 //创建窗口
 hwnd = CreateWindow(lpszClassName,
   "window窗口创建",
   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)
{
//根据消息值转相应的消息处理

 RECT rect;
 HBRUSH hbrush;
 char *str="/n/n/n/n这是输出的内容s/neruiwioeutiowreuri";
static int cx,cy;
 switch (message)
 {
  //重画窗口客户区消息处理
 case WM_SIZE:
  cx=LOWORD(lParam);
  cy=HIWORD(lParam);

   case WM_PAINT:   
   HDC hdc;         //定义设备描述表句柄
   PAINTSTRUCT ps;         //定义绘图信息结构变量
   hdc = BeginPaint (hwnd,&ps);    //获取要重画的窗口的设备描述表句柄
   //TextOut(hdc,150,150,"这是我自己创建的第一个窗口哦!", 28);
   //输出文本
   SetRect(&rect,0,0,200,200);
   hbrush=CreateSolidBrush(RGB(255,0,0));
   FillRect(hdc,&rect,hbrush);
   Ellipse(hdc,181,181,300,300);

              
   //TextOut(hdc,0,0,str,strlen(str));
  //TextOut(hdc,0,0,"这是输出的内容sssssssssssssss",14);
   DrawText(hdc,str,strlen(str),&rect,NULL);
          
            TextOut(hdc,200,220,"你好!", 6);

   TextOut(hdc,350,180,"你好!欢迎光临!", 16);

   EndPaint (hwnd,&ps);
   break; 
  //响应鼠标单击消息
  case WM_LBUTTONDOWN:
   {
    MessageBeep(0);    
   }
   break;
  //撤消窗口消息处理
  case WM_DESTROY:
   PostQuitMessage (0);
   break;
  //其它转缺省窗口函数
  default:
   return DefWindowProc (hwnd, message,   wParam, lParam);
 }
 return 0;
}