SetViewportOrgEx和SetWindowOrgEx

在MM_TEXT映射模式下使用这两个函数。

对于

BOOL SetViewportOrgEx(
HDC hdc, // 设备内容HANDLE
int X, // 新Viewport的x坐标
int Y, // 新Viewport的y坐标
LPPOINT lpPoint // 原来的Viewport的坐标
);

这样逻辑点(0,0)将映射到设备点(X,Y),即(X,Y)成为了原点。

 

对于

BOOL SetWindowOrgEx(HDC hdc, int X, int Y, LPPOINT lpPoint);

SetWindowOrgEx函数的参数总是以逻辑单位的形式给出

 

#include <Windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("WNDCLASS NAME");
    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("窗口创建失败!需要Windows NT!(传递消息为UNICODE)"), szAppName, MB_ICONERROR);

        return 0;
    }

    hwnd = CreateWindow(
        szAppName,
        TEXT("Hk_Mayfly"),
        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) {
    HDC hdc;
    PAINTSTRUCT ps;
    static int iMapMode;
    TCHAR szBuffer[] = L"Hk_Mayfly!!!!?";
    static int cxClient, cyClient;

    switch (message) {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        //1
        TextOut(hdc, 0, 0, TEXT(""), 1);
        SelectObject(hdc, CreateSolidBrush(RGB(0, 245, 255)));//蓝色
        Ellipse(hdc, 0, 0, 100, 100);
        SelectObject(hdc, CreateSolidBrush(RGB(67,205,128)));//绿色
        Ellipse(hdc, cxClient - 100, cyClient - 100, cxClient, cyClient);

        //2
        //SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);//将逻辑点(0,0)映射到设备点(cxClient / 2,cyClient / 2),即将(cxClient / 2,cyClient / 2)作为(0,0)点
        /*TextOut(hdc, 0, 0, TEXT("●"), 1);
        SelectObject(hdc, CreateSolidBrush(RGB(0, 245, 255)));
        Ellipse(hdc, 0, 0, 100, 100);
        SelectObject(hdc, CreateSolidBrush(RGB(67, 205, 128)));
        Ellipse(hdc, cxClient - 100, cyClient - 100, cxClient, cyClient);*/

        //3
        /*将逻辑点(cxClient / 2,cyClient / 2)映射到设备点(0,0),将(0,0)点作为(0,200)*/
        /*SetWindowOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);
        TextOut(hdc, 0, 0, TEXT("●"), 1);
        SelectObject(hdc, CreateSolidBrush(RGB(0, 245, 255)));
        Ellipse(hdc, 0, 0, 100, 100);
        SelectObject(hdc, CreateSolidBrush(RGB(67, 205, 128)));
        Ellipse(hdc, cxClient - 100, cyClient - 100, cxClient, cyClient);*/
        EndPaint(hwnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

1.

 

2.

 

3.

 

posted @ 2019-08-26 18:07  Hk_Mayfly  阅读(383)  评论(0编辑  收藏  举报