Object-oriented programming experiment three basic usage of sduwh sub-windows and controls, use of resources refer to experiment report 1

Collected from the Internet, for reference only

Experiment 3 collected two complete reports, this is one, and the other is in the next article of this column.

Experimental topic

"Object-Oriented Programming" Experiment 3

Experiment topic: Basic usage of sub-windows and controls, resource usage

Overall purpose: understand, message transfer between windows, call stack; master how to make and use cursors, icons, and menus; master how to respond to menus, how to load and use shortcut menus; understand GetWindowLong/SetWindowLong, GetClassLong/SetClassLong Function usage.

Experimental content process:

Improve the ChildWin sample program to display window-specific information (window ID number) in the sub-window; try to draw a rectangle of a specific color in each sub-window

    • Add processing code in the refresh processing message of the window procedure function of the child window;
    • Use the GetWindowLong function to get the child window ID;
    • Set the information to be displayed in the sub-window according to the sub-window ID (use the wsprintf function to organize the string to be displayed)
    • Try to draw a rectangle of a specific color in the child window (using CreateSolidBrush, Rectangle function)
  1. Track the messaging process
    • Set breakpoints at WM_CREATE, WM_PAINT, WM_COMMAND, WM_LBUTTONDOWN of Childwin's main window process function
    • Set breakpoints at WM_CREATE, WM_PAINT, WM_CHILDWIN, WM_LBUTTONDOWN of the child window
    • Run the program and view the response process of each breakpoint to understand the order of message delivery
  2. Design and implement a soft keyboard

Use sub-windows to implement a soft keyboard (draw a simulated keyboard with sub-windows in the client area of ​​the window), and click each sub-window with the mouse to input different characters. Displays the entered characters in the window.

  1. Create a new project in your own working directory for control testing (you can copy easywin.c of the Easywin project to the directory of the new project, modify the file name to ctltest.cpp (change the suffix to .cpp), compile, link and Make the program work (including fixing compilation errors)].
  2. * Use Button to complete the soft keyboard required by 4*. Compare the differences with the original implementation.
  3. Create various types of control sub-windows, including static, edit, listbox, combobox, button, scrollbar, etc.; add corresponding message processing in the window procedure function of the main window to realize the communication between the main window and the sub-window (try to set the control's Title/content, get the title/content of the control and give a prompt in the main window when the control has operations such as clicks).
  4. Same as step 5, create a new project for resource testing.
  5. Added new resources: cursor, icon, and displayed correctly
    • Use the resource editor to edit a cursor (color), and be careful to delete unnecessary image types in the resource , otherwise the cursor may not be displayed correctly.
    • Set a hot spot for the cursor (hot spot)
    • Edit an icon with the resource editor
    • Modify the code of the registered window class so that the program can use the custom cursor and icon normally
    • Use the DrawIcon function to add processing to the WM_PAINT message, and display the custom cursor and icon in the client area of ​​the window.
    • Create an additional cursor for use in subsequent experimental steps
  6. add menu
    • Create a menu item command in the menu to modify the cursor used by the window, which can be named "ChangeCursor", and the corresponding ID is assumed to be ID_CHANGECURSOR
    • Modify the code of the registered window class so that the menu can be loaded correctly when the program is running
    • In another way, by modifying the code of creating the window ( CreateWindow ) , the menu can be loaded correctly
  7. Add menu message response code
    • Increase WM_COMMAND message processing
    • Respond to menu commands by referring to the processing method in the menu sample code in Chapter 5
    • The response to the ID_CHANGECURSOR command is: to change the cursor used by the window, you can use SetClassLong (hwnd, GCL_HCURSOR,
                                 (LONG)LoadCursor((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                                                             MAKEINTRESOURCE(IDC_CURSOR2) ) );
  8. Handle the right mouse button, add shortcut menu processing
    • The menu command issued by the right button is also responded by WM_COMMAND message. After adding the shortcut menu, the program should be able to issue the same command through the main menu or the shortcut menu.
  9. Add the compilation and linking of the project completed in this experiment to the batch file used for program construction, and use the batch file to automatically build all the projects in Experiment 1, 2, and 3 at one time.
  10. The experimental report submission time for this experiment is before 21:30 on October 22.

Notice:

  1. 5* Optional.
  2. Make a backup of each experiment, and carry all the experimental results (electronic versions) of previous experiments for future reference in each experimental class.
  3. The format and file name of the experiment report should meet the requirements.

Experiment Report 1 ( For another report, see the next article in this column)

  1. Experimental requirements

Understand, message transmission between windows, and call stack; master the making and use of cursors, icons, and menus; master the response of menus, the loading and use of shortcut menus; understand the usage of GetWindowLong/SetWindowLong, GetClassLong/SetClassLong functions .

2. Experimental process and results

2.1 Create a project

2.2 Program flow and data model analysis

  1. Improve the ChildWin sample program to display window-specific information (window ID number) in the sub-window; try to draw a rectangle of a specific color in each sub-window
    • Add processing code in the refresh processing message of the window procedure function of the child window;
    • Use the GetWindowLong function to get the child window ID;
    • Set the information to be displayed in the sub-window according to the sub-window ID (use the wsprintf function to organize the string to be displayed)
    • Try to draw a rectangle of a specific color in the child window (using CreateSolidBrush, Rectangle function)

     

2. Track the messaging process

WM_CREATE, WM_PAINT, WM_COMMAND,

    • Set a breakpoint at WM_LBUTTONDOWN
    • Set breakpoints at WM_CREATE, WM_PAINT, WM_CHILDWIN, WM_LBUTTONDOWN of the child window
    • Run the program and view the response process of each breakpoint to understand the order of message delivery

3. Design and implement a soft keyboard

Use sub-windows to implement a soft keyboard (draw a simulated keyboard with sub-windows in the client area of ​​the window), and click each sub-window with the mouse to input different characters. Displays the entered characters in the window.

4. Create a new project in your own working directory for control testing (you can copy easywin.c of the Easywin project to the directory of the new project, modify the file name to ctltest.cpp (change the suffix to .cpp), compile, link and make the program work (including fixing compilation errors)]

 

  • * Use Button to complete the soft keyboard required by 4*. Compare the differences with the original implementation.
  • Create various types of control sub-windows, including static, edit, listbox, combobox, button, scrollbar, etc.; add corresponding message processing in the window procedure function of the main window to realize the communication between the main window and the sub-window (try to set the control's Title/content, get the title/content of the control and give a prompt in the main window when the control has operations such as clicks).

 

  • Same as step 4, create a new project for resource testing.
  • Added new resources: cursor, icon, and displayed correctly
    • Use the resource editor to edit a cursor (color), and be careful to delete unnecessary image types in the resource , otherwise the cursor may not be displayed correctly.
    • Set a hot spot for the cursor (hot spot)
    • Edit an icon with the resource editor
    • Modify the code of the registered window class so that the program can use the custom cursor and icon normally
    • Use the DrawIcon function to add processing to the WM_PAINT message, and display the custom cursor and icon in the client area of ​​the window.
    • Create an additional cursor for use in subsequent experimental steps

 

 

 

 

 

9. Add menu

  • add menu
    • Create a menu item command in the menu to modify the cursor used by the window, which can be named "ChangeCursor", and the corresponding ID is assumed to be ID_CHANGECURSOR
    • Modify the code of the registered window class so that the menu can be loaded correctly when the program is running
    • In another way, by modifying the code of creating the window ( CreateWindow ) , the menu can be loaded correctly

 

  • Add menu message response code
    • Increase WM_COMMAND message processing
    • Respond to menu commands by referring to the processing method in the menu sample code in Chapter 5
    • The response to the ID_CHANGECURSOR command is: to change the cursor used by the window, you can use SetClassLong (hwnd, GCL_HCURSOR,
                                 (LONG)LoadCursor((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                                                             MAKEINTRESOURCE(IDC_CURSOR2) ) );

 

  1. Handle the right mouse button, add shortcut menu processing

The menu command issued by the right button is also responded by WM_COMMAND message. After adding the shortcut menu, the program should be able to issue the same command through the main menu or the shortcut menu.

 

  • Add the compilation and linking of the project completed in this experiment to the batch file used for program construction, and use the batch file to automatically build all the projects in Experiment 1, 2, and 3 at one time.

2.3 Problems and solutions

  1. The cursor set in the main window cannot be displayed in the sub-window. In order to set my own cursor in the sub-window, I use global variables to write the cursor program of the sub-window;
  2. Since I used the if conditional statement in the paint, the first key of this soft keyboard cannot be used, and it cannot be refreshed after being pressed, so all the letters are moved back one bit (you can also set the global variable instead of the if statement) Just draw the judgment);
  3. After the shortcut is written, it cannot run. After checking, it is g_hSubMenu = GetSubMenu(g_hMenu, 0); The statement is written after the return statement, which makes it unable to run normally, but the system cannot report an error.

3. Experiment summary

In this experiment, I not only practiced the window knowledge and menu knowledge taught by the teacher in class, but also mastered the ability to draw cursors and icons, mastered the creation and use of menus, response methods, and the loading of shortcut menus. And how to use it, I also learned about many extracurricular functions through Internet search and other methods, such as: GetCursorPos(), TrackPopupMenu(), etc. Not only exercised their independent thinking ability but also broadened their horizons.

附录 关键代码
软键盘绘制代码:
case WM_PAINT:
		GetClientRect(hWnd, &rect);
		hdc = BeginPaint(hWnd, &ps);
		if (!(LOWORD(wParam) == nChildWin_ID))
		{
			string[n] = number + 65;
			n++;
			DrawText(hdc, string, n, &rect, DT_SINGLELINE);
			
			
		}	
		DrawIcon(hdc,1,1, LoadIcon(hin, MAKEINTRESOURCE(IDI_ICON1)));
		EndPaint(hWnd, &ps);
		return 0;
子窗口命中写入代码:
case WM_COMMAND:                
		if (!(LOWORD(wParam) == nChildWin_ID))
		{
			SendMessage((HWND)hChildWnd[nCol][nRow], WM_CHILDWIN, 0, 0L);

		
			nChildWin_ID = LOWORD(wParam);
			nCol = nChildWin_ID >> 4;
			nRow = (nChildWin_ID > 15) ? (nChildWin_ID - 4) : nChildWin_ID;
		}
		else
		{
			switch (wParam)
			{
			case ID_CHANGECURSOR:
				SetClassLong(hWnd, GCL_HCURSOR,
					(LONG)LoadCursor((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
						MAKEINTRESOURCE(IDC_CURSOR1)));
				break;
			
			}
		}
		InvalidateRect(hWnd, NULL, TRUE);
		return 0;
主窗口绘制菜单,光标,图形代码:
 wcMain.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	wcMain.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE (IDC_CURSOR2));
	wcMain.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wcMain.lpszMenuName =  MAKEINTRESOURCE(IDR_MENU3);





改变光标菜单代码:
case WM_COMMAND:                 
			
		if (!(LOWORD(wParam) == nChildWin_ID))
		{
			SendMessage((HWND)hChildWnd[nCol][nRow], WM_CHILDWIN, 0, 0L);

			
			nChildWin_ID = LOWORD(wParam);
			nCol = nChildWin_ID >> 4;
			nRow = (nChildWin_ID > 15) ? (nChildWin_ID - 4) : nChildWin_ID;
		}
		else
		{
			switch (wParam)
			{
			case ID_CHANGECURSOR:
				SetClassLong(hWnd, GCL_HCURSOR,
					(LONG)LoadCursor((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
						MAKEINTRESOURCE(IDC_CURSOR1)));
				break;
			
			}
		}
		InvalidateRect(hWnd, NULL, TRUE);
		return 0;
右键快捷菜单代码:
	case WM_RBUTTONDOWN:
			GetCursorPos(&stPos);
			TrackPopupMenu(g_hSubMenu,TPM_LEFTALIGN,stPos.x,stPos.y,NULL,hWnd,NULL);
			break;

Guess you like

Origin blog.csdn.net/qq_22471349/article/details/131013379