ホーム

OFF-SOFT.net

OFF-SOFT.net

ウェブやソフトウェアに関するサポート&情報サイトです。サイト構築からソフトウェアの作成、利用まであなたの助けになるかも・・・・しれません。たぶん・・。

(1) VC++ 2008 Expressを使ってWin32++の環境設定とサンプルコンパイル

公開日| 2010年03月30日 | 2 のコメントがあります。
概要 :
 win32++がV6.8にアップデートされました。
 win32++も、多少は、知名度が上がってきたようで、日本のサイトでもちらほら見かけるようになりました。しかしながら、MFC,WTLにくらべれば、全くマイナーなGUIライブラリです。

 あまりwin32++をご存じない方のために簡単に説明しておきます。
 win32++は、C++で記述されたWindows専用のGUIライブラリで、MFCそっくりに作られています。しかし、残念ながら、MFCと全く同等の機能は持ちません。MFCの持っている画面関連クラスについて対応してて、それ以外では、ソケットクラスぐらいです。(2010.3現在) 例えば、CFontやCFileなどはありません。WTLでも同じようにすべてが対応づけられないようにwin32++も対応づけられません。

つまり、MFCのプラグラムをそのまま移植するのは、MFCからWTLと同じくらいに多少手間がかかります。

ここまでの説明を踏まえて、今回は、まず、win32++の開発環境について簡単に説明してみましょう。

ダウンロード

win32++のライセンス

win32++のライセンスは、MITライセンスに近いものです。
ソースコードの先頭にコピーライト表示とライセンスについての記述がありますので、そちらを確認しましょう。
以下は、ライセンスに関する原文の抜粋です。

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

まずは、サンプルをコンパイルしてみましょう。
ダウンロードしたWin32xx NNN.zipを適当なディレクトリへ解凍します
* NNN: バージョン番号

解凍すると以下のようなフォルダが作成されたことと思います。
├─Browser
│  ├─ProjectFiles
│  └─src
│      └─res
├─Dialog
│  ├─ProjectFiles
│  └─src
│      └─res
├─DialogBars
│  ├─ProjectFiles
│  └─src
│      └─res
├─DialogDemo
│  ├─ProjectFiles
│  └─src
│      └─res
├─DialogTab
│  ├─ProjectFiles
│  └─src
│      └─res
├─DirectX
│  ├─ProjectFiles
│  └─src
│      └─res
├─Dock
│  ├─ProjectFiles
│  └─src
│      └─res
├─DockContainer
│  ├─ProjectFiles
│  └─src
│      └─res
├─DockTabbedMDI
│  ├─ProjectFiles
│  └─src
│      └─res
├─Explorer
│  ├─ProjectFiles
│  └─src
│      └─res
├─FastGDI
│  ├─ProjectFiles
│  └─src
│      └─res
├─FormDemo
│  ├─ProjectFiles
│  └─src
│      └─res
├─Frame
│  ├─ProjectFiles
│  └─src
│      └─res
├─MDIFrame
│  ├─ProjectFiles
│  └─src
│      └─res
├─MDIFrameDemo
│  ├─ProjectFiles
│  └─src
│      └─res
├─MDIFrameSplitter
│  ├─ProjectFiles
│  └─src
│      └─res
├─Networking
│  ├─ClientDlg
│  │  └─res
│  ├─ServerDlg
│  │  └─res
│  └─Simple
├─Notepad
│  ├─ProjectFiles
│  └─src
│      └─res
├─Performance
│  ├─ProjectFiles
│  └─src
├─Picture
│  ├─ProjectFiles
│  └─src
│      └─res
├─PropertySheet
│  ├─ProjectFiles
│  └─src
│      └─res
├─RibbonFrame
│  ├─ProjectFiles
│  └─src
│      └─res
├─RibbonSimple
│  ├─ProjectFiles
│  └─src
│      └─res
├─Scribble
│  ├─ProjectFiles
│  └─src
│      └─res
├─Simple
│  ├─ProjectFiles
│  └─src
│      └─res
├─Splitter
│  ├─ProjectFiles
│  └─src
│      └─res
├─TabDemo
│  ├─ProjectFiles
│  └─src
│      └─res
├─Themes
│  ├─ProjectFiles
│  └─src
│      └─res
├─Threads
│  ├─ProjectFiles
│  └─src
├─Tutorials
│  ├─Tutorial1
│  ├─Tutorial2
│  ├─Tutorial3
│  ├─Tutorial4
│  ├─Tutorial5
│  │  └─res
│  ├─Tutorial6
│  │  └─res
│  ├─Tutorial7
│  │  └─res
│  ├─Tutorial8
│  │  └─res
│  └─Tutorial9
│      └─res
├─Win32++
│  ├─help
│  ├─include
│  ├─new projects
│  ├─output
│  └─tools
└─WinCE Samples
    ├─Dialog
    │  └─res
    ├─DialogDemo
    │  └─res
    ├─DlgSubclass
    │  └─res
    ├─PocketPCFrame
    │  └─res
    ├─Scribble
    │  └─res
    ├─Simple
    └─Subclass
ここで、win32++のライブラリは、フォルダ名:Win32++にすべてあります。
つまり、それ以外は、ドキュメント並びにサンプルなのです。

Win32++ディレクトリの構成は、以下のとおりです。

Win32++\include : ヘッダのみで構成されたwin32++ライブラリです。
Win32++\src : ヘッダ、ソースに分離されたwin32++ライブラリです。

つまり前者は、WTLのようにインクルードするだけでwin32++のプロジェクトを作成できます。
後者は、スタティックライブラリを作成し、win32++のプロジェクトへリブリンクすることになるでしょう。


今回は、環境を知ることが目的ですから、まずは、簡単な画面サンプルをコンパイルしてみましょう。

SimpleWindowsサンプルをコンパイルしてみる
解凍して作成されたフォルダ"Simple"の下、ProjectFilesというフォルダがあると思います。
そこには、VC++2003,2005,2008,2010に対応したプロジェクトファイルが存在します。
今回は、VC++2008 Expressを使ってみます。

Simple_2008.sln をVC++2008 Expressで開いてみましょう。


次は、ビルドです。ビルドを実行してみてください。
以下の警告が出力されるかもしれません。
..\win32++\include\wincore.h(816) : warning C4819: 
..\win32++\include\wincore.h(1806) : warning C4819: 
この警告は、通常のASCIIコード(日本語の環境であればShiftJis)で認識できない文字コードを含んでいるため正しく保存できない由の警告です。
警告先を確認すれば分かりますが、無視しても大丈夫だと思います。

先の警告を除き実行ファイルの作成までできたことと思います。

さあ、実行してみましょう。

このような画面が表示されましたか?

サンプルをコンパイルは、ここまででOKです。
特別なにも設定する必要もありません。

ほとんどのサンプルはコンパイルできる事と思います。ただし、ATL(COM)を使ったサンプルBrowserや、Windows7用に対応しているRibbonなどは、VC++ ExpressのみやXPの環境ではコンパイルできないでしょう。
※上記件は、別の記事で説明したいと思います。

サンプルの中身を見てみましょう。
先にコンパイルし、実行してみたSimpleWIndowについてソースコードを確認してみましょう。
プロジェクトは、以下のファイルから構成しています。
main.cpp
SimpleApp.cpp
SimpleApp.h
View.cpp
View.h
resource.h
Resource.rc
res\star.ico

[main.cpp]
1
2
3
4
5
6
7
8
9
10
11
#include "SimpleApp.h"
 
 
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    // Start Win32++
    CSimpleApp MyApp;
 
	// Run the application
    return MyApp.Run();
}
SimpleApp.hで定義されているclass CSimpleApp (親クラス CWinApp)を生成し、Run()メソッドを起動しているだけです。

[SimpleApp.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Definitions for the CSimpleApp class
CSimpleApp::CSimpleApp()
{
    // Constructor for CSimpleApp
}
 
BOOL CSimpleApp::InitInstance()
{
    // This function is called automatically when the application starts
 
    // Create the Window
    m_View.Create();
 
	return TRUE;
}
InitInstance()メソッドで、View.hで定義されているclass CView(親クラス CWnd)を生成(Create)しているだけです。

[View.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Definitions for the CView class
void CView::OnCreate()
{
	// OnCreate is called automatically during window creation when a
	// WM_CREATE message received.
 
	// Tasks such as setting the icon, creating child windows, or anything
	// associated with creating windows are normally performed here.
 
	// Set the window's icon
	SetIconSmall(IDW_MAIN);
	SetIconLarge(IDW_MAIN);
 
	TRACE(_T("OnCreate\n"));
}
 
void CView::OnDestroy()
{
	// End the application when the window is destroyed
	::PostQuitMessage(0);
}
 
void CView::OnInitialUpdate()
{
	// OnInitialUpdate is called after the window is created.
	// Tasks which are to be done after the window is created go here.
 
	TRACE(_T("OnInitialUpdate\n"));
}
 
void CView::OnPaint(HDC hDC)
{
	// OnPaint is called automatically whenever a part of the
	// window needs to be repainted.
 
	// Centre some text in our view window
	CRect r = GetClientRect();
	::DrawText(hDC, LoadString(IDW_MAIN), -1, &r, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
 
void CView::PreCreate(CREATESTRUCT& cs)
{
	// This function will be called automatically by Create. It provides an
	// opportunity to set various window parameters prior to window creation.
	// You are not required to set these parameters, any paramters which
	// aren't specified are set to reasonable defaults.
 
	// Set some optional parameters for the window
	cs.dwExStyle = WS_EX_CLIENTEDGE;		// Extended style
	cs.lpszClass = _T("View Window");		// Window Class
	cs.x = 50;								// top x
	cs.y = 50;								// top y
	cs.cx = 400;							// width
	cs.cy = 300;							// height
	cs.lpszName = LoadString(IDW_MAIN);		// Window title
}
 
void CView::OnSize()
{
	// Force the window to be repainted during resizing
	Invalidate();
}
 
LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	// This function is our message procedure. We process the messages for
	// the view window here.  Unprocessed messages are passed on for
	//  default processing.
 
	switch(uMsg)
	{
	case WM_DESTROY:
		OnDestroy();
		return 0;	// return a value. No default processing
 
	case WM_SIZE:
		OnSize();
		break;	// and also do default processing for this message
	}
 
	// pass unhandled messages on for default processing
	return WndProcDefault(uMsg, wParam, lParam);
}
PreCreate()メソッド内で、画面のタイトル、サイズ、スタイルを指定しています。
OnCreate()メソッド内で、アイコンを指定しています。
OnPaint()メソッド内で、画面中央に"Simple Window"と文字列を表示しています。

resource.h
Resource.rc
res\star.ico
これらは、リソース関連のファイルなので、説明を省略します。

MFCをご存じであれば、上記の継承関係だけで大まかな流れを把握できることと思います。
また、WIndows API に精通されていれば、win32++のライブラリであるwincore.hを確認されれば、処理の流れをすぐに把握できることと思います。

もし、MFCもWIndows API もご存じないようであれば、まずは、wincore.hの中をどのように動作するかmainルーチンからステップ実行で追っていかれることをおすすめします。
その上で、Windowsがどのように作成されていくかをしっかりと理解しましょう。


今回は、環境と簡単のサンプルまででしたが、MFC経験者は、win32++の可能性に気づかれたのではないでしょうか。
win32++をうまく使えば、MFCからの依存から抜け出せるかもしれません。

コメント

2 のコメントがあります。 “(1) VC++ 2008 Expressを使ってWin32++の環境設定とサンプルコンパイル”


  1. 41
    2011年03月23日 @ 20:43:54

    Poor MFC clone with no flexibility in the design.
    This is why WTL/ATL still rules.

  2. NEL
    2011年12月21日 @ 20:07:27

    Win32++ still lacks most of the features found in many frameworks like message mapping macros. Using the traditional switch statements tends to become spaghetti code especially if there are lots of messages and events that must be handled.

    This framework seems suitable for beginners but not for large projects or commercial software development.

コメントをどうぞ







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ