当前位置:网站首页>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
边栏推荐
- How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
- Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
- Super easy to use asynchronous export function of Excel
- QT dynamic translation of Chinese and English languages
- ThreadLocal test multithreaded variable instance
- [vs Code] solve the problem that the jupyter file displays exceptions in vs code
- socket编程 send()与 recv()函数详解
- Query stored procedures in PostgreSQL
- The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
- js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
猜你喜欢

Quartz. Www. 18fu Used in net core

Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800

QT learning summary

全新的ORM框架——BeetlSQL介绍

. net 5 Web custom middleware implementation returns the default picture

File upload vulnerability summary and upload labs shooting range documentary

Node configuration environment CMD does not take effect

A comprehensive understanding of static code analysis

2022g2 boiler stoker examination question bank and online simulation examination

Xamarin effect Chapter 22 recording effect
随机推荐
Ide-idea-problem
Generate QR code through zxing
Detailed explanation of socket programming send() and recv() functions
Build websocket server in. Net5 webapi
Huawei mobile ADB devices connection device is empty
2022t elevator repair test simulation 100 questions and online simulation test
POI create and export Excel based on data
Blazor University (12) - component lifecycle
JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
批量下載文件----壓縮後再下載
集合之List接口
[untitled]
Using swagger in. Net5
yes. Net future
Advanced sorting - fast sorting
MySQL keyword group_ Concat, combined connection query
A comprehensive understanding of static code analysis
Experiment 6 input / output stream
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
Establishing and traversing binary tree