当前位置:网站首页>Visual programming - Experiment 2
Visual programming - Experiment 2
2022-04-23 03:22:00 【Tang Encheng_ hhhc】
One 、 Purpose and requirement of the experiment
1、 Use API Function programming contains all kinds of resources Windows Applications ;
2、 Realization Windows Application response to keyboard and mouse .
Two 、 Experimental content :
1、 Problem description :
Create a menu , There are three menu items , Respectively “ file ”、“ Calculation ” and “ help ”, among ,“ file ” The menu item contains “ open ”、“ preservation ”、“ drawing ”、“ sign out ” Other menu options ;“ Calculation ” The menu contains “ The sum of the ”、“ variance ”、“ Root mean square ” Other menu options ;“ help ” The menu item contains “ Calculating the sum helps ”、“ Calculate variance help ” and “ Calculating the root mean square helps ” as well as “ About ” Equivalency .
If you click “ drawing ” A menu item , Then draw the following figure . If you press the left mouse button in the user area , Then dynamically create one including “ Delete calculated sum ”、“ Add calculated average ” and “ Modify the calculated mean square deviation ” Pop up menu of three menu items , This menu controls “ Calculation ” The menu item in the menu is deleted 、 Add and modify .( Request new pop-up menu not available at the beginning , It becomes available after pressing the right key .) single click “ Modify the calculated mean square deviation ” After menu item ,“ Calculation ” Under the menu “ Calculate the mean square error ” Item to “ Linear fitting ”, single click “ Add calculated average ” After menu item ,“ Calculation ” Added... To the menu “ Calculate average ” A menu item .
Set the cursor to a word in your name 、 Set the icon to another word in your name .
2、 Resources used in the application 、 news , Introduce the functions of the main functions ;
3、 Program realization 4、 Operating results
3、 ... and 、 Program code ( all , Include visual editing diagram of resource file )
Four 、 Operation results and Analysis
The main function code is as follows :
#include <windows.h>
#include " Experiment two .h"
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
static int Paint = 0, flag = 0, flags = 0;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HACCEL hAccel;
WNDCLASSEX wcex;
HWND hWnd;
MSG msg;
TCHAR lpszMenuName[] = L"Menu";
TCHAR lpszClassName[] = L" Modal dialog ";
TCHAR lpszTitle[] = L"2020316101099_ Huang Jingjing _ Experiment 2 ";
//------------------------------ The following initialization window classes ------------------------------
wcex.cbSize = sizeof(WNDCLASSEX); // The size of the window class
wcex.style = 0; // The default window type is
wcex.lpfnWndProc = WndProc; // The window processing function is WndProc
wcex.cbClsExtra = 0; // Window class has no extension
wcex.cbWndExtra = 0; // The window instance has no extension
wcex.hInstance = hInstance; // Current instance handle
// wcex.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDR_MAINFRAME)); // The icon of the window is the default icon
wcex.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursorFromFile(L" Customize .cur"); // The window uses the arrow cursor
// wcex.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // The window background is white
wcex.lpszMenuName = lpszMenuName; // There is no menu in the window
wcex.lpszClassName = lpszClassName; // The window class name is “ Window example ”
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDR_MAINFRAME)); // The small icon of the window is the default icon
// wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
//------------------------------ The following window class registration ------------------------------
if (!RegisterClassEx(&wcex)) // Warn if registration fails
{
MessageBox(NULL, L" Window class registration failed !", L" Window registration ", NULL);
return 1;
}
//------------------------------ Create the following window ------------------------------
hWnd = CreateWindow(
lpszClassName, // Window class name
lpszTitle, // The title name of the window instance
WS_OVERLAPPEDWINDOW, // The style of the window
CW_USEDEFAULT, CW_USEDEFAULT, // The coordinates of the lower left corner of the window are the default values
CW_USEDEFAULT, CW_USEDEFAULT, // The height and width of the window are the default values
NULL, // This window has no parent window
NULL, // This window has no main menu
hInstance, // Create the current handle of this window application
NULL // Do not use this value
);
if (!hWnd) // Warn if window creation fails
{
MessageBox(NULL, L" Failed to create window !", L" create a window ", NULL);
return 1;
}
ShowWindow(hWnd, nCmdShow); // Display window
UpdateWindow(hWnd);
hInst = hInstance;
hAccel = LoadAccelerators(hInst, lpszMenuName);
// Draw user area
while (GetMessage(&msg, NULL, 0, 0)) // Message loop
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(int)msg.wParam; // When the program terminates, the information is returned to the system
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HPEN hP;
HBRUSH hB;
HDC hDC; // Define a handle to the device context
TEXTMETRIC tm;
PAINTSTRUCT PtStr; // Defines a structure variable that points to the drawing information
HMENU Menu;
static HMENU hadmenu;
POINT pt;
switch (message)
{
// Left click the mouse
case WM_LBUTTONDOWN:
if (flag == 0)
{
Menu = GetMenu(hwnd);
hadmenu = CreateMenu();
AppendMenu(hadmenu, MF_GRAYED, IDM_DELETE, L" Delete calculated sum ");
AppendMenu(hadmenu, MF_GRAYED, IDM_ADD, L" Add calculated average ");
AppendMenu(hadmenu, MF_GRAYED, IDM_ALTER, L" Modify the calculated mean square deviation ");
InsertMenu(Menu, 3, MF_POPUP | MF_BYPOSITION, (UINT)hadmenu, L" New menu ");
flag = 1;
// EnableMenuItem(Menu, IDM_DELETE);
DrawMenuBar(hwnd);
break;
}
// Right click and press
case WM_RBUTTONDOWN:
EnableMenuItem(hadmenu, IDM_ADD, MF_ENABLED | MF_BYCOMMAND);
EnableMenuItem(hadmenu, IDM_DELETE, MF_ENABLED | MF_BYCOMMAND);
EnableMenuItem(hadmenu, IDM_ALTER, MF_ENABLED | MF_BYCOMMAND);
case WM_COMMAND:
switch (LOWORD(wParam))
{
/*case WM_SETCURSOR: HCURSOR hCursor; hCursor = LoadCursorFromFile(L" Customize .cur"); SetCursor(hCursor);*/
case IDM_OPEN:
MessageBox(hwnd, L" The file is open !", L" file open ", MB_OK);
break;
case IDM_SAVE:
MessageBox(hwnd, L" File saved successfully !", L" file save ", MB_OK);
break;
case IDM_EXIT:
SendMessage(hwnd, WM_DESTROY, 0, 0); break;
case IDM_ABOUT: // Create dialog
DialogBox(hInst, L"About", hwnd, (DLGPROC)DlgProc); break;
case IDM_PAINT:
Paint = 1;
break;
case IDM_DELETE:
Menu = GetMenu(hwnd);
DeleteMenu(Menu, IDM_ZONGHE, MF_BYCOMMAND);
DrawMenuBar(hwnd);
break;
case IDM_ALTER:
Menu = GetMenu(hwnd);
ModifyMenu(Menu, IDM_JUNFANCHA, MF_BYCOMMAND, IDM_XIANXING, L" Linear fitting ");
DrawMenuBar(hwnd);
break;
case IDM_ADD:
Menu = GetMenu(hwnd);
InsertMenu(Menu, IDM_JUNFANCHA, MF_BYCOMMAND | MF_ENABLED, IDM_XIANXING, L" Calculate average ");
// AppendMenu(Menu, MF_ENABLED, IDM_PINJUNZHI, L" Calculate average ");
DrawMenuBar(hwnd);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
// Handle redraw messages
case WM_PAINT:
if (Paint == 1)
{
hDC = BeginPaint(hwnd, &PtStr); // Start painting
SetMapMode(hDC, MM_TEXT);
hB = CreateHatchBrush(HS_CROSS, RGB(255, 0, 0));
SelectObject(hDC, hB);
RoundRect(hDC, 180, 10, 260, 70, 10, 10);
Pie(hDC, 180, 70, 260, 160, 210, 70, 230, 70);
Rectangle(hDC, 180, 160, 260, 220);
hP = CreatePen(PS_DASHDOT, 1, RGB(0, 255, 0));
SelectObject(hDC, hP);
MoveToEx(hDC, 140, 115, NULL);
LineTo(hDC, 300, 115);
MoveToEx(hDC, 220, 0, NULL);
LineTo(hDC, 220, 240);
hB = (HBRUSH)GetStockObject(DC_BRUSH);
SetDCBrushColor(hDC, RGB(225, 0, 0));
SelectObject(hDC, hB);
Rectangle(hDC, 320, 100, 400, 180);
EndPaint(hwnd, &PtStr);
}
return 0;
default: return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
BOOL CALLBACK DlgProc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
EndDialog(hdlg, 0);
break;
}
break;
case WM_CLOSE:
EndDialog(hdlg, 0);
break;
}
return 0;
}
Resource function code
#include <windows.h>
#include " Experiment two .h"
Menu MENU
{
POPUP " file "
{
MENUITEM " open ", IDM_OPEN
MENUITEM SEPARATOR
MENUITEM " preservation ", IDM_SAVE
MENUITEM SEPARATOR
MENUITEM " drawing ", IDM_PAINT
MENUITEM SEPARATOR
MENUITEM " sign out ", IDM_EXIT
}
POPUP " Calculation "
{
MENUITEM " The sum of the ", IDM_ZONGHE
MENUITEM SEPARATOR
MENUITEM " variance ", IDM_FANCHA
MENUITEM SEPARATOR
MENUITEM " Root mean square ", IDM_JUNFANCHA
}
POPUP " help "
{
MENUITEM " Calculating the sum helps ", IDM_ZONGHEHELP
MENUITEM SEPARATOR
MENUITEM " Calculate variance help ", IDM_FANCHAHELP
MENUITEM SEPARATOR
MENUITEM " Calculating the root mean square helps ", IDM_JUNFANCHAHELP
MENUITEM SEPARATOR
MENUITEM " About ", IDM_ABOUT
}
}
Menu ACCELERATORS
{
"^O", IDM_OPEN
"^S", IDM_SAVE
}
IDR_MAINFRAME ICON "my Icon .ico"
Header file code
#pragma once
#define IDM_OPEN 10
#define IDM_SAVE 12
#define IDM_PAINT 15
#define IDM_EXIT 17
#define IDM_ZONGHE 19
#define IDM_FANCHA 21
#define IDM_JUNFANCHA 23
#define IDM_ZONGHEHELP 25
#define IDM_FANCHAHELP 27
#define IDM_JUNFANCHAHELP 28
#define IDM_ABOUT 20
#define IDR_MAINFRAME 128
#define IDM_DELETE 30
#define IDM_ADD 34
#define IDM_ALTER 36
#define IDM_PINJUNZHI 38
#define IDM_XIANXING 40
The difficulty of this experiment mainly lies in the mouse cursor , Addition and modification of small icon files , In the next section, I will mainly explain this problem . In addition, adding menu resource files is also a difficult problem .
版权声明
本文为[Tang Encheng_ hhhc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220621319587.html
边栏推荐
- “如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
- Oracle query foreign keys contain comma separated data
- Knowledge of software testing~
- Establishing and traversing binary tree
- Téléchargement en vrac de fichiers - téléchargement après compression
- Find the number of leaf nodes of binary tree
- [MySQL] left Function | Right Function
- Web Course Design - his system
- QT learning summary
- Scenario Title: how does system a use the page of system B
猜你喜欢
为什么BI对企业这么重要?
一文了解全面静态代码分析
Eight elder brothers chronicle [4]
File upload vulnerability summary and upload labs shooting range documentary
关于idea调试模式下启动特别慢的优化
Use of ADB command [1]
Supersocket is Used in net5 - command
QT dynamic translation of Chinese and English languages
MySQL之explain关键字详解
Flink customizes the application of sink side sinkfunction
随机推荐
Flink real-time data warehouse project - Design and implementation of DWS layer
Mysql database design specification
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
[Mysql] LEFT函数 | RIGHT函数
The most easy to understand service container and scope of dependency injection
2022 P cylinder filling training test questions and simulation test
Ide-idea-problem
Knowledge of software testing~
Do you really understand hashcode and equals???
xutils3修改了我提报的一个bug,开心
Téléchargement en vrac de fichiers - téléchargement après compression
JS implementation of new
General testing technology [1] classification of testing
. net 5 Web custom middleware implementation returns the default picture
IDEA查看历史记录【文件历史和项目历史】
可以接收多种数据类型参数——可变参数
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
Configuration table and page information automatically generate curd operation page
Cefsharp stores cookies and reads cookies
The query type of MySQL is very inefficient.