📄 testgdi.cpp
字号:
rect1.bottom = 3 * (v_nHeightDisplay / 5);
rect2.left = 2 * (v_nWidthDisplay / 5);
rect2.top = 2 * (v_nHeightDisplay / 5);
rect2.right = v_nWidthDisplay;
rect2.bottom = v_nHeightDisplay;
GetTextMetricsW(hdc, &tm);
v_peg_text_y2 = (rect2.top - tm.tmHeight) / 2;
v_peg_text_y1 = v_peg_text_y2 + rect1.bottom;
if(hrgnText1){
VERIFY(DeleteObject((HGDIOBJ)hrgnText1) == TRUE);
hrgnText1 = NULL;
}
if(hrgnText2){
VERIFY(DeleteObject((HGDIOBJ)hrgnText2) == TRUE);
hrgnText2 = NULL;
}
if(hrgn){
VERIFY(DeleteObject((HGDIOBJ)hrgn) == TRUE);
hrgn = NULL;
}
VERIFY((hrgn = CreateRectRgnIndirect(&rect)) != NULL);
VERIFY((hrgn1 = CreateRectRgnIndirect(&rect1)) != NULL);
VERIFY((hrgn2 = CreateRectRgnIndirect(&rect2)) != NULL);
VERIFY(CombineRgn(hrgn, hrgn1, hrgn2, RGN_XOR) == COMPLEXREGION);
VERIFY(DeleteObject((HGDIOBJ)hrgn1) == TRUE);
VERIFY(DeleteObject((HGDIOBJ)hrgn2) == TRUE);
rect1.left = 0;
rect1.top = 3 * (v_nHeightDisplay / 5);
rect1.right = 2 * (v_nWidthDisplay / 5);
rect1.bottom = v_nHeightDisplay;
rect2.left = 3 * (v_nWidthDisplay / 5);
rect2.top = 0;
rect2.right = v_nWidthDisplay;
rect2.bottom = 2 * (v_nHeightDisplay / 5);
VERIFY((hrgnText1 = CreateRectRgnIndirect(&rect1)) != NULL);
VERIFY((hrgnText2 = CreateRectRgnIndirect(&rect2)) != NULL);
oldhbr = (HBRUSH) SelectObject(hdc,(HGDIOBJ) rghbr[0]);
VERIFY(SelectObject(hdc, (HGDIOBJ)rghbr[3]) != NULL);
SelectObject(hdc, oldhbr);
// VERIFY(SetTextColor(hdc, PALETTEINDEX(0)) != CLR_INVALID);
ReleaseDC(hWnd, hdc);
break;
case WM_PAINT:
fNeedToDisplayText = TRUE;
return DefWindowProc(hWnd, message, uParam, lParam);
case WM_TIMER:
//
// Do GDI operation
//
hdc = GetDC(hWnd);
oldhpn = (HPEN) SelectObject(hdc,(HGDIOBJ) hpnSolid);
if(fNeedToDisplayText){
SelectClipRgn(hdc, hrgnText1);
FillRect(hdc, &rect1, (HBRUSH)GetStockObject(WHITE_BRUSH));
DisplayText(hdc, v_wszPegasus1, 0, v_peg_text_y1-2, 0);
DisplayText(hdc, v_wszPegasus2, 0, v_peg_text_y1, 2);
SelectClipRgn(hdc, hrgnText2);
DisplayText(hdc, v_wszPegasus5, peg_text_x2, v_peg_text_y2-2, 0);
DisplayText(hdc, v_wszPegasus6, peg_text_x2, v_peg_text_y2, 2);
fNeedToDisplayText = FALSE;
}
// VERIFY(SelectClipRgn(hdc, hrgn) == COMPLEXREGION);
oldhbr = (HBRUSH) SelectObject(hdc,(HGDIOBJ) rghbr[0]);
if(v_nGdiOperations > 1){
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
fBlinking = TRUE;
SelectClipRgn(hdc, hrgnText2);
FillRect(hdc, &rect2, (HBRUSH)GetStockObject(WHITE_BRUSH));
SelectClipRgn(hdc, hrgnText1);
FillRect(hdc, &rect1, (HBRUSH)GetStockObject(WHITE_BRUSH));
DisplayMeasuringText(hdc, v_nGdiOperations, &fBlinking);
dwTime = GetTickCount();
}
SelectClipRgn(hdc, hrgn);
for(i=0; i<v_nGdiOperations; i++){
RECT r;
SelectObject(hdc,(HGDIOBJ) rghbr[rand(NUM_BRUSHES)]);
if(nTest == 0) {
randomize_rect(&r);
Ellipse(hdc,r.left,r.top,r.right,r.bottom);
} else if(nTest == 1) {
randomize_rect(&r);
Rectangle(hdc,r.left,r.top,r.right,r.bottom);
} else if(nTest == 2) {
POINT pts[3];
pts[0].x = rand(v_nWidthDisplay); pts[0].y = rand(v_nHeightDisplay);
pts[1].x = rand(v_nWidthDisplay); pts[1].y = rand(v_nHeightDisplay);
pts[2].x = rand(v_nWidthDisplay); pts[2].y = rand(v_nHeightDisplay);
Polygon(hdc,pts,3);
}
if(++nTest > 2) nTest = 0;
if((i % 100) == 99){
SelectClipRgn(hdc, hrgnText1);
DisplayMeasuringText(hdc, v_nGdiOperations, &fBlinking);
SelectClipRgn(hdc, hrgn);
}
}
if(v_nGdiOperations > 1){
dwTime = GetTickCount() - dwTime;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
SelectClipRgn(hdc, NULL);
SelectObject(hdc, (HGDIOBJ) oldhpn);
SelectObject(hdc, (HGDIOBJ)oldhbr);
ReleaseDC(hWnd, hdc);
wsprintfW(v_wszPegasus5, TEXT("%u milli-seconds for"), dwTime);
wsprintfW(v_wszPegasus6, TEXT("%d GDI Operations"), v_nGdiOperations);
fNeedToDisplayText = TRUE;
SetWindowLong(hWnd, GWL_STYLE, WS_OVERLAPPED | WS_VISIBLE);
SetWindowPos(hWnd,
NULL,
0, 0,
0, 0,
SWP_NOSIZE | SWP_NOZORDER);
hdc = GetDC(hWnd);
} else {
SelectClipRgn(hdc, NULL);
SelectObject(hdc, (HGDIOBJ) oldhpn);
SelectObject(hdc, (HGDIOBJ)oldhbr);
}
ReleaseDC(hWnd, hdc);
v_nGdiOperations = 1;
break;
case WM_DESTROY:
//
// Release GDI resources
//
for (i = 0; i < NUM_BRUSHES; i++){
if(rghbr[i]){
DeleteObject((HGDIOBJ)rghbr[i]);
}
}
if(hrgnText1){
VERIFY(DeleteObject((HGDIOBJ)hrgnText1) == TRUE);
hrgnText1 = NULL;
}
if(hrgnText2){
DeleteObject((HGDIOBJ) hrgnText2);
}
if(hrgn){
DeleteObject((HGDIOBJ) hrgn);
}
if(hpnDashed){
DeleteObject((HGDIOBJ) hpnDashed);
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, uParam, lParam);
}
return 0;
}
extern "C"
void
DisplayText(
HDC hdc,
LPWSTR lpwStr,
int x,
int y,
int yoffset
)
{
SIZE dimension;
int n;
int halfHeight;
n = lstrlenW(lpwStr);
GetTextExtentExPointW(hdc, lpwStr, n, 0, NULL, NULL, &dimension);
halfHeight = dimension.cy / 2;
if(ExtTextOut(hdc,
x + (v_nWidthDisplay / 5 - dimension.cx / 2),
y + halfHeight * yoffset,
0,
NULL,
lpwStr,
n,
NULL) == FALSE){
NKDbgPrintfW(TEXT("TESTGDI: ExtTextOut Fails Error = %d\r\n"), GetLastError());
}
}
extern "C"
void
DisplayMeasuringText(
HDC hdc,
int n,
BOOL *pfBlinking
)
{
TCHAR wszBuffer[50];
// if(*pfBlinking){
// SetTextColor(hdc, PALETTEINDEX(0));
// } else {
// SetTextColor(hdc, PALETTEINDEX(3));
// }
DisplayText(hdc, v_wszPegasus4, 0, v_peg_text_y1-2, 0);
wsprintfW(wszBuffer, TEXT("%d GDI Operations"), n);
DisplayText(hdc, wszBuffer, 0, v_peg_text_y1, 2);
*pfBlinking = !*pfBlinking;
}
extern "C"
BOOL
PerfDlgProc(
HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam
)
{
switch (msg) {
case WM_INITDIALOG:
CenterWindow(hWnd, GetWindow(hWnd, GW_OWNER));
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
GetWindowTextW(
GetDlgItem(hWnd, ID_EDIT),
v_wszPegasus3,
sizeof(v_wszPegasus3) / sizeof(TCHAR)
);
return EndDialog(hWnd, 1);
case IDCANCEL:
return EndDialog(hWnd, 0);
}
break;
}
return FALSE;
}
extern "C"
BOOL
CenterWindow(
HWND hwndChild,
HWND hwndParent
)
{
RECT rcChild, rcParent;
int cxChild, cyChild, cxParent, cyParent;
int cxScreen, cyScreen, xNew, yNew;
HDC hdc;
// Get the Height and Width of the child window
GetWindowRect(hwndChild, &rcChild);
cxChild = rcChild.right - rcChild.left;
cyChild = rcChild.bottom - rcChild.top;
// Get the Height and Width of the parent window
GetWindowRect(hwndParent, &rcParent);
cxParent = rcParent.right - rcParent.left;
cyParent = rcParent.bottom - rcParent.top;
// Get the display limits
hdc = GetDC(hwndChild);
cxScreen = GetDeviceCaps(hdc, HORZRES);
cyScreen = GetDeviceCaps(hdc, VERTRES);
ReleaseDC(hwndChild, hdc);
// Calculate new X position, then adjust for screen
xNew = rcParent.left + ((cxParent - cxChild) / 2);
if (xNew < 0)
{
xNew = 0;
}
else if ((xNew + cxChild) > cxScreen)
{
xNew = cxScreen - cxChild;
}
// Calculate new Y position, then adjust for screen
yNew = rcParent.top + ((cyParent - cyChild) / 2);
if (yNew < 0)
{
yNew = 0;
}
else if ((yNew + cyChild) > cyScreen)
{
yNew = cyScreen - cyChild;
}
// Set it, and return
return SetWindowPos(hwndChild,
NULL,
xNew, yNew,
0, 0,
SWP_NOSIZE | SWP_NOZORDER);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -