xref: /AOO41X/main/sal/workben/clipboardwben/testpaste/cbptest.cxx (revision 87d2adbc9cadf14644c3679b041b9226f7630199)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_sal.hxx"
27 // TestWin32.cpp : Definiert den Einsprungpunkt f�r die Anwendung.
28 //
29 
30 #define _WIN32_DCOM
31 
32 #include "stdafx.h"
33 
34 #include <windows.h>
35 
36 #include <ole2.h>
37 #include <objidl.h>
38 #include <objbase.h>
39 #include <process.h>
40 #include <olectl.h>
41 #include <stdlib.h>
42 #include <malloc.h>
43 #include <..\..\inc\systools\win32\MtaOleClipb.h>
44 
45 #include "resource.h"
46 
47 #define MAX_LOADSTRING 100
48 
49 // Globale Variablen:
50 HINSTANCE           hInst;                      // aktuelle Instanz
51 WCHAR               szTitle[MAX_LOADSTRING];            // Text der Titelzeile
52 WCHAR               szWindowClass[MAX_LOADSTRING];  // Text der Titelzeile
53 ATOM                MyRegisterClass( HINSTANCE hInstance );
54 BOOL                InitInstance( HINSTANCE, int );
55 LRESULT CALLBACK    WndProc( HWND, UINT, WPARAM, LPARAM );
56 LRESULT CALLBACK    About( HWND, UINT, WPARAM, LPARAM );
57 void                PasteClipboardData(HWND hwndParent);
58 void                PasteClipboardData2(HWND hwndParent);
59 
60 LPSTREAM            g_pStm    = NULL;
61 char*               pTextBuff = NULL;
62 DWORD               lData     = 0;
63 
64 //----------------------------------------------------
65 // a thread function
66 //----------------------------------------------------
67 
ThreadProc(LPVOID pParam)68 unsigned int _stdcall ThreadProc(LPVOID pParam)
69 {
70     IDataObject* pIDataObj = NULL;
71     FORMATETC    formatETC;
72     STGMEDIUM    stgMedium;
73     LPVOID       pGlobMem;
74     HWND         hwnd;
75     DWORD        sizeGlobBuff;
76     HRESULT      hr;
77 
78     hwnd = (HWND)pParam;
79 
80     OleInitialize( NULL );
81 
82     hr = OleGetClipboard( &pIDataObj );
83 
84     hr = CoGetInterfaceAndReleaseStream(
85         g_pStm,
86         __uuidof(IDataObject),
87         reinterpret_cast<LPVOID*>(&pIDataObj));
88 
89     formatETC.cfFormat = CF_TEXT;
90     formatETC.ptd      = NULL;
91     formatETC.dwAspect = DVASPECT_CONTENT;
92     formatETC.lindex   = -1;
93     formatETC.tymed    = TYMED_HGLOBAL;
94 
95     hr = pIDataObj->GetData( &formatETC, &stgMedium );
96     pGlobMem = GlobalLock( stgMedium.hGlobal );
97     if ( NULL != pGlobMem )
98     {
99         if ( NULL != pTextBuff )
100         {
101             free( pTextBuff );
102         }
103 
104         sizeGlobBuff = GlobalSize( stgMedium.hGlobal );
105         pTextBuff = (char*)malloc( sizeGlobBuff + 1 );
106         ZeroMemory( pTextBuff, sizeGlobBuff + 1 );
107 
108         memcpy( pTextBuff, pGlobMem, sizeGlobBuff );
109         lData = sizeGlobBuff;
110 
111         InvalidateRect( hwnd, NULL, TRUE );
112         UpdateWindow( hwnd );
113     }
114 
115     GlobalUnlock( stgMedium.hGlobal );
116 
117     ReleaseStgMedium( &stgMedium );
118 
119     pIDataObj->Release();
120 
121     //CoUninitialize( );
122 
123     OleUninitialize( );
124 
125     return 0;
126 }
127 
128 //----------------------------------------------------
129 // WinMain
130 //----------------------------------------------------
131 
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)132 int APIENTRY WinMain(HINSTANCE hInstance,
133                      HINSTANCE hPrevInstance,
134                      LPSTR     lpCmdLine,
135                      int       nCmdShow )
136 {
137     // ZU ERLEDIGEN: F�gen Sie hier den Code ein.
138     MSG     msg;
139     HACCEL  hAccelTable;
140     HRESULT hr = E_FAIL;
141 
142     // it's important to initialize ole
143     // in order to use the clipboard
144     //hr = OleInitialize( NULL );
145     hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
146     //hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
147 
148     // Globale Zeichenfolgen initialisieren
149     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
150     LoadStringW(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);
151     MyRegisterClass(hInstance);
152 
153     // Initialisierung der Anwendung durchf�hren:
154     if( !InitInstance( hInstance, nCmdShow ) )
155     {
156         return FALSE;
157     }
158 
159     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32);
160 
161     // Hauptnachrichtenschleife:
162     while( GetMessage(&msg, NULL, 0, 0) )
163     {
164         if( !TranslateAccelerator (msg.hwnd, hAccelTable, &msg) )
165         {
166             TranslateMessage( &msg );
167             DispatchMessage( &msg );
168         }
169     }
170 
171     // uninitializing the ole libraries
172     //OleUninitialize( );
173     CoUninitialize( );
174 
175     return msg.wParam;
176 }
177 
178 
179 
180 //
181 //  FUNKTION: MyRegisterClass()
182 //
183 //  AUFGABE: Registriert die Fensterklasse.
184 //
185 //  KOMMENTARE:
186 //
187 //    Diese Funktion und ihre Verwendung sind nur notwendig, wenn dieser Code
188 //    mit Win32-Systemen vor der 'RegisterClassEx'-Funktion kompatibel sein soll,
189 //    die zu Windows 95 hinzugef�gt wurde. Es ist wichtig diese Funktion aufzurufen,
190 //    damit der Anwendung kleine Symbole mit den richtigen Proportionen zugewiesen
191 //    werden.
192 //
MyRegisterClass(HINSTANCE hInstance)193 ATOM MyRegisterClass( HINSTANCE hInstance )
194 {
195     WNDCLASSEXW wcex;
196 
197     wcex.cbSize = sizeof(WNDCLASSEX);
198 
199     wcex.style          = CS_HREDRAW | CS_VREDRAW;
200     wcex.lpfnWndProc    = (WNDPROC)WndProc;
201     wcex.cbClsExtra     = 0;
202     wcex.cbWndExtra     = 0;
203     wcex.hInstance      = hInstance;
204     wcex.hIcon          = LoadIcon(hInstance, (LPCTSTR)IDI_TESTWIN32);
205     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
206     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
207     wcex.lpszMenuName   = (LPCWSTR)IDC_TESTWIN32;
208     wcex.lpszClassName  = szWindowClass;
209     wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
210 
211     return RegisterClassExW(&wcex);
212 }
213 
214 //
215 //   FUNKTION: InitInstance(HANDLE, int)
216 //
217 //   AUFGABE: Speichert die Instanzzugriffsnummer und erstellt das Hauptfenster
218 //
219 //   KOMMENTARE:
220 //
221 //        In dieser Funktion wird die Instanzzugriffsnummer in einer globalen Variable
222 //        gespeichert und das Hauptprogrammfenster erstellt und angezeigt.
223 //
InitInstance(HINSTANCE hInstance,int nCmdShow)224 BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
225 {
226    HWND hWnd;
227 
228    hInst = hInstance; // Instanzzugriffsnummer in unserer globalen Variable speichern
229 
230    hWnd = CreateWindowExW(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
231       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
232 
233    if( !hWnd )
234    {
235       return FALSE;
236    }
237 
238    ShowWindow( hWnd, nCmdShow );
239    UpdateWindow( hWnd );
240 
241    return TRUE;
242 }
243 
244 //
245 //  FUNKTION: WndProc(HWND, unsigned, WORD, LONG)
246 //
247 //  AUFGABE:  Verarbeitet Nachrichten f�r das Hauptfenster.
248 //
249 //  WM_COMMAND  - Anwendungsmen� verarbeiten
250 //  WM_PAINT    - Hauptfenster darstellen
251 //  WM_DESTROY  - Beendigungsnachricht ausgeben und zur�ckkehren
252 //
253 //
WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)254 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
255 {
256     int         wmId;
257     int         wmEvent;
258     PAINTSTRUCT ps;
259     HDC         hdc;
260     TCHAR       szHello[MAX_LOADSTRING];
261 
262 
263     LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
264 
265     switch( message )
266     {
267         case WM_COMMAND:
268             wmId    = LOWORD(wParam);
269             wmEvent = HIWORD(wParam);
270             // Men�auswahlen analysieren:
271             switch( wmId )
272             {
273                 case IDD_PASTE:
274                     //PasteClipboardData(hWnd);
275                     PasteClipboardData2(hWnd);
276                     break;
277 
278                 case IDM_EXIT:
279                    DestroyWindow( hWnd );
280                    break;
281 
282                 default:
283                    return DefWindowProc( hWnd, message, wParam, lParam );
284             }
285             break;
286 
287         case WM_PAINT:
288             hdc = BeginPaint (hWnd, &ps);
289             // ZU ERLEDIGEN: Hier beliebigen Code zum Zeichnen hinzuf�gen...
290             RECT rt;
291             GetClientRect( hWnd, &rt );
292 
293             if ( NULL != pTextBuff )
294             {
295                 DrawText( hdc, pTextBuff, lData, &rt, DT_CENTER );
296             }
297             else
298             {
299                 DrawText( hdc, szHello, strlen(szHello), &rt, DT_CENTER );
300             }
301 
302             EndPaint( hWnd, &ps );
303             break;
304 
305         case WM_DESTROY:
306             PostQuitMessage( 0 );
307             break;
308 
309         default:
310             return DefWindowProc( hWnd, message, wParam, lParam );
311    }
312    return 0;
313 }
314 
PasteClipboardData2(HWND hwndParent)315 void PasteClipboardData2(HWND hwndParent)
316 {
317     IDataObject* pIDataObject;
318     HRESULT      hr;
319     FORMATETC    formatETC;
320     STGMEDIUM    stgMedium;
321     LPVOID       pGlobMem;
322     HWND         hwnd;
323     DWORD        sizeGlobBuff;
324 
325     hr = MTAGetClipboard( &pIDataObject );
326     if ( SUCCEEDED( hr ) )
327     {
328         formatETC.cfFormat = CF_TEXT;
329         formatETC.ptd      = NULL;
330         formatETC.dwAspect = DVASPECT_CONTENT;
331         formatETC.lindex   = -1;
332         formatETC.tymed    = TYMED_HGLOBAL;
333 
334         hr = pIDataObject->GetData( &formatETC, &stgMedium );
335         pGlobMem = GlobalLock( stgMedium.hGlobal );
336         if ( NULL != pGlobMem )
337         {
338             if ( NULL != pTextBuff )
339             {
340                 free( pTextBuff );
341             }
342 
343             sizeGlobBuff = GlobalSize( stgMedium.hGlobal );
344             pTextBuff = (char*)malloc( sizeGlobBuff + 1 );
345             ZeroMemory( pTextBuff, sizeGlobBuff + 1 );
346 
347             memcpy( pTextBuff, pGlobMem, sizeGlobBuff );
348             lData = sizeGlobBuff;
349 
350             InvalidateRect( hwndParent, NULL, TRUE );
351             UpdateWindow( hwndParent );
352         }
353 
354         GlobalUnlock( stgMedium.hGlobal );
355 
356         ReleaseStgMedium( &stgMedium );
357 
358         pIDataObject->Release();
359     }
360 }
361 
362 //----------------------------------------------------
363 // clipboard handling
364 //----------------------------------------------------
365 
366 /*
367 void PasteClipboardData(HWND hwndParent)
368 {
369     IDataObject* pIDataObj = NULL;
370     HRESULT      hr        = E_FAIL;
371     unsigned int dwId;
372 
373     hr = OleGetClipboard( &pIDataObj );
374     if ( SUCCEEDED( hr ) )
375     {
376         HRESULT hr = CoMarshalInterThreadInterfaceInStream(
377             __uuidof(IDataObject), //The IID of inteface to be marshaled
378             pIDataObj,       //The interface pointer
379             &g_pStm          //IStream pointer
380             );
381 
382         HANDLE hThread = (HANDLE)_beginthreadex(
383                 NULL,       //Security
384                 0,          //Stack Size
385                 ThreadProc, //Start Address
386                 NULL,       //Parmeter
387                 (unsigned int)hwndParent,   //Creation Flag
388                 &dwId       //Thread Id
389                 );
390 
391         //Wait for the thread to finish execution
392         //A thread handle is signaled is thread execution
393         //is complete
394         for(;;)
395         {
396             DWORD dwRet = ::MsgWaitForMultipleObjects(
397                                     1,          //Count of objects
398                                     &hThread,   //pointer to the array of objects
399                                     FALSE,      //Wait for all objects?
400                                     INFINITE,   //Wait How Long?
401                                     QS_ALLINPUT //Wait for all messges
402                                     );
403 
404             //This means that the object is signaled
405             if ( dwRet != WAIT_OBJECT_0 + 1 )
406                 break;
407 
408             //Remove the messages from the queue
409             MSG msg;
410 
411             while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
412             {
413                 //Not essential
414                 TranslateMessage(&msg);
415                 //Let the windowproc handle the message
416                 DispatchMessage(&msg);
417             }
418         }
419 
420         CloseHandle( hThread );
421         pIDataObj->Release();
422     }
423 }
424 */
425