Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
229 views

WTL For MFC Programmers, Part I - ATL GUI Classes

This document provides an overview of using ATL GUI classes for MFC programmers. It discusses how WTL builds on ATL to provide window classes and a message map model similar to MFC. The key points covered include: - WTL uses ATL-style class templates to provide window classes like CWindowImpl that are derived from CWindow and handle the window procedure - Features like DECLARE_WND_CLASS and BEGIN/END_MSG_MAP provide a message map similar to MFC for mapping window messages to member functions - Traits classes like CWinTraits can be used to configure window styles and attributes for WTL window classes - The message map supports handlers for common messages

Uploaded by

GeoMoruzea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views

WTL For MFC Programmers, Part I - ATL GUI Classes

This document provides an overview of using ATL GUI classes for MFC programmers. It discusses how WTL builds on ATL to provide window classes and a message map model similar to MFC. The key points covered include: - WTL uses ATL-style class templates to provide window classes like CWindowImpl that are derived from CWindow and handle the window procedure - Features like DECLARE_WND_CLASS and BEGIN/END_MSG_MAP provide a message map similar to MFC for mapping window messages to member functions - Traits classes like CWinTraits can be used to configure window styles and attributes for WTL window classes - The message map supports handlers for common messages

Uploaded by

GeoMoruzea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

WTL for MFC Programmers, Part I - ATL GUI Classes - The Co...

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 $

<#

ATL and WTL history


%
% .
% 2
% 2;
F
$ # 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 $$ # !

#
#>

#
#

# #

?#

class CMyWnd : public CWindowImpl<CMyWnd>


{
...
};

# # $# 9
CMyWnd
!
# #

# < #$
#
# < $
#
#

>#

#9
#

>9
!

#
#
;

9 #!
class CMyWnd #
!
#%
#
#
# #
% $# !$
#

#
#

template <class T>


class B1
{
public:
void SayHi()
{
T* pT = static_cast<T*>(this);

// HUH?? I'll explain this below

pT->PrintClassName();
}
void PrintClassName() { cout << "This is B1"; }
};

class D1 : public B1<D1>


{
// No overridden functions at all
};
class D2 : public B1<D2>
{
void PrintClassName() { cout << "This is D2"; }

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

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

@ CWnd CWindow <


I$ %#
<
#
# CWindow <
$

?
#

%
>

#
# 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 $
$

class CMyWindow : public CWindowImpl<CMyWindow>


{
public:
DECLARE_WND_CLASS(_T("My Window Class"))
};

/ ?

#
#<

#
D
# #

#
!

# #
$
> !
#
#
BEGIN_MSG_MAP #
END_MSG_MAP

#
#
@ #

#
!$

? #
$

class CMyWindow : public CWindowImpl<CMyWindow>


{
public:
DECLARE_WND_CLASS(_T("My Window Class"))
BEGIN_MSG_MAP(CMyWindow)
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
>
$

typedef CWinTraits<WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN |

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;

Filling in the message map


#
%
# ;
#
#

!
#

#
!
#
!

#
'
#

# # #
# >
#
#
% 9 $
#
#$
#
# #
! WM_NOTIFY
!
WM_CLOSE #
WM_DESTROY

%
;!
#< 9
#
$ # >
>
WM_COMMAND #
$

#
#

#
%
=

#
9

# 9
#%
9
<9
@

$
@

class CMyWindow : public CWindowImpl<CMyWindow, CWindow, CFrameWinTraits>


{
public:
DECLARE_WND_CLASS(_T("My Window Class"))
BEGIN_MSG_MAP(CMyWindow)
MESSAGE_HANDLER(WM_CLOSE, OnClose)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
END_MSG_MAP()
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
DestroyWindow();
return 0;
}
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PostQuitMessage(0);
return 0;
}
};

E $@
#
#
#
$
<9
< !
#!
9 $ #
#
<# ; #
@ #

WPARAM #
LPARAM %# $ D 9 $ #%
$ # >
9 $
!
bHandled
# #
TRUE
#
#! $
# #
=! 9 $ #
@
!#$ WindowProc()
#
#
#
bHandled
FALSE
!!
#
9 $ #%
?
9
!#
#
#

# #
#
$

# WM_COMMAND #

#
9 $
#
#

$ # #

< $

=6 IDC_ABOUT

class CMyWindow : public CWindowImpl<CMyWindow, CWindow, CFrameWinTraits>


{
public:
DECLARE_WND_CLASS(_T("My Window Class"))
BEGIN_MSG_MAP(CMyWindow)
MESSAGE_HANDLER(WM_CLOSE, OnClose)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
COMMAND_ID_HANDLER(IDC_ABOUT, OnAbout)
END_MSG_MAP()
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
DestroyWindow();
return 0;
}
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PostQuitMessage(0);
return 0;
}
LRESULT OnAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
MessageBox ( _T("Sample ATL window"), _T("About MyWindow") );
return 0;
}
};

#
#

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*

template <class T, COLORREF t_crBrushColor>


class CPaintBkgnd
{
public:
CPaintBkgnd() { m_hbrBkgnd = CreateSolidBrush(t_crBrushColor); }
~CPaintBkgnd() { DeleteObject ( m_hbrBkgnd ); }
BEGIN_MSG_MAP(CPaintBkgnd)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
END_MSG_MAP()
LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
T*
pT = static_cast<T*>(this);
HDC dc = (HDC) wParam;
RECT rcClient;
pT->GetClientRect ( &rcClient );
FillRect ( dc, &rcClient, m_hbrBkgnd );
return 1;
// we painted the background
}
protected:
HBRUSH m_hbrBkgnd;
};

@
#

#
#

#
CPaintBkgnd #
CPaintBkgnd #
#
$ !
# #
# %# $ *

$
$
# #

<# >

#
$

# #
&

#
t_

$
#
$
#
9
9
# #
9#< $
t_crBrushColor / ?
#
#
#
WM_ERASEBKGND
OnEraseBkgnd() #
!
< $
#
!
$
%
# @
OnEraseBkgnd()
GetClientRect()* C
>
#
%
# GetClientRect()
$
@
!
@B
>
#
%
#
T
#
$
pT %# #<
5
OnEraseBkgnd() #
$ # >
%
$

?;

!?

!
# 9
$
!$
%
#
? !

!
!

%
$

@
#
& #
# K

9
#

wParam

class CMyWindow : public CWindowImpl<CMyWindow, CWindow, CFrameWinTraits>,


public CPaintBkgnd<CMyWindow, RGB(0,0,255)>

5
#

CMyWindow

CMyWindow
#
#

#
#

#
CPaintBkgnd
CHAIN_MSG_MAP #

class CMyWindow : public CWindowImpl<CMyWindow, CWindow, CFrameWinTraits>,


public CPaintBkgnd<CMyWindow, RGB(0,0,255)>
{
...
typedef CPaintBkgnd<CMyWindow, RGB(0,0,255)> CPaintBkgndBase;
BEGIN_MSG_MAP(CMyWindow)
MESSAGE_HANDLER(WM_CLOSE, OnClose)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
COMMAND_HANDLER(IDC_ABOUT, OnAbout)
CHAIN_MSG_MAP(CPaintBkgndBase)
END_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
? $ #<
!"

<# CComModule %# #<

<

_Module C

// stdafx.h:
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <atlbase.h>
extern CComModule _Module;
#include <atlwin.h>

#
"
33 !

$
#

// Base ATL classes


// Global _Module
// ATL windowing classes

<#
_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

// Create & show our main window


if ( NULL == wndMain.Create ( NULL, CWindow::rcDefault,
_T("My First ATL Window") ))
{
// Bad news, window creation failed

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>

// Base ATL classes


// ATL windowing classes

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 !

#>

class CAboutDlg : public CDialogImpl<CAboutDlg>


{
public:
enum { IDD = IDD_ABOUT };
BEGIN_MSG_MAP(CAboutDlg)
END_MSG_MAP()
};

#
<$ ;
WM_CLOSE #
WM_INITDIALOG
!

#
#
#
#

!
#

!
> 9< #

%#
$
! $

<$

>

<$
9

<#
#

#
C

#
#
#

class CAboutDlg : public CDialogImpl<CAboutDlg>


{
public:
enum { IDD = IDD_ABOUT };
BEGIN_MSG_MAP(CAboutDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_CLOSE, OnClose)
COMMAND_ID_HANDLER(IDOK, OnOKCancel)
COMMAND_ID_HANDLER(IDCANCEL, OnOKCancel)
END_MSG_MAP()
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
CenterWindow();
return TRUE;
// let the system set the focus
}
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
EndDialog(IDCANCEL);
return 0;
}
LRESULT OnOKCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
EndDialog(wID);
return 0;
}
};

=$
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
#

<# >
#

class CMyWindow : public CWindowImpl<CMyWindow, CWindow, CFrameWinTraits>,


public CPaintBkgnd<CMyWindow,RGB(0,0,255)>
{
public:
BEGIN_MSG_MAP(CMyWindow)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
COMMAND_ID_HANDLER(IDC_ABOUT, OnAbout)
// ...
CHAIN_MSG_MAP(CPaintBkgndBase)
END_MSG_MAP()
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
HMENU hmenu = LoadMenu ( _Module.GetResourceInstance(), // _AtlBaseModule in VC7
MAKEINTRESOURCE(IDR_MENU1) );
SetMenu ( hmenu );
return 0;

LRESULT OnAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)


{
CAboutDlg dlg;
dlg.DoModal();
return 0;

}
// ...
};

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

# & *0774;077, <9


<$ = #%
#9
>
=
@ !
# #
= #

@
!
#
&=
<

@
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

You might also like