Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1759869
  • 博文数量: 290
  • 博客积分: 10653
  • 博客等级: 上将
  • 技术积分: 3178
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-24 23:08
文章存档

2013年(6)

2012年(15)

2011年(25)

2010年(86)

2009年(52)

2008年(66)

2007年(40)

分类: C/C++

2008-10-28 10:13:12

声明: 由于是在阅读programming windows  这本电子书时的读书笔记,所以所有源代码都来自于这本书中,主要由于快毕业的原因,想对以前学的东西巩固一下;所以,如果是为了学习visusl C++编程的同学,需要的话留下电子邮件,或者自己去下这本书来看;学完这本书后,如果有一定的OOp基础的人,那么,MFC对你来说,已经不成问题;
首先来看一些概念:
一 : 窗口及相关接口
1.Windows 98和Windows NT都是支持32位优先权式多任务(preemptive multitasking)及多线程的图形操作系统。Windows拥有图形使用者接口(GUI ),这种使用者界面也称作「可视化接口」或「图形窗口环境]

2.所有GUI都在点矩阵对应的视讯显示器上处理图形。图形提供了使用屏幕的最佳方式、传递信息的可视化丰富多彩环境,以及能够WYSIWYG(what you see is what you get:所见即所得)的图形视讯显示和为书面文件准备好格式化文字输出内容。
3.WINDOWS程序占用一个应用窗口,所谓窗口,就是显示器上一块矩形区域.而程序占据窗口.所以应从程序的角度来理解窗口处理.菜单和对话框允许用户试验一个新程序并探究它的功能。大多数Windows程序同时具有键盘接口和鼠标接口。虽然Windows程序的大多数功能可通过键盘控制,但使用鼠标要容易得多。
从程序写作者的角度看,一致的使用者接口来自于Windows建构菜单和对话框的内置程序。所有菜单都有同样的键盘和鼠标接口,因为这项工作是由Windows处理,而不是由应用程序处理
  ??? 什么是接口,计算机接口是怎么实现的.有关这方面的知识,查阅,<<计算机操作系统架构>>和相应的硬件书籍 .
4.Windows的早期版本使用一种「非优先权式(non-preemptive)」的多任务系统。这意味着Windows不使用系统定时器将处理时间分配给系统中运行的多个应用程序,程序必须自愿放弃控制以便其它程序运行。在Windows NT和Windows 98中,多任务是优先权式的,而且程序自身可分割成近乎同时执行的多个执行绪。
操作系统不对内存进行管理便无法实现多任务。当新程序启动、旧程序终止时,内存会出现碎裂空间。系统必须能够将闲置的内存空间组织在一起,因此系统必须能够移动内存中的程序代码和数据块。
Windows上执行的程序可共享在称为「动态链接库」的文件中的例程。Windows包括一个机制,能够在执行时连结使用动态链接库中例程的程序。Windows自身基本上就是一个动态链接库的集合。
Windows是一个图形接口,Windows程序能够在视讯显示器和打印机上充分利用图形和格式化文字。图形接口不仅在外观上更有吸引力,而且还能够让使用者传递高层次的信息。
Windows应用程序不能直接存取屏幕和打印机等图形显示设备硬件。相反,Windows提供一种图形程序语言(称作图形设备接口,或者GDI),使显示图形和格式化文字更容易。Windows虚拟化了显示硬件,使为Windows编写的程序可使用任何具有Windows设备驱动程序的视频卡或打印机,而程序无需确定系统相连的设备类型。
对Windows开发者来说,将与设备无关的图形接口输出到IBM PC上不是件轻松的事。PC的设计是基于开放式架构的原则,鼓励第三方硬件制造商为PC开发接口设备,而且开发了大量这样的设备。虽然出现了多种标准,PC上的传统MS-DOS程序仍不得不各自支持许多不同的硬设备。这对MS-DOS字处理软件来说非常普遍,它们连同1到2张有许多小文件的磁盘一同销售,每个文件支持一种特定的打印机。Windows程序不要求每个应用程序都自行开发这些驱动程序,因为这种支持是Windows的一部分。
 

HELLOWIN.C

/*------------------------------------------------------------
   HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
                 (c) Charles Petzold, 1998
  ------------------------------------------------------------*/


#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;//窗口过程原型声明

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("HelloWin") ;
     HWND hwnd ;
     MSG msg ;
     WNDCLASS wndclass ;

     wndclass.style = CS_HREDRAW | CS_VREDRAW ;//如果水平和垂直大小改变,则重绘窗口
ndclass.lpfnWndProc = WndProc ;//唯一和窗口过程相关的代码
     wndclass.cbClsExtra = 0 ;
     wndclass.cbWndExtra = 0 ;
     wndclass.hInstance = hInstance ;
     wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;//load an icon for use by a program
     wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;//load an Cursor for use by a program
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;//Obtains a graphic object in this case a brush used for painting the wondows background;
     wndclass.lpszMenuName = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))//注册窗口类
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     hwnd = CreateWindow (szAppName, // window class name 创建窗口

                          TEXT ("The Hello Program"), // window caption

                          WS_OVERLAPPEDWINDOW, // window style

                          CW_USEDEFAULT, // initial x position

                          CW_USEDEFAULT, // initial y position

                          CW_USEDEFAULT, // initial x size

                          CW_USEDEFAULT, // initial y size

                          NULL, // parent window handle

                          NULL, // window menu handle

                          hInstance, // program instance handle

                          NULL) ; // creation parameters

     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;//Directs the window to paint itself. 
     
     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 ;//绘图结构句柄
     RECT rect ;//矩形结构句柄,句柄是一数字,代表计算机中的一种资源;
   
     switch (message)
     {
     case WM_CREATE:
          PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          
          GetClientRect (hwnd, &rect) ;
          
          DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          EndPaint (hwnd, &ps) ;
          return 0 ;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

我们知道,一个窗口的创建,要经历,填充窗口类,注册窗口类,创建窗口,将窗口显示在屏幕上,然后进入消息循环,直到退出窗口为止;上述代码,有18个窗口函数,记忆这些函数是比较费力的,因此,书中给出这些API函数的基本描述:而对这些函数的具体功能,大家可以查阅MSDN获得;

  • LoadIcon Loads an icon for use by a program.

  • LoadCursor Loads a mouse cursor for use by a program.

  • GetStockObject Obtains a graphic object, in this case a brush used for painting the window's background.

  • RegisterClass Registers a window class for the program's window.

  • MessageBox Displays a message box.

  • CreateWindow Creates a window based on a window class.

  • ShowWindow Shows the window on the screen.

  • UpdateWindow Directs the window to paint itself.

  • GetMessage Obtains a message from the message queue.

  • TranslateMessage Translates some keyboard messages.

  • DispatchMessage Sends a message to a window procedure.

  • PlaySound Plays a sound file.

  • BeginPaint Initiates the beginning of window painting.

  • GetClientRect Obtains the dimensions of the window's client area.

  • DrawText Displays a text string.

  • EndPaint Ends window painting.

  • PostQuitMessage Inserts a "quit" message into the message queue.

  • DefWindowProc Performs default processing of messages.

    二: 大写字母下划线前戳的意义:

    CS Class style option        
    CW Create window option
    DT Draw text option
    IDI ID number for an icon
    IDC ID number for a cursor
    MB Message box options
    SND Sound option
    WM Window message
    WS Window style

    匈牙利表示法;
                     主要用在变量和子程序的命名中, 用连在一起的部分来命名变量,格式是类型前加上变量
               说明,
                       类型用小写字母,有如下几种:
                      说明则用首字母大写的几个英文单词组成

    Prefix Data Type
    c char or WCHAR or TCHAR
    by BYTE (unsigned char)
    n short
    i int
    x, y int used as x-coordinate or y-coordinate
    cx, cy int used as x or y length; c stands for "count"
    b or f BOOL (int); f stands for "flag"
    w WORD (unsigned short)
    l LONG (long)
    dw DWORD (unsigned long)
    fn function
    s string
    sz string terminated by 0 character
    h handle
    p pointer

    两种窗口类型定义;
    typedef struct tagWNDCLASSA
    {
         UINT        style ;
         WNDPROC     lpfnWndProc ;
         int         cbClsExtra ;
         int         cbWndExtra ;
         HINSTANCE   hInstance ;
         HICON       hIcon ;
         HCURSOR     hCursor ;
         HBRUSH      hbrBackground ;
         LPCSTR      lpszMenuName ;
         LPCSTR      lpszClassName ;
    }
    WNDCLASSA, * PWNDCLASSA, NEAR * NPWNDCLASSA, FAR * LPWNDCLASSA ;

    Notice some uses of Hungarian notation here: The lpfn prefix means "long pointer to a function." (Recall that in the Win32 API there is no distinction between long pointers and near pointers. This is a remnant of 16-bit Windows.) The cb prefix stands for "count of bytes" and is often used for a variable that denotes a byte size. The h prefix is a handle, and the hbr prefix means "handle to a brush." The lpsz prefix is a "long pointer to a string terminated with a zero."

    The Unicode version of the structure is defined like so:

     

    typedef struct tagWNDCLASSW 
    {
         UINT        style ;
         WNDPROC     lpfnWndProc ;
         int         cbClsExtra ;
         int         cbWndExtra ;
         HINSTANCE   hInstance ;
         HICON       hIcon ;
         HCURSOR     hCursor ;
         HBRUSH      hbrBackground ;
         LPCWSTR     lpszMenuName ;
         LPCWSTR     lpszClassName ;
    }
    WNDCLASSW, * PWNDCLASSW, NEAR * NPWNDCLASSW, FAR * LPWNDCLASSW ;
    
    两个结构唯一的不同,就是后一个结构比起前一个Ansi版本来,后两个结构域是宽字符版本;
    hwnd = CreateWindow (szAppName,    // window class name
            
        TEXT ( "The Hello Program"), // window caption
            
                WS_OVERLAPPEDWINDOW,     // window style
            
               CW_USEDEFAULT,           // initial x position
            
               CW_USEDEFAULT,           // initial y position
            
                CW_USEDEFAULT,            // initial x size
            
              CW_USEDEFAULT,           // initial y size
            
                NULL,                        // parent window handle
            
               NULL,                     // window menu handle
            
       hInstance,                  // program instance handle
            
       NULL) ;                      // creation parameters
    typedef struct tagMSG
            
    {
            
        HWND   hwnd ;
            
        UINT   message ;
            
        WPARAM wParam ;
            
        LPARAM lParam ;
            
        DWORD  time ;
            
        POINT  pt ;
            
    }
            
    MSG, * PMSG ;
            
    typedef struct tagPOINT
            
    {
            
        LONG  x ;
            
        LONG  y ;
            
    }
            
    POINT, * PPOINT;
    


     

  • 阅读(1150) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~