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();
}
浙公网安备 33010602011771号