【计算机图形学】1-1-3 主窗口的基本参数设置

本文介绍了一个简单的Windows图形界面应用程序的实现方法,包括主窗口的创建、位置设置、大小调整以及最大化等基本操作。

文件:gui_windows.h

#ifndef GUI_WINDOWS_H_INCLUDED
#define GUI_WINDOWS_H_INCLUDED

#include <assert.h>
#include <windows.h>
#include "define.h"

#define main fake_main

int fake_main();
int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR, int);
LRESULT CALLBACK gui_mainProcess(HWND, UINT, WPARAM, LPARAM);
void gui_createMainWindow(const char *);
void gui_setMainWindowPosition(int32 x, int32 y);
void gui_setMainWindowSize(int32 w, int32 h);
void gui_setMainWindowToCenter();
void gui_setMainWindowMinimize();
void gui_setMainWindowMaximize();
void gui_setMainWindowRestore();
void gui_mainLoop();

#endif // GUI_WINDOWS_H_INCLUDED

文件:gui_windows.c

#include "gui_windows.h"

static HINSTANCE mainInstance;
static HWND handleMainWindow;
static int32 systemWidth, systemHeight;

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   PSTR szCmdLine,
                   int iCmdShow)
{
    mainInstance = hInstance;
    systemWidth = GetSystemMetrics(SM_CXSCREEN);
    systemHeight = GetSystemMetrics(SM_CYSCREEN);
    return fake_main();
}

void gui_createMainWindow(const char *title)
{
    WNDCLASS mainWindow;
    mainWindow.style            = CS_HREDRAW | CS_VREDRAW;
    mainWindow.lpfnWndProc      = gui_mainProcess;
    mainWindow.cbClsExtra       = 0;
    mainWindow.cbWndExtra       = 0;
    mainWindow.hInstance        = mainInstance;
    mainWindow.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    mainWindow.hCursor          = LoadCursor(NULL, IDC_ARROW);
    mainWindow.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
    mainWindow.lpszMenuName     = NULL;
    mainWindow.lpszClassName    = "mainWindow";
    if(!RegisterClass(&mainWindow))
    {
        MessageBox(NULL, TEXT("Cannot register class for main window."), TEXT(title), MB_ICONERROR);
        exit(0);
    }
    handleMainWindow = CreateWindow("mainWindow",
                                    title,
                                    WS_OVERLAPPEDWINDOW,
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    NULL,
                                    NULL,
                                    mainInstance,
                                    NULL);
    ShowWindow(handleMainWindow, 1);
    UpdateWindow(handleMainWindow);
}

void gui_setMainWindowPosition(int32 x, int32 y)
{
    assert(handleMainWindow != NULL);
    SetWindowPos(handleMainWindow, NULL, x, y, 0, 0, SWP_NOSIZE);
}

void gui_setMainWindowSize(int32 w, int32 h)
{
    assert(handleMainWindow != NULL);
    SetWindowPos(handleMainWindow, NULL, 0, 0, w, h, SWP_NOMOVE);
}

void gui_setMainWindowToCenter()
{
    assert(handleMainWindow != NULL);
    RECT rect;
    GetWindowRect(handleMainWindow, &rect);
    int32 w = rect.right - rect.left;
    int32 h = rect.bottom - rect.top;
    gui_setMainWindowPosition((systemWidth - w) >> 1, (systemHeight - h) >> 1);
}

void gui_setMainWindowMinimize()
{
    assert(handleMainWindow != NULL);
    SendMessage(handleMainWindow, WM_SYSCOMMAND, SC_MINIMIZE, (LPARAM)NULL);
}

void gui_setMainWindowMaximize()
{
    assert(handleMainWindow != NULL);
    SendMessage(handleMainWindow, WM_SYSCOMMAND, SC_MAXIMIZE, (LPARAM)NULL);
}

void gui_setMainWindowRestore()
{
    assert(handleMainWindow != NULL);
    SendMessage(handleMainWindow, WM_SYSCOMMAND, SC_RESTORE, (LPARAM)NULL);
}

void gui_mainLoop()
{
    assert(handleMainWindow != NULL);
    MSG message;
    while(GetMessage(&message, NULL, 0, 0))
    {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
}

LRESULT CALLBACK gui_mainProcess(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc ;
    PAINTSTRUCT ps ;
    RECT rect ;
    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        EndPaint(hwnd, &ps) ;
        break;
    case WM_DESTROY:
        PostQuitMessage(0) ;
        break;
    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
}

文件:main.c

#include <stdio.h>
#include <stdlib.h>
#include "gui.h"

int main()
{
    #ifndef DEBUG
        freopen("log.txt", "w", stdout);
    #endif
    freopen("error.txt", "w", stderr);
    gui_createMainWindow("ZPIC");
    gui_setMainWindowSize(800, 600);
    gui_setMainWindowToCenter();
    gui_setMainWindowMaximize();
    gui_mainLoop();
    return 0;
}

由于与图形学无关,这里只需知道函数名及相应功能即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值