DAY04 资源操作

  1.  图标资源
  2. 菜单资源
  3. 对话框资源

 

 

 

#include <Windows.h>
#include <CommCtrl.h>
#include <tchar.h>

#include "resource1.h"

//添加了 CALLBACK 调用约定
LRESULT CALLBACK  Wndproc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
);


//MAKEINTRESOURCEW 宏 (winuser.h)
//将整数值转换为与资源管理功能兼容的资源类型。 此宏用于代替包含资源名称的字符串。

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance,LPSTR lpCmdLine,int nCmdShow )
{
	WNDCLASSW mainWindowClass = {0};
	mainWindowClass.lpszClassName = L"资源操作";
	mainWindowClass.lpfnWndProc = Wndproc;
	//mainWindowClass.lpszMenuName = MAKEINTRESOURCEW(IDR_MENU1);
//HMENU hMenu=LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MENU1));

	mainWindowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1 );
	

	if(!RegisterClassW(&mainWindowClass)){
		MessageBoxW(NULL,L"注册失败",L"错误",MB_ICONERROR);
		return 1;

	}

	HWND hwindow = CreateWindowW(
		mainWindowClass.lpszClassName,
		L"资源操作",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		0,
		CW_USEDEFAULT,
		0,
		NULL,
		NULL,
		hInstance,
		0
	);

	if (hwindow) {
		ShowWindow(hwindow, SW_SHOWNORMAL);
		UpdateWindow(hwindow);
		MSG msg = { 0 };
		while (GetMessageW(&msg, 0, 0, 0))
		{
			TranslateMessage(&msg);
			DispatchMessageW(&msg);
		}
	}


	return 0;
}


//添加了 CALLBACK 调用约定
LRESULT CALLBACK  Wndproc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
) {

	HINSTANCE hInstance = GetModuleHandle(0);
	switch (uMsg)
	{
	case WM_CREATE:
	{
		//CreateWindowW(WC_BUTTON, L"获取文本框内容", WS_VISIBLE | WS_CHILD, 10, 50, 120, 30, hwnd, (HMENU)0x1002, hInstance, 0);
		CreateWindowW(WC_BUTTON, L"设置菜单", WS_CHILD | WS_VISIBLE, 10, 10, 120, 30, hwnd, (HMENU)0x1001, hInstance, 0);
		break;
	}
	case WM_RBUTTONDOWN: {

		//WORD xPos = LOWORD(lParam);   // 鼠标X坐标(相对于窗口客户区)
		//WORD yPos = HIWORD(lParam);   // 鼠标Y坐标(相对于窗口客户区)
		///*wchar_t debugMsg[100];
		//swprintf(debugMsg, 100, L"xPos: %d, yPos: %d\n", xPos, yPos);
		//OutputDebugString(debugMsg);*/
		//POINT point{ 0 };
		//point.x = xPos;
		//point.y = yPos;
		//ClientToScreen(hwnd, &point);
		POINT point{ 0 };
		GetCursorPos(&point);

		HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MENU1));
		HMENU subMenu = GetSubMenu(hMenu, 0);
		TrackPopupMenu(subMenu, TPM_LEFTALIGN, point.x, point.y, 0, hwnd, NULL);

		break;
	}
	case WM_DESTROY:
		PostQuitMessage(0);  // 必须要有,否则无法退出程序 
		break;
	
	case WM_COMMAND: {
		WORD ControlId = LOWORD(wParam);
		switch (ControlId)
		{
		case 0x1001: {

			HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MENU1));
			SetMenu(hwnd, hMenu);


			break;
		}
		case 40001:
		{
			MessageBoxW(hwnd, L"右击菜单的命令", L"提示", MB_OK);
			break;
		}
		default:
			break;
		}
		break;
	}
	default:
		break;
	}

	return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

  

  1 #include <Windows.h>
  2 #include <CommCtrl.h>
  3 #include "resource.h"
  4 
  5 /**
  6 对话框
  7 
  8 HWND CreateDialogW(
  9     HINSTANCE hInstance,    // 应用程序实例句柄
 10     LPCWSTR   lpTemplate,   // 对话框模板
 11     HWND      hWndParent,   // 父窗口句柄
 12     DLGPROC   lpDialogFunc  // 对话框过程函数
 13 );
 14 */
 15 INT_PTR CALLBACK  Dlgproc(
 16     HWND hwndDlg,
 17     UINT uMsg,
 18     WPARAM wParam,
 19     LPARAM lParam
 20 )
 21 {
 22     switch (uMsg)
 23     {
 24     case WM_INITDIALOG: 
 25     {
 26 
 27         MessageBoxW(hwndDlg, L"对话框创建成功", L"提示", MB_OK);
 28         break;
 29     }
 30     case WM_CLOSE:
 31     {
 32         int   result =MessageBoxW(hwndDlg, L"是否要关闭窗口", L"提示", MB_OKCANCEL);
 33         if (result == IDOK) {
 34             DestroyWindow(hwndDlg);
 35             PostQuitMessage(0);
 36         }
 37         
 38 
 39         break;
 40     }
 41     default:
 42         return FALSE;
 43     }
 44 
 45     return TRUE;
 46     
 47 }
 48 
 49 
 50 INT_PTR CALLBACK  Dlgproc2(
 51     HWND hwndDlg,
 52     UINT uMsg,
 53     WPARAM wParam,
 54     LPARAM lParam
 55 )
 56 {
 57     switch (uMsg)
 58     {
 59     case WM_INITDIALOG:
 60     {
 61 
 62         MessageBoxW(hwndDlg, L"对话框创建成功", L"提示", MB_OK);
 63         break;
 64     }
 65     case WM_CLOSE:
 66     {
 67         EndDialog(hwndDlg,0);
 68 
 69         break;
 70     }
 71     default:
 72         return FALSE;
 73     }
 74 
 75     return TRUE;
 76 
 77 }
 78 
 79 
 80 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) 
 81 {
 82 
 83     // 非模态对话框    
 84     /*HWND hDlg  = CreateDialog(hInst,MAKEINTRESOURCEW(IDD_DIALOG1), NULL, Dlgproc);
 85 
 86 
 87     if (hDlg == NULL)
 88     {
 89         MessageBox(NULL, L"创建对话框失败!", L"错误", MB_OK | MB_ICONERROR);
 90         return -1;
 91     }
 92 
 93     ShowWindow(hDlg, cmdshow);
 94     UpdateWindow(hDlg);
 95 
 96     MSG msg = {0};
 97     while (GetMessageW(&msg, 0, 0, 0))
 98     {
 99         TranslateMessage(&msg);
100         DispatchMessageW(&msg);
101     }
102 */
103 
104     // 模态对话框
105     DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_DIALOG1), NULL, Dlgproc2);
106 
107     return 0;
108 }

 

posted @ 2025-12-01 10:38  巨兽~墨菲特  阅读(7)  评论(0)    收藏  举报