IntPtr hwnd = IntPtr.Zero;
List<IntPtr> childHwnds = new List<IntPtr>();
...
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct WindowCompositionAttribData
{
public _WINDOWCOMPOSITIONATTRIB Attribute;
public int * Data;
public int SizeOfData;
}
[DllImport("ext-ms-win-ntuser-private-l1-1-1.dll", SetLastError = true)]
internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttribData data);
public static unsafe void SetWindowCloak(IntPtr handle)
{
int flag = 1;
var data = new WindowCompositionAttribData();
data.Attribute = _WINDOWCOMPOSITIONATTRIB.WCA_CLOAK; //17
data.SizeOfData = sizeof(int);
data.Data = & flag;
SetWindowCompositionAttribute(handle, ref data);
int result = Marshal.GetLastWin32Error();
if (result != 0)
{
throw new Win32Exception(result);
}
}
public static IntPtr EnableVisualStyles()
{
StringBuilder sbSystemDir = new StringBuilder(256);
GetSystemDirectory(sbSystemDir, 256);
ACTCTX actCtx = new ACTCTX();
actCtx.cbSize = Marshal.SizeOf(typeof(ACTCTX));
actCtx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID |
ACTCTX_FLAG_SET_PROCESS_DEFAULT |
ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;
actCtx.lpSource = "shell32.dll";
actCtx.wProcessorArchitecture = 0;
actCtx.wLangId = 0;
actCtx.lpAssemblyDirectory = sbSystemDir.ToString();
actCtx.lpResourceName = (IntPtr) 124;
IntPtr AcCtx = CreateActCtx(ref actCtx);
ActivateActCtx(AcCtx, out IntPtr cookie);
return cookie;
}
...
IntPtr coreWnd = ((ICoreWindowInterop) (object) CoreWindow.GetForCurrentThread()).WindowHandle;
IntPtr hInstance = Process.GetCurrentProcess().Handle;
string szAppName = "HelloWin";
WNDCLASS wndclass = new WNDCLASS();
wndclass.style = (uint)(ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw);
wndclass.lpfnWndProc = (WndProc)((hWnd, message, wParam, lParam) =>
{
IntPtr hdc;
PAINTSTRUCT ps;
RECT rect;
switch ((WM) message)
{
case WM.PAINT:
{
hdc = BeginPaint(hWnd, out ps);
GetClientRect(hWnd, out rect);
FillRect(hdc, ref rect, GetStockObject(StockObjects.WHITE_BRUSH));
EndPaint(hWnd, ref ps);
return IntPtr.Zero;
break;
}
case WM.DESTROY:
{
return IntPtr.Zero;
break;
}
case WM.CREATE:
{
return IntPtr.Zero;
break;
}
}
return DefWindowProc(hWnd, (WM) message, wParam, lParam);
});
wndclass.hInstance = hInstance;
wndclass.lpszMenuName = null;
wndclass.lpszClassName = szAppName;
var regResult = RegisterClass(ref wndclass);
EnableVisualStyles();
hwnd = CreateWindowEx(
(uint)(WindowStylesEx.WS_EX_LAYERED | WindowStylesEx.WS_EX_COMPOSITED),
(ushort) new IntPtr((int)(uint) regResult),
"The Hello Program", // window caption
(uint) WindowStyles.WS_CHILD, // window style
0, // initial x position
0, // initial y position
500, // initial x size
500, // initial y size
coreWnd, // parent window handle
IntPtr.Zero, // window menu handle
hInstance, // program instance handle
IntPtr.Zero); // creation parameters
var ctls = new INITCOMMONCONTROLSEX(CommonControls.ICC_STANDARD_CLASSES | CommonControls.ICC_PROGRESS_CLASS);
InitCommonControlsEx(ref ctls);
ShowWindow(hwnd, (int) ShowWindowCommands.Normal);
SetWindowCloak(hwnd);
IntPtr hwndButton = CreateWindow(
"BUTTON", // Predefined class
"Hello from Comctl32!", // Button text
(uint) WindowStyles.WS_VISIBLE | (uint) WindowStyles.WS_CHILD, // Styles
0, // x position
0, // y position
200, // Button width
100, // Button height
hwnd, // Parent window
IntPtr.Zero, // No menu.
hInstance,
IntPtr.Zero); // Pointer not needed.
IntPtr hwndProg = CreateWindow(
"msctls_progress32", // Predefined class
null, // text
(uint) WindowStyles.WS_VISIBLE | (uint) WindowStyles.WS_CHILD | 0x08, // Styles
0, // x position
110, // y position
200, // width
35, // height
hwnd, // Parent window
IntPtr.Zero, // No menu.
hInstance,
IntPtr.Zero); // Pointer not needed.
SendMessage(hwndProg, (int)(WM.USER + 10), 1, 0); //Starts ProgressBar animation
childHwnds.Add(hwndButton);