WTL For MFC Programmers, Part I - ATL GUI Classes
WTL For MFC Programmers, Part I - ATL GUI Classes
WTL For MFC Programmers, Part I - ATL GUI Classes
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
! "#
&' ( ' )*
&
+,
. /
01
40 '5 &'5(*
6 %
3
8
$
$
&
!''(
00) %
# 0774 7 77
00 6
077, "( 74
$#
9 "" ( :#
)*+! $
!,
; , , 1<
:. 6 . 2
=
$
5
=
$
3# =
# >
$
#
; 9
#
#
6 !
#
=
#
#
%#
#
# #
5 $ $
!#
.2.
= ' (
= ' )
6#
=@ A
=3
B
9
#
: %
C
9
,-
23*
!"! !## %
00
+-
?;
-* .
$!! =
9 $
# !
< !
< #
!#
$ #
#9
#
$ #
#< $
@ #
$
)"
'
/
#
E $
# #
#
=
>
#
'
!
'
9 $
#
561
$
#
3# !
)
9 $ #%
# %
!
1 12
!
!
#
5
# #
#< =
9
>
#
<
561
#
!'
.?
#
#
#
#
#
)"
*5 # =
=@
#%
&
=!
<9 '
$
E $ # $
<
$
# 9
$ 9 $$
E $ # !
#
#
3# !
561 %
!
$
!
!
9 $ $
# '
%
)"! #$
G=
#
9 $ $
=
9
/
(; ;)
$
077,
%
3# !
561 E $ # @ $
# =5F
#
#
$
'
#
#
#
$ =@ #
#
#
<
#
<$
#
>
#
9
)"!
G !
)7#
! ' ($
) " D* &
#$
#
$
$ >
9
< $ #
!
.?
6
!
'
0 !''#
9 $ # @ <$
=! 9 $ # $
# %
$
9
#
9 $@
# 9 %
#
%
9
' )"$
#
$
#
; 3# "G #
G.# 9
$ ; !; #
<$
# #
$
F
'
$
#
= ' (
< < ?
#
!
= ' )
>
< < ?
#
!
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
/
@
$<
!
'
9
! 9 $ #% @ $ #
9 $ 3# !
$(VCInstallDir)PlatformSDK\include !
E $
#
#
>
E $
#
$<
3=; %
>
E $
>
= #%
'
@ $
><
'
-9
#
9
=
# 9 $$
<
#
#
# #
>
#
@ >
(; ;)
#@ <
=! 9 $ #
>#
3= %
9 $# $ !
@ WPARAM #
LPARAM
# 9 $
#
3
*
9 $$
'
#? 5
H!
>
#
$ #
$
#
<9
9
#
#%
#
H #
#
3 #
!#$
- C
!$ 9
); ;- $
@ ! $
! 9 $ #% # 9
#
$<
1
?
#
#
)
$
#
9 $
#> #
#
561
#> $
#
#< % ($VCInstallDir)include #
#
>
$
# >
G6F /F .6= G
40 3=
$
#
=! 9 $
@ >
#
&
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
>
$ #<
>
!
/
/
/
=
2
=
= # #
=! 9 $@
>
#
#
@
9 $ C
%
!
@ A8= #
A8=
#
%
#
#
! ?<
# ! % # $
1 !
# #!
<#
#%
%
; 9
#
>
# !
#
J#
$
$
#
56/ 9 $
!
< > 9 $ # <$9 #
>
#
# G
!! # 9 $
<9
$!!
9 !!
#
!
% %
9 $
!G
!
#
! #
$
$< # # 9
#
9 $@% <
I$
! #<
#
#
<$
=! 9 $@
>
#
9 $
#
# $# #<
%
>#
# 9 $ >
%
F
/
C#
2 12
<
?
%
! #
# 8= ! # $
>
#
># $
!
> 662 66' #
G$ #
#
8=G !$
$# 9
%
! #$
&
! ?<
*
$
#
? $ #<
#
# # # 9; >
#
E $ # #
9 <$ ! ?
9 $
!#
#!!
?
#
&
#
: 6
! ? # <$
#
#
#%
#
< #>*
=! 9 $
#
#
; ?
# !$ 9 &! #
9
$
#
#
CFrameWnd #
#
#
CSplitterWindow
CDialog ;; #
# @
!!
#
!9
?
<$ $
*
# 9
#
#
#
> =
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
=
=
#
!
#
!
#> A8=
$
9
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
#
# F
#
#!
=@%
# #
# =@
!#
! #$
1
>
$ < !
9 $@% <
9#
5
# < #
=
9
#
!=
=
% #
I$ > 9 ? #
<# >
%
!
9 $ #9
!!
<# D
$
!$ >9
#
%
!#
$
#%
$
%
#
#
A8=
>
< !
# 9
#
9 $
<#
#
#
< # 9 >
!#
#
@ KF ;
< # 9
#
# $ #
#
#
&
# #
%
$
$ #
$
# #9 #
#
<
9 #
<#
! A8= #
A8= #
# ! ?<
$
#
!
#
<
<$ ! ?
# #
%
!;
$ <
'
9@
$
"#
%
/. * 5
I$ %#
!
>
$ <
#@
9
)7 # # #
<
#
#>
#
#
# 9
F
<
$
# 9 #< $
@ CWnd #
CDialog
< <$
!
4#
) &
0*'
4 #
%
4"
%
$ <
<
)"#
! #$
%
!
#
#
%
@
#
)"
#
# 9<
%
<9
!
#
) , = #%
%
B =@ #
#
>
$
; $
# ),9
%
$# 9 *
# 5 $
!
%
),#
&=@
ATL-style templates
.%
!9 $ #
9 $$ # !
#
#>
#
#
# #
?#
# # $# 9
CMyWnd
!
# #
# < #$
#
# < $
#
#
>#
#9
#
>9
!
#
#
;
9 #!
class CMyWnd #
!
#%
#
#
# #
% $# !$
#
#
#
pT->PrintClassName();
}
void PrintClassName() { cout << "This is B1"; }
};
3 12
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
};
int main()
{
D1 d1;
D2 d2;
// prints "This is B1"
// prints "This is D2"
d1.SayHi();
d2.SayHi();
return 0;
}
static_cast<T*>(this)
# J#
#
$# #
< #! #
public B1<D2> 9 $@ <
$<
#
# * #
/
SayHi()
@ % $#
? #
>
@
? #
SayHi()
>
=
% >
<
#
*=@
#! <
#
>#
this
#$
#
#
#$
#
this <
?# 9 >
SayHi() =
B1*
D1*
D2*
#
#
;
9 &=! 9 $
class D3 :
<
! 9
D1*
D2* &#
9
?
#
! 9
#
#
#
B1<D1>
# J#
<
void B1<D1>::SayHi()
{
D1* pT = static_cast<D1*>(this);
pT->PrintClassName();
}
D1
%
#
#>
SayHi()
B1 # # PrintClassName()
@ $
B1<D2> #
# J#
SayHi() ? #
void B1<D2>::SayHi()
{
D2* pT = static_cast<D2*>(this);
pT->PrintClassName();
}
D2
<
=
= #%
=@
!$
# PrintClassName()
#
!
@
#%
#
I$ #
I$
$
9 < #$
<
# # % $# !$
# #
%
#
!#%<
", <#
#
<
!
$
%<
# $
<
9 # <
! #
!
?#
07
#$
!# $
# J
%<
J
&
$
#
9<
<9
*
#%
#
$
> !
1 %
F1
%
$
<# >
#
#@
9
# #
$
%
!
#
#
&
!#
#
<9
#
!#
#
#
!
G
!# G ! #
#
# # <
#
CWindow =
# #
#
# $
# HWND #
%
#
!
#
#
!
8
40 3=
# #> # HWND #
!
# #
$
# SetWindowText() #
DestroyWindow() CWindow # # $<
< m_hWnd # 9 $ # #
!9 $
# HWND
CWindow #
# # operator HWND
9 $ #
# # CWindow <
# !$
# #> #
HWND
I$ %#
CWnd::GetSafeHwnd()
#
CWindow % 9 !!
!
##
<
#
<
$ > CWnd
12
?
#
%
>
#
# 9
HWND
9
CWnd
!
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
9 $
#
$
$
@
#
#%
<
#
% 9
>
; #
"
0
4
#
!
#
#
9 $
CWindowImpl CWindowImpl
#
#
#
# <#
#
!
WindowProc()
< ? CDialogImpl #
$
!
#
#
#
!# #
CAxDialogImpl
9 $
#
!#$
$< #
CWnd
1 %
CWindow <
# 9
!#
# #
# #
#
CAxDialogImpl CDialogImpl $
% 2
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
CWindowImpl E $
% !
!
#
9
$
DECLARE_WND_CLASS
DECLARE_WND_CLASS_EX #
CWndClassInfo #
#
WNDCLASSEX
$
DECLARE_WND_CLASS
!9
#
#
#
$
!#$ %# $ !
<
DECLARE_WND_CLASS_EX
9 $#
!9 # #
9 #
<# >
$
E $ # #
NULL !
#
#
#
# # #
! 9 $
#
$ #
=@ >
!
9 $
$
/ ?
#
#<
#
D
# #
#
!
# #
$
> !
#
#
BEGIN_MSG_MAP #
END_MSG_MAP
#
#
@ #
#
!$
? #
$
=@
$
%
#
#
#
#
#
#
9
< #
!
#
# 9
#
?
#
# #
C
@ # #
#
$
!
9
#
#
# #
@ #%
$
$
< <
#
CWinTraits
typedef CWinTraits<WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
WS_EX_APPWINDOW> CMyWindowTraits;
class CMyWindow : public CWindowImpl<CMyWindow, CWindow, CMyWindowTraits>
{
public:
DECLARE_WND_CLASS(_T("My Window Class"))
BEGIN_MSG_MAP(CMyWindow)
END_MSG_MAP()
};
#
%
#
# #!
CFrameWinTraits
9
CWinTraits
CMyWindowTraits
!
# J#
!
<$
# 9
!
; %
# 9
>
$
12
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
WS_CLIPSIBLINGS,
WS_EX_APPWINDOW | WS_EX_WINDOWEDGE>
CFrameWinTraits;
!
#
#
!
#
!
#
'
#
# # #
# >
#
#
% 9 $
#
#$
#
# #
! WM_NOTIFY
!
WM_CLOSE #
WM_DESTROY
%
;!
#< 9
#
$ # >
>
WM_COMMAND #
$
#
#
#
%
=
#
9
# 9
#%
9
<9
@
$
@
E $@
#
#
#
$
<9
< !
#!
9 $ #
#
<# ; #
@ #
WPARAM #
LPARAM %# $ D 9 $ #%
$ # >
9 $
!
bHandled
# #
TRUE
#
#! $
# #
=! 9 $ #
@
!#$ WindowProc()
#
#
#
bHandled
FALSE
!!
#
9 $ #%
?
9
!#
#
#
# #
#
$
# WM_COMMAND #
#
9 $
#
#
$ # #
< $
=6 IDC_ABOUT
#
#
6 12
COMMAND_HANDLER #
$
# #
$ # >
WM_NOTIFY
# >
#
# #
9 $
NOTIFY_HANDLER
# #
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
14
F
$
#
#
!!
<
CWnd #
#< 9
$
9 <9 #
#
<#
#
# #
># #
?;
#
#
# #
#
# #
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
5
#
CCmdTarget
# #
#
$
%
9 #
#
G
?; G
#
#
$ >
# ; #
#% # PreTranslateMessage()
#
# #
! #$
#
<
$ $# 9 #
!
%
<# >
#
$
#>
%
#
#
> m_hWnd &
HWND
<
<9 #
WM_ERASEBKGND
# #
#
CWindow*
@
#
#
#
#
CPaintBkgnd #
CPaintBkgnd #
#
$ !
# #
# %# $ *
$
$
# #
<# >
#
$
# #
&
#
t_
$
#
$
#
9
9
# #
9#< $
t_crBrushColor / ?
#
#
#
WM_ERASEBKGND
OnEraseBkgnd() #
!
< $
#
!
$
%
# @
OnEraseBkgnd()
GetClientRect()* C
>
#
%
# GetClientRect()
$
@
!
@B
>
#
%
#
T
#
$
pT %# #<
5
OnEraseBkgnd() #
$ # >
%
$
?;
!?
!
# 9
$
!$
%
#
? !
!
!
%
$
@
#
& #
# K
9
#
wParam
5
#
CMyWindow
CMyWindow
#
#
#
#
#
CPaintBkgnd
CHAIN_MSG_MAP #
...
};
7 12
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
9
#
#
#
CHAIN_MSG_MAP
$ <
#
#
#
CPaintBkgnd /
# WM_CLOSE WM_DESTROY #
IDC_ABOUT
<
#
< #$
#
#
#
#
# 9 < #$ CHAIN_MSG_MAP
typedef
#
# #>
# #
D !
CPaintBkgnd<CMyWindow, RGB(0,0,255)> #
#
$
#>
> #
@
#
#
# #
E $
#
<#
$
#
#
#
#
%#< 9 #%
% #
?;
#
#
!!
!
#$
# # 9 #
$ #
#
CWnd;
<#
#
#
#
#
#
#
# #
# CHAIN_MSG_MAP
#
#
#%
#
9
-.-
/
.2.
#
#%
# &
%
#% #
&!
<# %# #<
theApp*
9
# 9 #
# #
9 $ !$ *
#
$
9
# # !
$
#
<# CWinApp %# #<
' (#
' )
# 9<
#
#
=
In VC 6
? $ #<
!"
<
_Module C
// stdafx.h:
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <atlbase.h>
extern CComModule _Module;
#include <atlwin.h>
#
"
33 !
$
#
<#
_Module %# #<
"
"
// main.cpp:
CComModule _Module;
CComModule #
# J#
!$
WinMain()
// main.cpp:
CComModule _Module;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev,
LPSTR szCmdLine, int nCmdShow)
{
_Module.Init(NULL, hInst);
_Module.Term();
}
# #
@
%
# CMyWindow <
Init()
9$
WinMain()
#
#
#
#
F
#
%
$
5
>
$ .2.
#
$
#
#
NULL
#
// main.cpp:
#include "MyWindow.h"
CComModule _Module;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev,
LPSTR szCmdLine, int nCmdShow)
{
_Module.Init(NULL, hInst);
CMyWindow wndMain;
MSG msg;
8 12
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
return 1;
}
wndMain.ShowWindow(nCmdShow);
wndMain.UpdateWindow();
// Run the message loop
while ( GetMessage(&msg, NULL, 0, 0) > 0 )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
_Module.Term();
return msg.wParam;
}
9 $ $ $#
# #
CreateWindow()
8
#< %
# RECT
> $
@
3=
$
CMyWindow <
<
# !#
CWindow::rcDefault
CW_USEDEFAULT !
< 9; # $#
$
!
#< 9
# RECT
#
<# > #
#
CWnd
#
<
! CWindow 8
<
@
#
CWindow <
In VC 7
)
<# > #
# # *
$
;
=@
# <
%
= ' )
Term()
$ ; # #
9 <$
#
#
#
#$
!
'
# # 9
9 $
% #
#
<#
# $#
CComModule
<9 ' )
#
%
#
#
!#
@
$
# 9 F$
!
@ #
#
#!?
#9
# 9&!
#
!
Init() #
> >
// stdafx.h:
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <atlbase.h>
#include <atlwin.h>
WinMain() !$
# # 9 _Module
>
>
// main.cpp:
#include "MyWindow.h"
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev,
LPSTR szCmdLine, int nCmdShow)
{
CMyWindow wndMain;
MSG msg;
// Create & show our main window
if ( NULL == wndMain.Create ( NULL, CWindow::rcDefault,
_T("My First ATL Window") ))
{
// Bad news, window creation failed
return 1;
}
wndMain.ShowWindow(nCmdShow);
wndMain.UpdateWindow();
// Run the message loop
while ( GetMessage(&msg, NULL, 0, 0) > 0 )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
C
9 12
>
>
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
10
$# 9 ?
#
#
"
<#
0 E $
C
#
#>
>
#
!
#
=@ #
#
! #
CDialogImpl
# $<
!#
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
#
<
#
!
@ #
@ $
$#
CDialogImpl !
# D
#
$
! CWindowImpl
IDD #
# #< $
$ #< $
!!
=6 !
#>
#
<$ ;
WM_CLOSE #
WM_INITDIALOG
!
#
#
#
#
!
#
!
> 9< #
%#
$
! $
<$
>
<$
9
<#
#
#
C
#
#
#
=$
12
<
%#
wID # #
IDOK
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
IDCANCEL
<$
#
$
#
#
#
#
#
!
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
>
9 $
# #
<
$
# $#
WM_CREATE #
!
!
#
#
$
$
# DoModal()
#< $
#
@
IDC_ABOUT
#
<# >
#
}
// ...
};
F
#
!!
!9 #
*#
CDialog
#
#
#
#
$
<$
#< %
9 $
9 $ #
$
!9
#
$
#
@ #
=
9 $ #
!
# #
DoModal() =! 9 $
@
! GetActiveWindow() &
<
$ ! #
LoadMenu() # #
#
!
CComModule
GetResourceInstance()
$
HINSTANCE ! #
$
#
$
#
AfxGetResourceHandle()
< #%
$
HINSTANCE !
.2. &
#
CComModule::GetModuleInstance()
!$
> AfxGetInstanceHandle() *
/
#
#
# OnCreate()
!!
<
GetResourceInstance()
$ ! $
#< $
!!
#
$ ; # #
<# _AtlBaseModule <
11 12
' (#
)
$
CAtlBaseModule #
!#$
<
!
3#
< !
0 5
%
=@
=!
#
# <
!
9 $ !
%
?
=!
$
# <
< #
2006/7/6
10:48
WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...
#
#
?
#
9 $
9 $
#
!
#
'
@ $
!
#
#
#
;
>
& '
#
>$
'
>
& '
#
>
<$
<#
#
!
>
$
$
<Add>
;
# #
# #
&
#
'
)
$ = '
>
<9
#
#
= 3#
#
== =
(
;
&
>
%
<#
?;
#
#
(
E $ #
9 $
?
$ =
E $ # #
< < ?
>
#
J#
# #
!
< < ?
#
#
!
=
#
!
9
# # $
#
#
>
< # #
9
#
$
$
# <
# #
% 9
#
! > #
<$
<
!
I$
, 4
@
!
#
&=
<
@
9
>
* <$
#
!
#
#>
# 6$
= # J
# 9 #9 =! 9 $ #
9
# 9
#
>
$<
<
$
#
#
=
#
#$
#%
3
* =! 9 $ $
#
&$
# !9 9 $
<$
9 $
$
#
%
! $<
9#
< #
I$
#
#
#9
<
#
#%# #<
9 $
9 #< $
#
#
8
#
6
5
http://www.codeproject.com/wtl/wtl4mfc1.asp?print=true
00 0774
!
< ", 077, 8 #
/#% #
L 3#
$<
%
== &
A8=
'
)"
9
#
%
$ 9
# !
# #
#<
% # 9
C
#
#
# #
"++, #
# 9 #
# <#
' $
#
C
9 $
#$
!
#
!# ! /
' $ 0777
> @
< # # #
%
# /#
M#<
! #
%
# 9
;! $
#
<$
6
# > M#<
! @ !#
$
#
# #<<
3
C #
& $
$
>
1
:!#
/&;;%%%*
12 12
00 6
077, "( 74
9
9
<<
>
#
19
#% <
1 / <
*
!
#
#9
#
#
6$
<#
&
#
<>
# ** C
<9
#
#
# 59 #
#
#
#
#H
#
$
077,
#
#
#
#
#
#
<
>
!!
#
<#< 9
%
!
8
/
# $#
>
"+++
$
F
# #
# >9 #
# 3 #95 #
@ !
. #
$#
N# #
3
!
#
;% ;% )
:*
'
/
#
%
9
O
#
3
6$
0774
"+++;077(
2006/7/6
10:48