1. 程式人生 > 其它 >5-Windows程式設計 -繪圖基礎

5-Windows程式設計 -繪圖基礎

  5、1 設定畫素

       

   設定畫素一般用四個位元組,最高位省略不用。

 

// 5-1-PWCP_設定畫素_顏色.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-1-PWCP_設定畫素_顏色.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            //
主視窗類名 // 此程式碼模組中包含的函式的前向宣告: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_
int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此處放置程式碼。 // 初始化全域性字串 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_MY51PWCP, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance);
// 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY51PWCP)); MSG msg; // 主訊息迴圈: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函式: MyRegisterClass() // // 目標: 註冊視窗類。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY51PWCP)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;// MAKEINTRESOURCEW(IDC_MY51PWCP); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex); } // // 函式: InitInstance(HINSTANCE, int) // // 目標: 儲存例項控制代碼並建立主視窗 // // 註釋: // // 在此函式中,我們在全域性變數中儲存例項控制代碼並 // 建立和顯示主程式視窗。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 將例項控制代碼儲存在全域性變數中 HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函式: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目標: 處理主視窗的訊息。 // // WM_COMMAND - 處理應用程式選單 // WM_PAINT - 繪製主視窗 // WM_DESTROY - 傳送退出訊息並返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { size_t red, green, blue; TCHAR szBuffer[1024]; switch (message) { case WM_COMMAND: { int wmId = LOWORD(wParam); // 分析選單選擇: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // TODO: 在此處新增使用 hdc 的任何繪圖程式碼... COLORREF color; color = RGB(255, 0, 0); //賦值顏色 for (size_t i = 0; i < 100; i++) //設定畫素點顏色 SetPixel(hdc, 100+i, 100, color); RECT rect; GetClientRect(hWnd, &rect); for (size_t i = rect.left; i <= rect.right; i++) for (size_t j = rect.top; j <= rect.bottom; j++) { color = RGB(255, rand() % 256, rand() % 256); SetPixel(hdc, i, j, color); } //獲取畫素點顏色 color = GetPixel(hdc, 100, 100); red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color); wsprintf(szBuffer, TEXT("x=100,y= 100的畫素點:R = %dpx, G= %dpx, B = %dpx"), red, green, blue); TextOut(hdc, 1, 1, szBuffer, lstrlen(szBuffer)); EndPaint(hWnd, &ps); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // “關於”框的訊息處理程式。 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }

         

  5、2 畫直線

        

 

// 5-2-PWCP_直線.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-2-PWCP_直線.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY52PWCP, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY52PWCP));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY52PWCP));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;//MAKEINTRESOURCEW(IDC_MY52PWCP);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            RECT rect;
            POINT apt[] = { 300,100,400,100,400,200,300,200,300,100 };
            POINT apt2[] = { 500,100,600,100,600,200,500,200,500,100 };
            POINT apt3[] = { 700,100,800,100,800,200,700,200,700,100 };

            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...

            //畫矩形框
            MoveToEx(hdc, 100, 100, NULL); //移動畫筆的位置
            LineTo(hdc, 200, 100);  //畫完線後自動改變畫筆的位置
            LineTo(hdc, 200, 200);
            LineTo(hdc, 100, 200);
            LineTo(hdc, 100, 100);

            //迴圈畫
            MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
            for (size_t i = 1; i < 5; i++)
                LineTo(hdc, apt[i].x, apt[i].y);

            //polyline 函式畫
            Polyline(hdc, apt2, 5);


            //polylineTo 函式畫
            MoveToEx(hdc, apt3[0].x, apt3[0].y, NULL);
            PolylineTo(hdc,apt3+1,4);

            //畫表格
            /*GetClientRect(hWnd, &rect);
            for (size_t x = 0; x <= rect.right; x+=100)
            {
                MoveToEx(hdc, x, 0, NULL);
                LineTo(hdc, x, rect.bottom);
            }

            for (size_t i = 0; i < rect.bottom; i+=100)
            {
                MoveToEx(hdc, 0, i, NULL);
                LineTo(hdc, rect.right, i);
            }*/



            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

      

  5、3 SINEWAVE

         

 

// 5-3-PWCP_直線_SINEWAVE.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-3-PWCP_直線_SINEWAVE.h"
#include <math.h>

#define MAX_LOADSTRING 100
#define NUM 1000
#define TWOPI (2 * 3.1415926)

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY53PWCPSINEWAVE, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY53PWCPSINEWAVE));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY53PWCPSINEWAVE));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;// MAKEINTRESOURCEW(IDC_MY53PWCPSINEWAVE);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, TEXT("SINEWAVE"), WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    //寬度,和高度
    static int cxClient, cyClient;
    POINT apt[NUM];
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            HPEN hPen = CreatePen(PS_DASH, 2, RGB(255, 0, 0));
            HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);

            MoveToEx(hdc, 0, cyClient / 2, NULL);
            LineTo(hdc, cxClient, cyClient / 2);

            for (size_t i = 0; i < NUM; i++)
            {
                apt[i].x =i *  cxClient / NUM;
                apt[i].y = (int)(cyClient / 2 * (1 - sin(TWOPI * i / NUM)));
                //SetPixel(hdc, apt[i].x, apt[i].y, RGB(255, 0, 0));
            }
            Polyline(hdc, apt, NUM);

            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

   5、 4 邊框繪製函式

      

 

 

// 5-4-PWCP_邊框繪製函式.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-4-PWCP_邊框繪製函式.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY54PWCP, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY54PWCP));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY54PWCP));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY54PWCP);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static int cxClient, cyClient;
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            
            //矩形
            //Rectangle(hdc, 200, 200, 400, 300);

            //橢圓
            //Ellipse(hdc, 200, 200, 400, 300);

            //倒角矩形
            //RoundRect(hdc, 200, 200, 400, 300, 50, 50);

            //圓弧
            //Arc(hdc, 200, 200, 400, 300, 350, 10, 10, 350);

            //弦弧
            //Chord(hdc, 200, 200, 400, 300, 350, 10, 10, 350);

            //頂弧
            //Pie(hdc, 200, 200, 400, 300, 350, 10, 10, 350);

            Rectangle(hdc, cxClient / 8, cyClient / 8, cxClient * 7 / 8, cyClient * 7 / 8);



            Ellipse(hdc, cxClient / 8, cyClient / 8, cxClient * 7 / 8, cyClient * 7 / 8);

            RoundRect(hdc, cxClient / 4, cyClient / 4, cxClient * 3 / 4, cyClient * 3 / 4, cxClient / 4, cyClient / 4);


            //對角線
            MoveToEx(hdc, 0, 0, NULL);
            LineTo(hdc, cxClient, cyClient);

            MoveToEx(hdc, cxClient, 0, NULL);
            LineTo(hdc, 0, cyClient);

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

       

 

 

 

   5、 5 貝塞爾曲線

     

// 5-5-PWCP_貝塞爾曲線.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-5-PWCP_貝塞爾曲線.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY55PWCP, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY55PWCP));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY55PWCP));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;//MAKEINTRESOURCEW(IDC_MY55PWCP);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
void DrawBezier(HDC hdc,POINT apt[])
{
    PolyBezier(hdc, apt, 4);
    //MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
    //LineTo(hdc, apt[1].x, apt[1].y);

    //MoveToEx(hdc, apt[2].x, apt[2].y, NULL);
    //LineTo(hdc, apt[3].x, apt[3].y);
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static POINT apt[4];
    size_t cxClient, cyClient;
    HDC hdc;
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);

        apt[0].x = cxClient/4;
        apt[0].y = cyClient/2;

    
        apt[1].x = cxClient / 2;
        apt[1].y = cyClient / 4;

        apt[2].x = cxClient / 2;
        apt[2].y = 3 * cyClient / 4;


        apt[3].x = 3 * cxClient / 4;
        apt[3].y = cyClient / 2;

        return 0 ;
    case WM_LBUTTONDOWN:
    case WM_RBUTTONDOWN:
    case WM_MOUSEMOVE:
        if (wParam & MK_LBUTTON || wParam & MK_RBUTTON) 
        {
            hdc = GetDC(hWnd);
            SelectObject(hdc, GetStockObject(WHITE_PEN));
            DrawBezier(hdc, apt);
            if (wParam & MK_LBUTTON)
            {
                apt[1].x = LOWORD(lParam);
                apt[1].y = HIWORD(lParam);
            }
            
            if (wParam & MK_RBUTTON)
            {
                apt[2].x = LOWORD(lParam);
                apt[2].y = HIWORD(lParam);
            }

            SelectObject(hdc, GetStockObject(BLACK_PEN));
            DrawBezier(hdc, apt);


            ReleaseDC(hWnd, hdc);
        }
        return 0;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...

            //apt[0].x = 100;
            //apt[0].y = 100;

            ////起點控點
            //apt[1].x = 50;
            //apt[1].y = 50;

            //apt[2].x = 250;
            //apt[2].y = 50;

            ////終點的控點
            //apt[3].x = 200;
            //apt[3].y = 100;


            DrawBezier(hdc, apt);

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

  5、 6 畫筆

      

 

// 5-6-畫筆.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-6-畫筆.h"

#define MAX_LOADSTRING 100
#define LINES 7

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY56, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY56));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY56));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;//MAKEINTRESOURCEW(IDC_MY56);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HPEN hPen1, hPen2, hPen3,hOldPen;
    static size_t cxClient, cyClient;
    size_t iLineDis;
    switch (message)
    {
    case WM_CREATE:
        hPen1 = CreatePen(PS_SOLID, 5,0);
        hPen2 = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
        hPen3 = CreatePen(PS_DOT, 1, RGB(0, 0, 255));
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            
            iLineDis = cyClient / (LINES + 1);


            //舊的畫筆儲存,選擇新的畫筆
            hOldPen = (HPEN)SelectObject(hdc,hPen1);

            MoveToEx(hdc, cxClient/10, iLineDis, NULL);
            LineTo(hdc, cxClient*9/10, iLineDis);

            SelectObject(hdc, hPen2);
            MoveToEx(hdc, cxClient / 10, iLineDis * 2, NULL);
            LineTo(hdc, cxClient * 9 / 10, iLineDis * 2);

            SelectObject(hdc, hPen3);
            MoveToEx(hdc, cxClient / 10, iLineDis * 3, NULL);
            LineTo(hdc, cxClient * 9 / 10, iLineDis * 3);


            SelectObject(hdc, CreatePen(PS_DASH, 1, RGB(0, 255, 0)));
            MoveToEx(hdc, cxClient / 10, iLineDis * 4, NULL);
            LineTo(hdc, cxClient * 9 / 10, iLineDis * 4);

            DeleteObject(SelectObject(hdc, CreatePen(PS_DASHDOT, 1, RGB(0, 0, 0))));
            MoveToEx(hdc, cxClient / 10, iLineDis * 5, NULL);
            LineTo(hdc, cxClient * 9 / 10, iLineDis * 5);

            DeleteObject(SelectObject(hdc, CreatePen(PS_DASHDOTDOT, 1, RGB(0, 0, 255))));
            MoveToEx(hdc, cxClient / 10, iLineDis * 6, NULL);
            LineTo(hdc, cxClient * 9 / 10, iLineDis * 6);

            DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));
            MoveToEx(hdc, cxClient / 10, iLineDis * 7, NULL);
            LineTo(hdc, cxClient * 9 / 10, iLineDis * 7);

            SelectObject(hdc,hOldPen);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        DeleteObject(hPen1);
        DeleteObject(hPen2);
        DeleteObject(hPen3);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

  

 

   5、  7 畫刷

   

 

// 5-7-畫刷.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-7-畫刷.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY57, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY57));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY57));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;//MAKEINTRESOURCEW(IDC_MY57);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            HBRUSH hBrush;
            PAINTSTRUCT ps;
            HPEN hPen;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));

            //無畫筆
            //hPen = CreatePen(PS_NULL, 0, 0);   

            SelectObject(hdc, hPen);
            hBrush =(HBRUSH)GetStockObject(LTGRAY_BRUSH);
            SelectObject(hdc, hBrush);
            Rectangle(hdc, 100, 100, 200, 200);
        
            hBrush = CreateSolidBrush(RGB(255, 255, 0));
            SelectObject(hdc, hBrush);
            Ellipse(hdc, 300, 100, 400, 200);

            hBrush = CreateHatchBrush(HS_BDIAGONAL, RGB(0, 255, 0));
            DeleteObject(SelectObject(hdc, hBrush));
            Rectangle(hdc, 100, 300, 200, 400);

            hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(0, 0, 255));
            DeleteObject(SelectObject(hdc, hBrush));
            Ellipse(hdc, 300, 300, 400, 400);


            hBrush = CreateHatchBrush(HS_HORIZONTAL, RGB(0, 255, 255));
            DeleteObject(SelectObject(hdc, hBrush));
            Ellipse(hdc, 500, 300, 600, 400);

            DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));
            DeleteObject(SelectObject(hdc, GetStockObject(WHITE_BRUSH)));

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

   5、 8  畫圖

     

 

 

// 5-8-畫圖.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-8-畫圖.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY58, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY58));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY58));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;//MAKEINTRESOURCEW(IDC_MY58);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            POINT point[5] = {
                {0,200},{600,200},{100,600},{300,0},{500,600}
            };
            POINT point2[9] = {
                {0,200},{600,200},{100,600},{300,0},{500,600},
                {400,50},{500,50},{500,150},{400,150}
            };
            int pn[2] = { 5,4 };

            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...

            //Rectangle(hdc, 100, 100, 320, 200);
            //RoundRect(hdc, 100, 100, 320, 200, 20, 20);
            //Ellipse(hdc, 100, 100, 320, 200);
            //Arc(hdc, 100, 100, 320, 200, 290, 50, 80, 300);
            //Pie(hdc, 100, 100, 320, 200, 200, 50, 80, 260);
            //Chord(hdc, 100, 100, 320, 200, 200, 50, 80, 260);
            Polygon(hdc, point, 5);
            PolyPolygon(hdc, point2, pn, 2);

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

 

   5、 9 多邊形填充模式

     

    

// 5-9-多邊形填充模式.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-9-多邊形填充模式.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY59, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY59));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY59));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY59);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    POINT point[5] = { {0,200},{600,200},{100,600},{300,0},{500,600} };
    static POINT aptFigure[10] =
    { 10,70,50,70,50,10,90,10,90,50,
        30,50,30,90,70,90,70,30,10,30 };
    POINT apt[10];
    static int cxClient, cyClient;
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            SelectObject(hdc, GetStockObject(GRAY_BRUSH));
            //SetPolyFillMode(hdc, ALTERNATE);
            //SetPolyFillMode(hdc, WINDING);
            //Polygon(hdc, point, 5);

            for (size_t i = 0; i < 10; i++)
            {
                apt[i].x = cxClient * aptFigure[i].x / 200; 
                apt[i].y = cyClient * aptFigure[i].y / 100;
            }
            SetPolyFillMode(hdc, ALTERNATE);
            Polygon(hdc, apt, 10);

            for (size_t i = 0; i < 10; i++)
            {
                apt[i].x += cxClient / 2;
            }
            SetPolyFillMode(hdc, WINDING);
            Polygon(hdc, apt, 10);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

 

   5、10  GDI對映模式

    

 

 

// 5-10-對映模式.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-10-對映模式.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY510, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY510));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY510));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY510);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            int iMapMode;
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            iMapMode = GetMapMode(hdc);

            //預設畫素對映模式
            SetMapMode(hdc, MM_TEXT);
            Rectangle(hdc, 100, 100, 200, 200);

            // 0.1mm
            SetMapMode(hdc, MM_LOMETRIC);
            Rectangle(hdc, 100, -100, 200, -200);

            // 0.01mm
            SetMapMode(hdc, MM_HIMETRIC);
            Ellipse(hdc, 1000, -1000, 2000, -2000);

            //0.01 in
            SetMapMode(hdc, MM_LOENGLISH);
            Rectangle(hdc, 100, -100, 200, -200);

            //0.001 in
            SetMapMode(hdc, MM_HIENGLISH);
            Ellipse(hdc, 1000, -1000, 2000, -2000);

            // 1/1440 in = 0.0176mm
            SetMapMode(hdc, MM_TWIPS);
            Rectangle(hdc, 1000, -1000, 2000, -2000);

            SetMapMode(hdc, iMapMode);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

   5、 11 GDI座標轉換

    

    

 

     

    

 

// 5-11-GDI 座標轉換.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-11-GDI 座標轉換.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY511GDI, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY511GDI));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY511GDI));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL; //MAKEINTRESOURCEW(IDC_MY511GDI);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static int cxClient, cyClient;
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            //繪圖時,寫程式碼使用的是邏輯座標
            //在寫程式碼,就像在一張大大的紙上畫圖,這張紙就是視窗
            MoveToEx(hdc, 0, cyClient / 2, NULL);
            LineTo(hdc, cxClient, cyClient/2);

            MoveToEx(hdc, cxClient / 2, 0, NULL);
            LineTo(hdc, cxClient / 2, cyClient);



            SetMapMode(hdc, MM_TEXT);
            SetMapMode(hdc, MM_LOENGLISH);
            //SetViewportOrgEx(hdc, cxClient/2, cyClient/2, NULL);
            //DPtoLP   裝置座標轉邏輯座標
            //LPtoDP   邏輯座標轉裝置座標
            POINT pt;
            pt.x = cxClient;
            pt.y = cyClient;
            DPtoLP(hdc, &pt, 1);

            SetWindowOrgEx(hdc, -pt.x / 2, -pt.y / 2, NULL);


            TextOut(hdc, 0, 0, TEXT("hello"), 5);
            //改變視口座標原點
            //SetViewportOrgEx(hdc, 100, 200, NULL);   
            //SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);

            //改變視窗座標原點
            //SetWindowOrgEx(hdc, -cxClient / 2, -cyClient / 2, NULL);
            //SetWindowOrgEx(hdc, 30, 10, NULL);
            //TextOut(hdc, 0, 0, TEXT("hello"), 5);
            //SetWindowOrgEx(hdc, 100, 50, NULL);
            //Rectangle(hdc, 0, 0, 200, 100);

            //for (size_t i = 0; i < 5; i++)
            //{
            //    //SetWindowOrgEx(hdc, 0, -16 * i, NULL);
            //    SetWindowOrgEx(hdc,  -56 * i, 0, NULL);
            //    //TextOut(hdc, 0, 16 * i, TEXT("hello"), 5);
            //    TextOut(hdc, 0, 0, TEXT("hello"), 5);

            //}




            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

 

   5、 12 自定義對映模式

    

// 5-12-自定義的對映模式.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-12-自定義的對映模式.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY512, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY512));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY512));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;//MAKEINTRESOURCEW(IDC_MY512);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static size_t cxClient, cyClient;
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...

            SetMapMode(hdc, MM_ISOTROPIC);
            SetWindowExtEx(hdc, 200, 200, NULL);
            SetViewportExtEx(hdc, cxClient, cyClient, NULL);
            SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);
            Ellipse(hdc, -80, 80, 80, -80);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

             (上圖各向同性)

    

 

             (上圖各向異性)

  5、  13  RECT

    

// 5-13-RECT.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-13-RECT.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY513RECT, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY513RECT));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY513RECT));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;    // MAKEINTRESOURCEW(IDC_MY513RECT);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HBRUSH hBrush;
    RECT rect = { 100,100,300,200 };
    RECT rect2;
    RECT rect3;
    BOOL r;
    POINT p;
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_LBUTTONDOWN:
        p.x = LOWORD(lParam);
        p.y = HIWORD(lParam);

        //點是否在 在矩形範圍內
        if (PtInRect(&rect, p))
        {
            MessageBox(hWnd, TEXT("點中了"),TEXT("TEST"), MB_OK);
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            hBrush = CreateSolidBrush(RGB(255, 0, 0));


            //放大矩形
            //InflateRect(&rect, 50, 10);

            //偏移位置
            //OffsetRect(&rect, 200, 200);

            //SetRect(&rect, 10, 10, 500, 400);

            //矩形填充
            //FillRect(hdc, &rect, hBrush);

            CopyRect(&rect2, &rect);
            OffsetRect(&rect2, 100, 50);
            //FillRect(hdc, &rect2, hBrush);
            //IntersectRect(&rect3, &rect, &rect2);
            UnionRect(&rect3, &rect, &rect2);

            FillRect(hdc, &rect3, hBrush);

            SetRectEmpty(&rect);

            r = IsRectEmpty(&rect2);
            //矩形的邊
            //FrameRect(hdc, &rect, hBrush);

            //反轉矩形
            //InvertRect(hdc, &rect);


            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

  5、 14 處理矩形

     

 

// 5-14-處理矩形.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-14-處理矩形.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY514, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY514));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY514));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;//MAKEINTRESOURCEW(IDC_MY514);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    RECT rect = { 100,100,200,200 };
    RECT rect2 = { 100,100,200,200 };
    RECT rect3;
    RECT rect4 = { 50,300,100,400 };
    RECT rect5 = { 75,360,150,480 };
    RECT rect6;
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...

            FillRect(hdc, &rect,(HBRUSH)GetStockObject(BLACK_BRUSH));

            SetRect(&rect, 300, 100, 400, 200);
            FillRect(hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
            rect.left = 500;
            rect.top = 100;
            rect.right = 600;
            rect.bottom = 200;
            FillRect(hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));

            OffsetRect(&rect2, 200, 200);
            FillRect(hdc, &rect2, (HBRUSH)GetStockObject(BLACK_BRUSH));

            InflateRect(&rect2, 50, 50);
            FillRect(hdc, &rect2, (HBRUSH)GetStockObject(BLACK_BRUSH));

            CopyRect(&rect3, &rect2);
            IntersectRect(&rect6, &rect4, &rect5);

            //UnionRect(&rect6, &rect4, &rect5);
            FillRect(hdc, &rect6, (HBRUSH)GetStockObject(BLACK_BRUSH));

            SetRectEmpty(&rect6);
            IsRectEmpty(&rect6);

            POINT point = { 100,200 };
            PtInRect(&rect5, point);

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

 

   5、 15  隨機矩形

     

// 5-15-隨機矩形.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-15-隨機矩形.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
int cxClient, cyClient;
HWND hWnd;
void DrawRectangle(HWND);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY515, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY515));

    MSG msg;

    // 主訊息迴圈:
   /* while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }*/

    while (true)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else  //現在windows沒有訊息要處理
        {
            DrawRectangle(hWnd);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY515));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL; //MAKEINTRESOURCEW(IDC_MY515);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

    hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

    void DrawRectangle(HWND hWnd)
    {
        HBRUSH hBrush;
        HDC hdc;
        RECT rect;
        if (cxClient == 0 || cyClient == 0)
            return;
        SetRect(&rect, rand() % cxClient, rand() % cyClient,
            rand() % cxClient, rand() % cyClient);
        hBrush = CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256));

        hdc = GetDC(hWnd);
        FillRect(hdc, &rect, hBrush);
        ReleaseDC(hWnd, hdc);
        DeleteObject(hBrush);
    }
View Code

    

   5、 16 區域和剪裁

    

// 5-16-區域和剪裁.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-16-區域和剪裁.h"
#include <math.h>

#define MAX_LOADSTRING 100
#define TWO_PI (2.0*3.14159)

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY516, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY516));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY516));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;//MAKEINTRESOURCEW(IDC_MY516);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HRGN hRgnTemp[6];
    static HRGN hRgnClip;
    static size_t cxClient, cyClient;
    double fRadius, fAngle;
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);

        hRgnTemp[0] = CreateEllipticRgn(0, cyClient / 3, cxClient / 2, 2 * cyClient / 3);
        hRgnTemp[1] = CreateEllipticRgn(cxClient / 2, cyClient / 3, cxClient, 2 * cyClient / 3);
        hRgnTemp[2] = CreateEllipticRgn(cxClient / 3, 0, 2 * cxClient / 3, cyClient/2);
        hRgnTemp[3] = CreateEllipticRgn(cxClient / 3, cyClient / 2, 2 * cxClient / 3, cyClient);
        hRgnTemp[4] = CreateRectRgn(0, 0, 1, 1);
        hRgnTemp[5] = CreateRectRgn(0, 0, 1, 1);
        CombineRgn(hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR);
        CombineRgn(hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR);
        hRgnClip = CreateRectRgn(0, 0, 1, 1);
        CombineRgn(hRgnClip, hRgnTemp[4], hRgnTemp[5], RGN_XOR);

        return 0;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
        
            //FillRgn(hdc, hRgnTemp[5], (HBRUSH)GetStockObject(BLACK_BRUSH));
            //FillRgn(hdc, hRgnClip, (HBRUSH)GetStockObject(BLACK_BRUSH));

            SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2,NULL);
        
            SelectClipRgn(hdc, hRgnClip);
            fRadius = _hypot(cxClient/2.0,cyClient/2.0);

            for ( fAngle = 0.0; fAngle < TWO_PI; fAngle+=TWO_PI/360)
            {
                MoveToEx(hdc,0, 0, NULL);
                LineTo(hdc, (int)(fRadius * cos(fAngle) + 0.5), (int)(fRadius * sin(fAngle) + 0.5));
            }
            

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code

    

 

   5、  17 繪圖模式

    

// 5-17-繪圖模式.cpp : 定義應用程式的入口點。
//

#include "framework.h"
#include "5-17-繪圖模式.h"

#define MAX_LOADSTRING 100

// 全域性變數:
HINSTANCE hInst;                                // 當前例項
WCHAR szTitle[MAX_LOADSTRING];                  // 標題欄文字
WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類名

// 此程式碼模組中包含的函式的前向宣告:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此處放置程式碼。

    // 初始化全域性字串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY517, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 執行應用程式初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY517));

    MSG msg;

    // 主訊息迴圈:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函式: MyRegisterClass()
//
//  目標: 註冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY517));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;//MAKEINTRESOURCEW(IDC_MY517);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函式: InitInstance(HINSTANCE, int)
//
//   目標: 儲存例項控制代碼並建立主視窗
//
//   註釋:
//
//        在此函式中,我們在全域性變數中儲存例項控制代碼並
//        建立和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 將例項控制代碼儲存在全域性變數中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目標: 處理主視窗的訊息。
//
//  WM_COMMAND  - 處理應用程式選單
//  WM_PAINT    - 繪製主視窗
//  WM_DESTROY  - 傳送退出訊息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    HPEN hPen;
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析選單選擇:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_LBUTTONDOWN:
        hdc = GetDC(hWnd);
        //使用的是預設的繪圖模式
        SetROP2(hdc, R2_COPYPEN); 

        SetROP2(hdc, R2_NOT);

        hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
        SelectObject(hdc, hPen);

        MoveToEx(hdc, 100, 200, NULL);
        LineTo(hdc, 400, 200);
        ReleaseDC(hWnd,hdc);
        break;
    case WM_PAINT:
        {
            hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此處新增使用 hdc 的任何繪圖程式碼...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “關於”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
View Code