Learning WTL8.0 Part-1 Win32 vs. ATL Windows Programming(二)

开发者在线 Builder.com.cn 更新时间:2007-09-24作者:ghost 来源:CSDN

本文关键词: ghost Win32 Windows

2. “Hello World!” in ATL
为了避免创建ATL COM EXE SERVER的开销和更好地说明Win32与ATL的内在关系,本例程序基于 1.1创建一个Win32 Project的程序创建。 
 
对于ATL Windows编程陌生的,于此处不必过于在意,稍后在3. 对Win32和ATL的初步观察和比较会做一定的讲解。
 
2.1 修改stdafx.h
在1.1创建的Win32代码中移除或注释掉stdafx.h中的:
// Windows Header Files:
#include <windows.h>
 
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
 
并在stdafx.h中添加如下的include语句:
 
#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include <atlwin.h>
#include "CWellcomeWindow.h"
 
2.2 添加CWellcomeWindow.h
#pragma once
#include "stdafx.h"
 
class CWellcomeWindow : public CWindowImpl<CWellcomeWindow,
      CWindow, CFrameWinTraits> {
public:
      DECLARE_WND_CLASS(NULL);
 
      BEGIN_MSG_MAP(CWellcomeWindow)
            MESSAGE_HANDLER(WM_CREATE, OnCreate)
            MESSAGE_HANDLER(WM_PAINT, OnPaint)
            MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
      END_MSG_MAP()
 
      LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
            HICON appIcon = LoadIcon(_Module.GetResourceInstance(),
                  MAKEINTRESOURCE(IDI_LEARNINGWTLPART1_ATL));
            this->SetIcon(appIcon);
 
            return 0;
      }
 
 
      LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
            PAINTSTRUCT ps;
            CComBSTR wellcome(_T("Hello World!"));
 
            HDC hdc; // = this->GetDC();
 
            hdc = this->BeginPaint(&ps); // this->BeginPaint(&ps);
                  TextOut(hdc, 0, 0, wellcome, wellcome.Length());
            this->EndPaint(&ps);
            //this->ReleaseDC(hdc);
 
            return 0;
      }
 
      LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
            ::PostQuitMessage(0);
 
            return 0;
      }
};
2.3 修改win32.cpp
保留WinMain方法声明和#include "stdafx.h"语句将其余代码删除或注释掉。添加_Module定义,修改WndMain方法体;完成后的代码如下:
#include "stdafx.h"
#include "LearningWTLPart1_ATL.h"
 
CComModule _Module;
 
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
      UNREFERENCED_PARAMETER(hPrevInstance);
      UNREFERENCED_PARAMETER(lpCmdLine);
 
      _Module.Init(NULL, hInstance);
 
      CComBSTR appTitle;
      appTitle.LoadString(_Module.GetResourceInstance(), IDS_APP_TITLE);
 
      CWellcomeWindow wnd;
      wnd.Create(NULL, 0, appTitle);
 
      // Main message loop:
      MSG msg;
      while (GetMessage(&msg, NULL, 0, 0))
      {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
 
      }
 
      _Module.Term();
 
      return (int) msg.wParam;
}
 
2.4 Build与程序输出
如果在1.1创建一个Win32 Project设置了编译器选项的,先将编译器选项设置恢复为默认值即/TP
 
程序输出如下:
 
以上的程序输出和 1.3 Build与程序输出对比,似乎缺点什么?!在Part-2再作交待。

用户评论

  • 用户名
  • 评论内容