The icon in the upper left corner of the settings dialog box under WIN32 development

Implementation

Load the icon through the LoadIcon function, and then send the loaded icon resource to the WM_SETICON message of its window through SendMessage
(Note: The specially encapsulated function under MFC is SetIcon, which is essentially to send a message)

SendMessage function and LoadIcon function introduction

1. Function prototype


HICON WINAPI LoadIcon(
  _In_opt_  HINSTANCE hInstance,
  _In_      LPCTSTR lpIconName
);

LRESULT WINAPI SendMessage(
  _In_  HWND hWnd,
  _In_  UINT Msg,
  _In_  WPARAM wParam,
  _In_  LPARAM lParam
);

2. Parameter introduction (please refer to MSDN for detailed usage)

hInstance: resource handle

lpIconName: icon name, you need to convert the icon ID through MAKEINTRESOURCE macro

hWnd: window handle

Msg: target message type

wParam: attached to the message information

lParam: attached to the message information

3. Return value

HICON type, handle of newly loaded icon, NULL is returned if loading fails

LRESULT type, the return value specifies the result of the message processing, depending on the message sent

Sample code

Note: The sample code does not create a window by registration, so you need to add the window and icon resources through VS yourself

/*
 *     图标ID:IDI_ICON
 *
 */
#include <windows.h>
#include "resource.h"

HINSTANCE g_hInstance;

void OnInitDialog(HWND hwndDlg)
{
	HICON hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON));
	SendMessage(hwndDlg, WM_SETICON, FALSE, (LPARAM)hIcon);
}

BOOL WINAPI MsgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
		OnInitDialog(hwndDlg);
		break;
	case WM_CLOSE:
		EndDialog(hwndDlg, IDCANCEL);
		break;
	default:
		break;
	}
	return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	g_hInstance = hInstance;
	DialogBox(hInstance, (LPCTSTR)IDD_MAIN_DLG, NULL, MsgProc);
	return 0;
}

Guess you like

Origin www.cnblogs.com/veis/p/12676769.html