xiaobin_hlj80

https://blog.csdn.net/xiaobin_HLJ80 http://blog.chinaunix.net/uid/31552151.html

导航

Icon - Normal Window

  • The handle needs to be obtained in the constructor;
  • the icon is set in the subsequent call.
For example, in InitUpdate() or InitDialog().

handle

  • Declare
protected:
    HICON m_hIcon;
  • Get handle
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);

set icon

SetIcon(m_hIcon, TRUE);         // Set big icon
SetIcon(m_hIcon, FALSE);        // Set small icon

example

使用之前的hello mfc举例子。

  • MainWindow.h
#pragma once
#include <afxwin.h>

class CMainWindow :
	public CFrameWnd
{
public:
	CMainWindow();
	~CMainWindow();

private:
	CButton m_btnAbout;

public:
	afx_msg void OnBtnClick();
	HICON m_hIcon;

	DECLARE_MESSAGE_MAP()
	afx_msg void OnHelpAboutus();
};

  • MainWindow.cpp
#include "MainWindow.h"
#include "AboutDlg.h"

#define ID_ABOUT 1101

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
	ON_COMMAND(ID_ABOUT, OnBtnClick)
	ON_COMMAND(ID_HELP_ABOUTUS, &CMainWindow::OnHelpAboutus)
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
	Create(NULL, _T("Hello MFC"),
		WS_OVERLAPPEDWINDOW,
		rectDefault,
		NULL,
		MAKEINTRESOURCE(IDR_MENU1)
	);

	CRect rt;
	rt.top = 10;
	rt.left = 150;
	rt.right = 250;
	rt.bottom = 30;

	m_btnAbout.Create(_T("About"),
		WS_CHILD | WS_VISIBLE,
		rt,
		this,
		ID_ABOUT);

	LoadAccelTable(MAKEINTRESOURCE(IDR_ACCELERATOR1));
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
	SetIcon(m_hIcon, TRUE);         // Set big icon
	SetIcon(m_hIcon, FALSE);        // Set small icon
}

afx_msg void CMainWindow::OnBtnClick()
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

CMainWindow::~CMainWindow()
{
}

void CMainWindow::OnHelpAboutus()
{
	OnBtnClick();
}

Ref

posted on 2026-09-21 05:16  xiaobin80  阅读(9)  评论(0)    收藏  举报