文件: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;
}
由于与图形学无关,这里只需知道函数名及相应功能即可。
本文介绍了一个简单的Windows图形界面应用程序的实现方法,包括主窗口的创建、位置设置、大小调整以及最大化等基本操作。
2199

被折叠的 条评论
为什么被折叠?



