-
Notifications
You must be signed in to change notification settings - Fork 0
/
acandidatewindows.cpp
202 lines (169 loc) · 5.48 KB
/
acandidatewindows.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright 2010 Keng-Yu Lin <[email protected]>
*
* This file is part of Array31.
*
* Array31 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Array31 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Array31. If not, see <http://www.gnu.org/licenses/>.
*/
#include "acandidatewindows.h"
HINSTANCE ACandidateWindow::hInstance = 0;
HICON ACandidateWindow::hicon = 0;
HFONT ACandidateWindow::hfont = 0;
std::wstring ACandidateWindow::composition;
std::wstring ACandidateWindow::candidates;
HWND ACandidateWindow::appHwnd = 0;
int ACandidateWindow::szX = 0;
int ACandidateWindow::szY = 0;
int ACandidateWindow::avgCharWidth = 0;
int ACandidateWindow::maxCharWidth = 0;
/*
void TRACEX( std::string s )
{
//std::ofstream f( "c:\\t.txt", std::ios::app );
//f << s;
}
void TRACEW( std::wstring s )
{
std::wofstream f( "c:\\users\\lexical\\array31w.txt", std::ios::app );
f << s;
}
*/
ACandidateWindow::ACandidateWindow()
: hwnd( 0 )
{
// load icon
hicon = LoadIcon( hInstance, MAKEINTRESOURCE( 100 ) );
// choose font
hfont = CreateFont( 0, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE, L"微軟正黑體" );
//hfont = CreateFont( 0, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE, L"細明體" );
TEXTMETRIC tm;
HDC hdc = GetDC( hwnd );
SelectObject( hdc, hfont );
GetTextMetrics( hdc, & tm ) ;
avgCharWidth = tm.tmAveCharWidth;
maxCharWidth = tm.tmMaxCharWidth;
//std::stringstream ss; ss << tm.tmAveCharWidth << "\t" << tm.tmMaxCharWidth << std::endl;
//TRACEX( ss.str() );
ReleaseDC( hwnd, hdc );
szX = 40 + 32 * avgCharWidth + 10 * maxCharWidth; //550;
szY = 32;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = ACandidateWindow::WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
//wc.hbrBackground = ( HBRUSH ) GetStockObject( LTGRAY_BRUSH ); // background color
//wc.hbrBackground = CreateSolidBrush( RGB( 0, 176, 142 ) ); // background color
wc.hbrBackground = CreateSolidBrush( RGB( 0, 150, 255 ) ); // background color
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT( "ACandidateWindow" );
RegisterClass( & wc );
HWND desktopHwnd = GetDesktopWindow();
RECT desktopRect;
GetWindowRect( desktopHwnd, & desktopRect );
hwnd = CreateWindowEx( WS_EX_TOOLWINDOW | WS_EX_TOPMOST, TEXT( "ACandidateWindow" ), TEXT( "caption" ), WS_POPUP, 100, desktopRect.bottom - 150, szX, szY, NULL, NULL, hInstance, NULL );
}
ACandidateWindow::~ACandidateWindow()
{
DestroyWindow( hwnd );
UnregisterClass( TEXT( "ACandidateWindow" ), hInstance );
}
BOOL ACandidateWindow::show()
{
return ShowWindow( hwnd, SW_SHOWNA );
}
BOOL ACandidateWindow::update()
{
//TRACEW( composition );
RECT rect;
GetClientRect( hwnd, & rect );
InvalidateRect( hwnd, & rect, FALSE );
return UpdateWindow( hwnd );
}
BOOL ACandidateWindow::redraw()
{
return RedrawWindow( hwnd, NULL, NULL, RDW_ERASENOW | RDW_UPDATENOW | RDW_ERASE | RDW_INTERNALPAINT | RDW_INVALIDATE );
}
BOOL ACandidateWindow::hide()
{
return ShowWindow( hwnd, SW_HIDE );
}
BOOL ACandidateWindow::move( int x, int y )
{
return MoveWindow( hwnd, x, y, szX, szY, TRUE );
}
LRESULT CALLBACK ACandidateWindow::WinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
HDC hdc;
PAINTSTRUCT ps;
ps.fErase = TRUE;
RECT rect;
POINT pos;
static POINTS cp;
switch( msg )
{
case WM_CREATE:
return 0;
case WM_PAINT:
hdc = BeginPaint( hwnd, & ps );
//SelectObject( hdc, ( HBRUSH )GetStockObject( GRAY_BRUSH ) );
SetTextColor( hdc, RGB( 255, 255, 255 ) );
SetBkMode( hdc, TRANSPARENT );
SelectObject( hdc, hfont );
GetClientRect( hwnd, & rect );
DrawIcon( hdc, 0, 0, hicon );
rect.left += 40;
//wchar_t kkk[256];kkk[255]=0;
//GetTextFaceW( hdc, 255, kkk );
//DrawTextW( hdc, kkk, -1, & rect, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
DrawText( hdc, composition.c_str(), -1, & rect, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
rect.left += avgCharWidth * 11;
DrawText( hdc, candidates.c_str(), -1, & rect, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
EndPaint( hwnd, & ps );
return 0;
/*
case WM_RBUTTONDOWN:
PostQuitMessage( 0 );
return 0;
*/
case WM_LBUTTONDOWN:
SetCapture( hwnd );
cp = MAKEPOINTS( lParam );
if( appHwnd )
SetFocus( appHwnd );
return 0;
case WM_LBUTTONUP:
ReleaseCapture();
return 0;
case WM_MOUSEMOVE:
//TRACEX( "WM_MOUSEMOVE\n" );
if( wParam == MK_LBUTTON )
{
GetCursorPos( & pos );
MoveWindow( hwnd, pos.x - cp.x, pos.y - cp.y, szX, szY, TRUE );
}
if( appHwnd )
SetFocus( appHwnd );
return 0;
case WM_SETFOCUS:
appHwnd = reinterpret_cast<HWND>( wParam );
return 0;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hwnd, msg, wParam, lParam );
}