1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cbptest.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
34 // TestWin32.cpp : Definiert den Einsprungpunkt für die Anwendung.
50 #include <..\..\inc\systools\win32\MtaOleClipb.h>
54 #define MAX_LOADSTRING 100
57 HINSTANCE hInst
; // aktuelle Instanz
58 WCHAR szTitle
[MAX_LOADSTRING
]; // Text der Titelzeile
59 WCHAR szWindowClass
[MAX_LOADSTRING
]; // Text der Titelzeile
60 ATOM
MyRegisterClass( HINSTANCE hInstance
);
61 BOOL
InitInstance( HINSTANCE
, int );
62 LRESULT CALLBACK
WndProc( HWND
, UINT
, WPARAM
, LPARAM
);
63 LRESULT CALLBACK
About( HWND
, UINT
, WPARAM
, LPARAM
);
64 void PasteClipboardData(HWND hwndParent
);
65 void PasteClipboardData2(HWND hwndParent
);
67 LPSTREAM g_pStm
= NULL
;
68 char* pTextBuff
= NULL
;
71 //----------------------------------------------------
73 //----------------------------------------------------
75 unsigned int _stdcall
ThreadProc(LPVOID pParam
)
77 IDataObject
* pIDataObj
= NULL
;
87 OleInitialize( NULL
);
89 hr
= OleGetClipboard( &pIDataObj
);
91 hr
= CoGetInterfaceAndReleaseStream(
93 __uuidof(IDataObject
),
94 reinterpret_cast<LPVOID
*>(&pIDataObj
));
96 formatETC
.cfFormat
= CF_TEXT
;
98 formatETC
.dwAspect
= DVASPECT_CONTENT
;
99 formatETC
.lindex
= -1;
100 formatETC
.tymed
= TYMED_HGLOBAL
;
102 hr
= pIDataObj
->GetData( &formatETC
, &stgMedium
);
103 pGlobMem
= GlobalLock( stgMedium
.hGlobal
);
104 if ( NULL
!= pGlobMem
)
106 if ( NULL
!= pTextBuff
)
111 sizeGlobBuff
= GlobalSize( stgMedium
.hGlobal
);
112 pTextBuff
= (char*)malloc( sizeGlobBuff
+ 1 );
113 ZeroMemory( pTextBuff
, sizeGlobBuff
+ 1 );
115 memcpy( pTextBuff
, pGlobMem
, sizeGlobBuff
);
116 lData
= sizeGlobBuff
;
118 InvalidateRect( hwnd
, NULL
, TRUE
);
119 UpdateWindow( hwnd
);
122 GlobalUnlock( stgMedium
.hGlobal
);
124 ReleaseStgMedium( &stgMedium
);
126 pIDataObj
->Release();
135 //----------------------------------------------------
137 //----------------------------------------------------
139 int APIENTRY
WinMain(HINSTANCE hInstance
,
140 HINSTANCE hPrevInstance
,
144 // ZU ERLEDIGEN: Fügen Sie hier den Code ein.
149 // it's important to initialize ole
150 // in order to use the clipboard
151 //hr = OleInitialize( NULL );
152 hr
= CoInitializeEx( NULL
, COINIT_MULTITHREADED
);
153 //hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
155 // Globale Zeichenfolgen initialisieren
156 LoadStringW(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
157 LoadStringW(hInstance
, IDC_TESTWIN32
, szWindowClass
, MAX_LOADSTRING
);
158 MyRegisterClass(hInstance
);
160 // Initialisierung der Anwendung durchführen:
161 if( !InitInstance( hInstance
, nCmdShow
) )
166 hAccelTable
= LoadAccelerators(hInstance
, (LPCTSTR
)IDC_TESTWIN32
);
168 // Hauptnachrichtenschleife:
169 while( GetMessage(&msg
, NULL
, 0, 0) )
171 if( !TranslateAccelerator (msg
.hwnd
, hAccelTable
, &msg
) )
173 TranslateMessage( &msg
);
174 DispatchMessage( &msg
);
178 // uninitializing the ole libraries
179 //OleUninitialize( );
188 // FUNKTION: MyRegisterClass()
190 // AUFGABE: Registriert die Fensterklasse.
194 // Diese Funktion und ihre Verwendung sind nur notwendig, wenn dieser Code
195 // mit Win32-Systemen vor der 'RegisterClassEx'-Funktion kompatibel sein soll,
196 // die zu Windows 95 hinzugefügt wurde. Es ist wichtig diese Funktion aufzurufen,
197 // damit der Anwendung kleine Symbole mit den richtigen Proportionen zugewiesen
200 ATOM
MyRegisterClass( HINSTANCE hInstance
)
204 wcex
.cbSize
= sizeof(WNDCLASSEX
);
206 wcex
.style
= CS_HREDRAW
| CS_VREDRAW
;
207 wcex
.lpfnWndProc
= (WNDPROC
)WndProc
;
210 wcex
.hInstance
= hInstance
;
211 wcex
.hIcon
= LoadIcon(hInstance
, (LPCTSTR
)IDI_TESTWIN32
);
212 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
213 wcex
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
214 wcex
.lpszMenuName
= (LPCWSTR
)IDC_TESTWIN32
;
215 wcex
.lpszClassName
= szWindowClass
;
216 wcex
.hIconSm
= LoadIcon(wcex
.hInstance
, (LPCTSTR
)IDI_SMALL
);
218 return RegisterClassExW(&wcex
);
222 // FUNKTION: InitInstance(HANDLE, int)
224 // AUFGABE: Speichert die Instanzzugriffsnummer und erstellt das Hauptfenster
228 // In dieser Funktion wird die Instanzzugriffsnummer in einer globalen Variable
229 // gespeichert und das Hauptprogrammfenster erstellt und angezeigt.
231 BOOL
InitInstance( HINSTANCE hInstance
, int nCmdShow
)
235 hInst
= hInstance
; // Instanzzugriffsnummer in unserer globalen Variable speichern
237 hWnd
= CreateWindowExW(0, szWindowClass
, szTitle
, WS_OVERLAPPEDWINDOW
,
238 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0, NULL
, NULL
, hInstance
, NULL
);
245 ShowWindow( hWnd
, nCmdShow
);
246 UpdateWindow( hWnd
);
252 // FUNKTION: WndProc(HWND, unsigned, WORD, LONG)
254 // AUFGABE: Verarbeitet Nachrichten für das Hauptfenster.
256 // WM_COMMAND - Anwendungsmenü verarbeiten
257 // WM_PAINT - Hauptfenster darstellen
258 // WM_DESTROY - Beendigungsnachricht ausgeben und zurückkehren
261 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
267 TCHAR szHello
[MAX_LOADSTRING
];
270 LoadString(hInst
, IDS_HELLO
, szHello
, MAX_LOADSTRING
);
275 wmId
= LOWORD(wParam
);
276 wmEvent
= HIWORD(wParam
);
277 // Menüauswahlen analysieren:
281 //PasteClipboardData(hWnd);
282 PasteClipboardData2(hWnd
);
286 DestroyWindow( hWnd
);
290 return DefWindowProc( hWnd
, message
, wParam
, lParam
);
295 hdc
= BeginPaint (hWnd
, &ps
);
296 // ZU ERLEDIGEN: Hier beliebigen Code zum Zeichnen hinzufügen...
298 GetClientRect( hWnd
, &rt
);
300 if ( NULL
!= pTextBuff
)
302 DrawText( hdc
, pTextBuff
, lData
, &rt
, DT_CENTER
);
306 DrawText( hdc
, szHello
, strlen(szHello
), &rt
, DT_CENTER
);
309 EndPaint( hWnd
, &ps
);
313 PostQuitMessage( 0 );
317 return DefWindowProc( hWnd
, message
, wParam
, lParam
);
322 void PasteClipboardData2(HWND hwndParent
)
324 IDataObject
* pIDataObject
;
332 hr
= MTAGetClipboard( &pIDataObject
);
333 if ( SUCCEEDED( hr
) )
335 formatETC
.cfFormat
= CF_TEXT
;
336 formatETC
.ptd
= NULL
;
337 formatETC
.dwAspect
= DVASPECT_CONTENT
;
338 formatETC
.lindex
= -1;
339 formatETC
.tymed
= TYMED_HGLOBAL
;
341 hr
= pIDataObject
->GetData( &formatETC
, &stgMedium
);
342 pGlobMem
= GlobalLock( stgMedium
.hGlobal
);
343 if ( NULL
!= pGlobMem
)
345 if ( NULL
!= pTextBuff
)
350 sizeGlobBuff
= GlobalSize( stgMedium
.hGlobal
);
351 pTextBuff
= (char*)malloc( sizeGlobBuff
+ 1 );
352 ZeroMemory( pTextBuff
, sizeGlobBuff
+ 1 );
354 memcpy( pTextBuff
, pGlobMem
, sizeGlobBuff
);
355 lData
= sizeGlobBuff
;
357 InvalidateRect( hwndParent
, NULL
, TRUE
);
358 UpdateWindow( hwndParent
);
361 GlobalUnlock( stgMedium
.hGlobal
);
363 ReleaseStgMedium( &stgMedium
);
365 pIDataObject
->Release();
369 //----------------------------------------------------
370 // clipboard handling
371 //----------------------------------------------------
374 void PasteClipboardData(HWND hwndParent)
376 IDataObject* pIDataObj = NULL;
380 hr = OleGetClipboard( &pIDataObj );
381 if ( SUCCEEDED( hr ) )
383 HRESULT hr = CoMarshalInterThreadInterfaceInStream(
384 __uuidof(IDataObject), //The IID of inteface to be marshaled
385 pIDataObj, //The interface pointer
386 &g_pStm //IStream pointer
389 HANDLE hThread = (HANDLE)_beginthreadex(
392 ThreadProc, //Start Address
394 (unsigned int)hwndParent, //Creation Flag
398 //Wait for the thread to finish execution
399 //A thread handle is signaled is thread execution
403 DWORD dwRet = ::MsgWaitForMultipleObjects(
404 1, //Count of objects
405 &hThread, //pointer to the array of objects
406 FALSE, //Wait for all objects?
407 INFINITE, //Wait How Long?
408 QS_ALLINPUT //Wait for all messges
411 //This means that the object is signaled
412 if ( dwRet != WAIT_OBJECT_0 + 1 )
415 //Remove the messages from the queue
418 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
421 TranslateMessage(&msg);
422 //Let the windowproc handle the message
423 DispatchMessage(&msg);
427 CloseHandle( hThread );
428 pIDataObj->Release();