zoukankan      html  css  js  c++  java
  • Windows程序设计珍藏版-正弦波源码

    WM_CREATE,WM_SIZE,WM_PAINT三个消息的先后顺序

    /*-----------------------------------------
       SINEWAVE.C -- Sine Wave Using Polyline
                     (c) Charles Petzold, 1998
      -----------------------------------------*/
    
    #include <windows.h>
    #include <math.h>
    
    #define NUM    1000
    #define TWOPI  (2 * 3.14159)
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        PSTR szCmdLine, int iCmdShow)
    {
        static TCHAR szAppName[] = TEXT("SineWave");
        HWND         hwnd;
        MSG          msg;
        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;
    
        if (!RegisterClass(&wndclass))
        {
            MessageBox(NULL, TEXT("Program requires Windows NT!"),
                szAppName, MB_ICONERROR);
            return 0;
        }
    
        hwnd = CreateWindow(szAppName, TEXT("Sine Wave Using Polyline"),
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT,
            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)
    {
        static int  cxClient, cyClient;
        HDC         hdc;
        int         i;
        PAINTSTRUCT ps;
        POINT       apt[NUM];
        CREATESTRUCT cs;
        RECT cr;
    
        switch (message)
        {
          //源码中没有对这个消息的处理,自己添加,以及上边的cr变量;
        case WM_CREATE:
            GetClientRect(hwnd, &cr);
            cxClient = cr.right - cr.left;
            cyClient = cr.bottom - cr.top;
            return 0;
        case WM_SIZE:
            cxClient = LOWORD(lParam);
            cyClient = HIWORD(lParam);
            return 0;
    
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
    
            MoveToEx(hdc, 0, cyClient / 2, NULL);
            LineTo(hdc, cxClient, cyClient / 2);
            
            for (i = 0; i < NUM; i++)
            {
                apt[i].x = i * cxClient / NUM;
                apt[i].y = (int)(cyClient / 2 * (1 - sin(TWOPI * i / NUM)));
            }
    
            Polyline(hdc, apt, NUM);
            
            EndPaint(hwnd, &ps);
            return 0;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }

    在三个消息后边下断点,发现先后顺序是

    WM_CREATE,WM_PAINT,WM_SIZE,

    源码中没有对create消息进行处理,导致程序一开始运行时,先处理paint的消息时,cxClient和cyClient初始值都还是0,看不到曲线波图形。点击最大最小化按钮,触发WM_SIZE消息后,对cxClient和cyClient复制后,才看到曲线波。

  • 相关阅读:
    SecureCRT
    NFS服务器搭建
    卸载oracle步骤
    redhat 5.5 x86_x64搭建samba服务器
    ORA-01940: cannot drop a user that is currently connected
    迁移表空间
    日常SQL使用总结
    DataGuard常规操作命令
    使用PowerDesginer画ER图
    数据库卸载
  • 原文地址:https://www.cnblogs.com/a-s-m/p/12893175.html
Copyright © 2011-2022 走看看