当前位置:网站首页>QT learning 02 GUI program example analysis
QT learning 02 GUI program example analysis
2022-06-29 23:59:00 【A little black sauce】
Qt Study 02 GUI Program example analysis
GUI Program development overview
Modern operating systems support GUI Interface
- Modern operating system Provide native SDK Support GUI Application development
- GUI Application development It's on modern operating systems Mainstream technology
- On different operating systems GUI The principle of development is the same
- On different operating systems *GUI SDK Different
GUI The principle of program development
- GUI At run time, the program will Create a message queue
- The system kernel will The user action Translate into the corresponding Program messages
- The program is in Operation process China needs Real time processing of messages in the queue
- When there are no messages in the queue , The program will be in A state of stagnation

- Different operating systems Support the same GUI Principles of development

/* Pseudo code */
int main()
{
fd = DefineMainWindow();
win = CreateMainWindow(fd);
CreateElements(win);
DsiplayMainWindow(win);
while (GetMessage(&msg)) {
// Send msg to win
}
return 0;
}
GUI The essence of program development
GUI Application development :
- Use... In code The program creates windows and window elements
- In message processing functions Make different responses according to program messages
classic GUI Program development mode :
- Visual interface development , Through what you see is what you get “ draw ” Exit interface ; The development environment automatically generates the corresponding program code
- Message mapping , The program maps the specific message to the specified function ; When a message is triggered , Function called
GUI Examples of program development
Most operating systems operate with C Function GUI SDK
With Windows Operating system as an example
Function name function RegisterClass Register with the system GUI Window style CreateWindow Create a window or window element ShowWindow Display the created window on the screen UpdateWindow Refresh the window on the screen GetMessage Get the message of the program message queue TranslateMessage Translate system messages DispatchMessage Send the message to the window handler
#include <windows.h>
#define STYLE_NAME L"MainForm"
#define BUTTON_ID 919
/* Main window definition function */
BOOL DefineMainWindow(HINSTANCE hInstance);
/* Main window creation function */
HWND CreateMainWindow(HINSTANCE hInstance, wchar_t* title);
/* Main window internal element creation function */
HWND CreateButton(HWND parent, int id, wchar_t* text);
/* The main window shows functions */
HWND DisplayMainWindow(HWND hWnd, int nCmdShow);
/* Main window message handling function */
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
static HWND MainWindow = NULL; // The main window handle is marked
BOOL WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg = {
0};
/* 1. Customize the main window style */
if( !DefineMainWindow(hInstance) )
{
return FALSE;
}
/* 2. Create main window */
MainWindow = CreateMainWindow(hInstance, STYLE_NAME);
if( MainWindow )
{
/* 3. Create control elements in the main window */
CreateButton(MainWindow, BUTTON_ID, L"My Button");
/* 4. Show the main window on the screen */
DisplayMainWindow(MainWindow, nCmdShow);
}
else
{
return FALSE;
}
/* 5. Enter the message loop */
while( GetMessage(&Msg, NULL, NULL, NULL) )
{
/* 6. Translate and convert system messages */
TranslateMessage(&Msg);
/* 7. Distribute the message to the corresponding message processing function */
DispatchMessage(&Msg);
}
return TRUE;
}
BOOL DefineMainWindow(HINSTANCE hInstance)
{
static WNDCLASS WndClass = {
0}; // System structure type
// Used to describe window styles
WndClass.style = 0;
WndClass.cbClsExtra = 0;
WndClass.cbClsExtra = 0;
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW); // Define the window background color
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Define the mouse style
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // Define the icon in the upper left corner of the window
WndClass.hInstance = hInstance; // Defining the window style belongs to the current application
WndClass.lpfnWndProc = WndProc; // Window message handling functions
WndClass.lpszClassName = STYLE_NAME; // Window style name
WndClass.lpszMenuName = NULL;
/* Register the defined window style into the system */
return RegisterClass(&WndClass);
}
HWND CreateMainWindow(HINSTANCE hInstance, wchar_t* title)
{
HWND hwnd = NULL;
hwnd = CreateWindow(STYLE_NAME, // Create the main window through the defined window style
title, // Main window title
WS_OVERLAPPEDWINDOW, // The display style of the main window after creation
CW_USEDEFAULT, // The upper left corner of the main window x coordinate
CW_USEDEFAULT, // The upper left corner of the main window y coordinate
CW_USEDEFAULT, // Main window width
CW_USEDEFAULT, // Main window height
NULL, // The parent window
NULL, // Window menu bar
hInstance, // The main window belongs to the current application
NULL); // Window parameters
return hwnd;
}
HWND DisplayMainWindow(HWND hWnd, int nCmdShow)
{
ShowWindow(hWnd,nCmdShow); // Display window
UpdateWindow(hWnd); // Refresh the window
return hWnd;
}
HWND CreateButton(HWND parent, int id, wchar_t* text)
{
HINSTANCE hInstance = (HINSTANCE)GetWindowLong(parent, GWL_HINSTANCE);
HWND hwnd = NULL;
hwnd = CreateWindow(L"button", // Create window elements through the predefined style of the system
text, // Window element title
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // Display style of window elements
50, // The window element is in the upper left corner of the window x coordinate
50, // The window element is in the upper left corner of the window y coordinate
200, // The width of the window element
60, // The height of the window element
parent, // The parent window of the window element
(HMENU)id, // Window elements ID value
hInstance, // Window elements belong to the current application
NULL); // Window element parameters
return hwnd;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
/* Call the default message handling function provided by the system */
return DefWindowProc(hWnd, message, wParam, lParam);
}
Summary
Modern operating system Provide native SDK Support GUI Application development
On different operating systems GUI SDK Different
On different operating systems GUI The principle of development is the same
GUI Program development includes :
Use... In code The program creates windows and window elements
In message processing functions Respond differently based on program messages
边栏推荐
- Applet plug-in access, development and precautions
- Embedded development: Hardware in the loop testing
- Leetcode(633)——平方数之和
- Solution to version conflict of flutter plug-in
- @Scheduled annotation pit, I stepped on it for you
- shell-位置参数变量和预定义变量
- How about counting Berry Pie 4? What are the possible ways to play?
- Solr basic operation 5
- Is China Merchants Securities reliable? Is it safe to open a stock account?
- HPE launched ARM CPU general server product
猜你喜欢

Machine learning: the concept and application of VC dimension

333333333333333333333333333333

西门子低代码平台通过Database Connector 连接Mysql 实现增删改查

After working in the software development industry for six years, I changed my ideas in those years

Matplotlib histogram of Matplotlib visualization plt bar()

New titanium cloud service won the "2022 love analysis · panoramic report of it operation and maintenance manufacturers" cloud management platform CMP representative manufacturer

Head on Amway! Good looking and practical motor SolidWorks model material see here

FPGA Development (2) -- IIC communication

雲和恩墨蓋國强,識別它、抓住它,在國產數據庫沸騰以前

Simple understanding of B tree and b+ tree
随机推荐
MySQL functions and constraints
Solr basic operation 5
Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here
Fund information disclosure
Sword finger offer 14- I. cut rope
Matplotlib plt Hist() parameter explanation
Cacti settings for spin polling
Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
打造一个 API 快速开发平台,牛逼!
333333333333333333333333333333
Virtual machine online migration based on openstack
koa2学习和使用
小程序插件接入、开发与注意事项
Embedded development: Hardware in the loop testing
6.29日刷题题解
6.28 problem solving
FPGA Development (1) -- serial port communication
golang7_ TCP programming
Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees
Official website of Greentree