我的第一个窗体

来源:互联网 发布:b类网络号怎么算 编辑:程序博客网 时间:2024/05/16 10:24

// DEMO2_3.CPP - A complete windows program

// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include <windows.h>   // include all the windows headers
#include <windowsx.h>  // include useful macros
#include <stdio.h>    
#include <math.h>

// DEFINES ////////////////////////////////////////////////

// defines for windows
#define WINDOW_CLASS_NAME "myclass1"

// GLOBALS ////////////////////////////////////////////////


// FUNCTIONS //////////////////////////////////////////////
//定义出一个回调函数供系统处理
LRESULT CALLBACK WindowProc(HWND hwnd,
          UINT msg,
                            WPARAM wparam,
                            LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT  ps;  // used in WM_PAINT
HDC    hdc; // handle to a device context

// what is the message
switch(msg)
 { 
 case WM_CREATE:
        {
  // do initialization stuff here

        // return success
  return(0);
  } break;

 case WM_PAINT:
  {
   //重绘
  // simply validate the window
  hdc = BeginPaint(hwnd,&ps); 
  // you would do all your painting here
        EndPaint(hwnd,&ps);

        // return success
  return(0);
     } break;

 case WM_DESTROY:
  {
   //销毁窗体
  // kill the application, this sends a WM_QUIT message
     //函数功能:该函数向系统表明有个线程有终止请求。通常用来响应WM_DESTROY消息。
  PostQuitMessage(0);//退出消息循环

        // return success
  return(0);
  } break;

 default:break;

    } // end switch

// process any messages that we didn't take care of
/*函数功能:该函数调用缺省的窗口过程来为应用程序没有处理的任何窗口消息提供缺省的处理。
该函数确保每一个消息得到处理。调用DefWindowProc函数时使用窗口过程接收的相同参数。*/
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
     HINSTANCE hprevinstance,
     LPSTR lpcmdline,
     int ncmdshow)
{

WNDCLASSEX winclass; // this will hold the class we create 实例一个窗体结构
HWND    hwnd;  // generic window handle 定义一个窗体句柄
MSG     msg;   // generic message 定义一个消息

// first fill in the window class stucture
winclass.cbSize         = sizeof(WNDCLASSEX);//指定了以字节为单位的结构的大小。这个成员是通过sizeof(WNDCLASSEX)实现的。你将会经常看到它,尤其是你使用了DirectX
winclass.style   = CS_DBLCLKS | CS_OWNDC |
                          CS_HREDRAW | CS_VREDRAW; //指定了窗口的风格。它经常被以CS_打头的符号常量定义。两种或两种以上的风格可以通过C语言中的“或”(|)运算符加以组合
/*
CS_HREDRAW 一旦移动或尺寸调整使客户区的宽度发生变化,就重新绘制窗口。
CS_VREDRAW:一旦移动或尺寸调整使客户区的高度发生变化,就重新绘制窗口。
CS_OWNDC:为该类中的每一个窗口分配一个唯一的设备上下文
CS_DBLCLKS:当用户双击鼠标时向窗口过程发送双击消息
*/
winclass.lpfnWndProc = WindowProc;//指向回调函数名
winclass.cbClsExtra  = 0;
winclass.cbWndExtra  = 0;
winclass.hInstance  = hinstance;
winclass.hIcon   = LoadIcon(NULL, IDI_APPLICATION); //指向窗口图标的句柄,它通常被LoadIcon()函数设置,具体有哪些样式查MSDN
winclass.hCursor  = LoadCursor(NULL, IDC_ARROW); //加载光标
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);//背景色
winclass.lpszMenuName = NULL;//无菜单
winclass.lpszClassName = WINDOW_CLASS_NAME;//窗体名称
winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);//指向小图标的句柄。小图标用来显示在窗口的标题栏里

// register the window class
//注册一个窗体,RegisterClassEx注册失败返回值0,那么取反则返回该函数
if (!RegisterClassEx(&winclass))
 return(0);

// create the window
//创建一个窗体,若创建失败则返回值0,那么失败的话退出该函数
if (!(hwnd = CreateWindowEx(NULL, // extended style
                            WINDOW_CLASS_NAME,   // class
          "我的第一个窗体", // title
          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
           0,0,     // initial x,y窗口位置的x坐标,y
          400,400,  // initial width, height 宽,高
          NULL,     // handle to parent //父窗口句柄
          NULL,     // handle to menu//菜单句柄
          hinstance,// instance of this application
          NULL))) // extra creation parms
return(0);

// enter main event loop
/*函数功能:该函数从调用线程的消息队列里取得一个消息并将其放于指定的结构。此函数可取得与指定窗口联系的消息和由PostThreadMesssge寄送的线程消息。
此函数接收一定范围的消息值。GetMessage不接收属于其他线程或应用程序的消息*/
while(GetMessage(&msg,NULL,0,0))//指向一个MSG结构的指针,用来保存消息
   {
  /*TranslateMessage()函数的作用是把虚拟键消息转换到字符消息,以满足键盘输入的需要。
 DispatchMessage()函数所完成的工作是把当前的消息发送到对应的窗口过程中去。*/
  // translate any accelerator keys
  TranslateMessage(&msg);
  // send the message to the window proc
  DispatchMessage(&msg);
  } // end while

// return to Windows like this
return(msg.wParam);//将返回代码依次返回调用层(退出)。    Specifies   the   exit   code   given   in   the   PostQuitMessage   function.   

} // end WinMain

///////////////////////////////////////////////////////////

 

原创粉丝点击