首页 > 用户发贴区 > 编程问题提问区 > c语言windows程序设计自学笔记(随学习更新)
2008
01-26

c语言windows程序设计自学笔记(随学习更新)

今天找到了windows程序设计一书,想来打开看看学习一下,发现很混乱,原理学的c语言和这个看起来很不象,看一个例子用了很长时间,我把例子中的语句查了一下,并标记上注释这样就好看多了,用c语言开发图形界面要做的东西比较繁琐,不过道理还是比较清晰的,比用工具直接画要理解的清楚多了。


下面看程序吧:


#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;    //windows消息
    WNDCLASS wndclass;    //窗口类
    wndclass.style = CS_HREDRAW | CS_VREDRAW;   //窗口样式
    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 = szAppName;   //窗口类名为”HelloWin”
    /*下面是窗口类的注册*/
    if (!RegisterClass(&wndclass)){
        /*消息框*/
        MessageBox(NULL, TEXT(“This program requires Windows NT!”),szAppName, MB_ICONERROR);
        return 0;
    }
    /*创建窗口,并赋值给窗口句柄*/
    hwnd = CreateWindow(szAppName, //窗口类名
                        TEXT(“第一个窗口”), //窗口标题
                        WS_OVERLAPPEDWINDOW, //窗口样式
                        CW_USEDEFAULT, //初始化窗口x坐标
                        CW_USEDEFAULT, //初始化窗口y坐标,x坐标和y坐标是屏幕左上角,窗口开始处
                        CW_USEDEFAULT, //初始化窗口宽大小
                        CW_USEDEFAULT, //初始化窗口高大小
                        NULL, //父窗口句柄
                        NULL, //窗口菜单句柄
                        hInstance,//创建此窗口的应用程序当前句柄
                        NULL); //创建参数
    ShowWindow(hwnd, iCmdShow); //显示窗口
    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;          //设备描述表(Device Context)的句柄,设备描述表中记录和此设备相关的各种信息
    PAINTSTRUCT ps;   //保存了窗口绘制客户区的一些信息,绘制客户区时是否要清除背景色,要更新的客户区的矩形区域的大小等等
    RECT rect;
    switch (message){
    case WM_CREATE:  //窗体一生成时产生的系列动作
        PlaySound(TEXT(“hellowin.wav”),   //指定播放声音的字符串
             NULL,                   //应用程序的实例句柄
                  SND_FILENAME | SND_ASYNC);  //播放标志SND_FILENAME指定了WAVE文件名,SND_ASYNC用异步方式播放声音
        return 0;
    case WM_PAINT:   //WM_PAINT是Windows窗口系统中一条重要的消息,应用程序通过处理该消息实现在窗口上的绘制工作
        hdc = BeginPaint(hwnd,&ps);  //beginpaint得到一个HDC的句柄和一个PAINTSTRUCT的结构,该结构包含了要重绘的各中信息
        GetClientRect(hwnd,&rect);
        DrawText(hdc,        //DC句柄
             TEXT(“windows窗口程序”),  //画文本
                 -1,                   //指向字符串中的字符数
                 &rect,               //指向结构RECT的指针,其中包含正文将被置于其中的矩形的信息(按逻辑坐标)
                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);    //DT_SINGLELINE只画单行,DT_CENTER文本垂直居中,DT_VCENTER水平居中
        EndPaint(hwnd,&ps);    //ENDPAINT()函数告诉WINDOWS已经时无效区变为有效,防止WM_PAINT
        return 0;
    case WM_DESTROY:  //该讯息是使用者单击Close按钮或者在程式的系统功能表上选择
        PostQuitMessage(0); //调用PostQuitMessage函数退出程序
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}



c语言windows程序设计自学笔记(随学习更新)》有 3 条评论

  1. SimonSworD 说:

    寒… DEV_C++里新建工程中可以自动创建…

  2. wowomeme11 说:

    呵呵  不错

    支持一下

  3. SimonSworD 说:

    #include <windows.h>

    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

    /*  Make the class name into a global variable  */
    char szClassName[ ] = “WindowsApp”;

    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)

    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */

        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);

        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows’s default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;

        /* The class is registered, let’s create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               “Windows App”,       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );

        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);

        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }

        /* The program return-value is 0 – The value that PostQuitMessage() gave */
        return messages.wParam;
    }

    /*  This function is called by the Windows function DispatchMessage()  */

    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don’t deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }

        return 0;
    }

留下一个回复