]> git.saurik.com Git - wxWidgets.git/blob - samples/dll/sdk_exe.cpp
No code changes, fixed some typos.
[wxWidgets.git] / samples / dll / sdk_exe.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/dll/my_exe.cpp
3 // Purpose: Sample showing how to use wx DLL from a Win32 application
4 // Author: Vadim Zeitlin
5 // Created: 2009-12-07
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 /*
12 This program is intentionally as simple as possible and shouldn't be seen
13 as an example of how to write a proper Win32 application (why should you
14 want to do this anyhow when you have wxWidgets). It's just a test bed for
15 the wx DLL which it uses.
16 */
17
18 // ============================================================================
19 // declarations
20 // ============================================================================
21
22 // ----------------------------------------------------------------------------
23 // headers
24 // ----------------------------------------------------------------------------
25
26 #include <windows.h>
27 #include <windowsx.h>
28
29 #include <stdio.h>
30 #include <tchar.h>
31
32 #include "my_dll.h"
33
34 namespace
35 {
36
37 // ----------------------------------------------------------------------------
38 // constants
39 // ----------------------------------------------------------------------------
40
41 const TCHAR *MAIN_WIN_CLASS_NAME = _TEXT("my_exe_main_win_class");
42
43 const int IDB_RUN_GUI_FROM_DLL = 100;
44
45 // ----------------------------------------------------------------------------
46 // globals
47 // ----------------------------------------------------------------------------
48
49 HINSTANCE g_hInstance;
50 HWND g_hwndMain;
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // callbacks
58 // ----------------------------------------------------------------------------
59
60 void
61 OnCommand(HWND /* hwnd */, int id, HWND /* hwndCtl */, UINT /* codeNotify */)
62 {
63 if ( id == IDB_RUN_GUI_FROM_DLL )
64 {
65 run_wx_gui_from_dll("child instance");
66 }
67 }
68
69 void OnDestroy(HWND hwnd)
70 {
71 wx_dll_cleanup();
72
73 PostQuitMessage(0);
74 }
75
76 LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
77 {
78 switch ( msg )
79 {
80 HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
81 HANDLE_MSG(hwnd, WM_DESTROY, OnDestroy);
82
83 default:
84 return DefWindowProc(hwnd, msg, wParam, lParam);
85 }
86
87 return 0;
88 }
89
90 // ----------------------------------------------------------------------------
91 // initialization functions
92 // ----------------------------------------------------------------------------
93
94 bool RegisterMainClass()
95 {
96 WNDCLASS wc;
97 ZeroMemory(&wc, sizeof(wc));
98 wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
99 wc.lpfnWndProc = MainWndProc;
100 wc.hInstance = g_hInstance;
101 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
102 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
103 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
104 wc.lpszClassName = MAIN_WIN_CLASS_NAME;
105
106 return RegisterClass(&wc) != 0;
107 }
108
109 bool CreateMainWindow()
110 {
111 g_hwndMain = CreateWindow
112 (
113 MAIN_WIN_CLASS_NAME,
114 _TEXT("Main Win32 app"),
115 WS_OVERLAPPEDWINDOW,
116 CW_USEDEFAULT, CW_USEDEFAULT,
117 400, 300,
118 NULL, NULL, g_hInstance, NULL
119 );
120 if ( !g_hwndMain )
121 return false;
122
123 CreateWindow
124 (
125 _TEXT("static"),
126 _TEXT("Main Win32 application"),
127 WS_CHILD | WS_VISIBLE,
128 10, 10, 200, 30,
129 g_hwndMain, (HMENU)-1, g_hInstance, NULL
130 );
131
132 CreateWindow
133 (
134 _TEXT("button"),
135 _TEXT("Run GUI from DLL"),
136 WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
137 200, 200, 150, 35,
138 g_hwndMain, (HMENU)IDB_RUN_GUI_FROM_DLL, g_hInstance, NULL
139 );
140
141 return true;
142 }
143
144 } // anonymous namespace
145
146 // ----------------------------------------------------------------------------
147 // entry point
148 // ----------------------------------------------------------------------------
149
150 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
151 {
152 g_hInstance = hInstance;
153
154 if ( !RegisterMainClass() )
155 return 1;
156
157 if ( !CreateMainWindow() )
158 return 2;
159
160 ShowWindow(g_hwndMain, nCmdShow);
161
162 MSG msg;
163 while ( GetMessage(&msg, NULL, 0, 0) )
164 {
165 TranslateMessage(&msg);
166 DispatchMessage(&msg);
167 }
168
169 return 0;
170 }