我有一个标签控件作为主窗口的子窗口和一个编辑控件作为主窗口的子窗口。编辑控件在选项卡控件显示区域之外绘制。显示区域是GetClientRect(hTab, &rc); SendMessage(hTab, TCM_ADJUSTRECT, FALSE, (LPARAM)&rc);
获得的区域。即使编辑控件是标签控件的子项,效果也是相同的。我知道我可以创建一个静态控件作为选项卡控件的子项,其大小适合选项卡显示区域,并使编辑控件作为静态控件的子项,这将解决我的问题,但我想将编辑控件设置为选项卡控件或主窗口的子项使事情变得简单。如果没有选项卡显示区域外的编辑控件绘画,有没有办法做到这一点?
#include <windows.h>
#include <tchar.h>
#include <commctrl.h>
#pragma comment(linker, \
"\"/manifestdependency:type='Win32' "\
"name='Microsoft.Windows.Common-Controls' "\
"version='6.0.0.0' "\
"processorArchitecture='*' "\
"publicKeyToken='6595b64144ccf1df' "\
"language='*'\"")
#pragma comment(lib, "ComCtl32.lib")
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hTab, hEdit;
switch(msg)
{
case WM_CREATE:
{
INITCOMMONCONTROLSEX icex;
TCHAR *lpTabLabel[] = { TEXT("First"), TEXT("Second") };
TCITEM tci;
int i;
icex.dwICC = ICC_TAB_CLASSES;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&icex);
hTab = CreateWindowEx(0, WC_TABCONTROLW, 0, WS_VISIBLE | WS_CHILD,
0, 0, 0, 0, hwnd, 0, GetModuleHandle(0), 0);
for(i = 0; i < 2; i++)
{
tci.mask = TCIF_TEXT;
tci.pszText = lpTabLabel[i];
SendMessage(hTab, TCM_INSERTITEM, i, (LPARAM)&tci);
}
//hEdit = CreateWindowEx(0, WC_EDIT, TEXT("Text"), WS_VISIBLE | WS_CHILD,
//40, 40, 80, 40, hwnd, 0, GetModuleHandle(0), 0);
//Now hTab is parent of hEdit
hEdit = CreateWindowEx(0, WC_EDIT, TEXT("Text"), WS_VISIBLE | WS_CHILD,
40, 40, 80, 40, hTab, 0, GetModuleHandle(0), 0);
}
break;
case WM_SIZE:
SetWindowPos(hTab, 0, 0, 0, LOWORD(lParam), HIWORD(lParam), SWP_NOMOVE | SWP_NOZORDER);
break;
//This won't be sent to WndProc anymore since now hTab is parent of hEdit
//You could subclass hTab and handle WM_CTLCOLOREDIT in there to see the same effect as in first picture
case WM_CTLCOLOREDIT:
SetBkColor((HDC)wParam, GetSysColor(COLOR_DESKTOP));
return (LRESULT)GetSysColorBrush(COLOR_DESKTOP);
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wc = {0};
HWND hwnd;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TEXT("WinClass");
if(!RegisterClassEx(&wc))
return 0;
InitCommonControls();
hwnd = CreateWindowEx(0, TEXT("WinClass"), TEXT("Hello"), WS_OVERLAPPEDWINDOW, 400, 200, 400, 100, 0, 0, hInstance, 0);
if(!hwnd)
return 0;
ShowWindow(hwnd, nShowCmd);
while(GetMessage(&msg, 0, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
第二张图片显示选项卡是编辑的父级