public void GetSystemColorBrush() { // System color brushes are special- they'll always give the same value BrushHandle brush = GdiMethods.GetSystemColorBrush(SystemColor.MenuBar); long handle = (long)brush.DangerousGetHandle(); handle = handle & 0xFFFF00; // This changed in RS4 from C5 to BF for the last byte. Checking the first // bytes to make sure we're in the right ballpark. handle.Should().Be(0x100000); }
static unsafe LRESULT WindowProcedure(WindowHandle window, WindowMessage message, WPARAM wParam, LPARAM lParam) { switch (message) { case WindowMessage.Create: ModuleInstance hInstance = window.GetWindowLong(WindowLong.InstanceHandle); // Create the white-rectangle window against which the // scroll bars will be positioned. The child window ID is 9. hwndRect = Windows.CreateWindow("static", null, WindowStyles.Child | WindowStyles.Visible | (WindowStyles)StaticStyles.WhiteRectangle, ExtendedWindowStyles.None, 0, 0, 0, 0, window, (IntPtr)9, hInstance, IntPtr.Zero); for (int i = 0; i < 3; i++) { // The three scroll bars have IDs 0, 1, and 2, with // scroll bar ranges from 0 through 255. hwndScroll[i] = Windows.CreateWindow("scrollbar", null, WindowStyles.Child | WindowStyles.Visible | WindowStyles.TabStop | (WindowStyles)ScrollBarStyles.Veritcal, ExtendedWindowStyles.None, 0, 0, 0, 0, window, (IntPtr)i, hInstance, IntPtr.Zero); hwndScroll[i].SetScrollRange(ScrollBar.Control, 0, 255, false); hwndScroll[i].SetScrollPosition(ScrollBar.Control, 0, false); // The three color-name labels have IDs 3, 4, and 5, // and text strings “Red”, “Green”, and “Blue”. hwndLabel[i] = Windows.CreateWindow("static", szColorLabel[i], WindowStyles.Child | WindowStyles.Visible | (WindowStyles)StaticStyles.Center, ExtendedWindowStyles.None, 0, 0, 0, 0, window, (IntPtr)i + 3, hInstance, IntPtr.Zero); // The three color-value text fields have IDs 6, 7, // and 8, and initial text strings of “0”. hwndValue[i] = Windows.CreateWindow("static", "0", WindowStyles.Child | WindowStyles.Visible | (WindowStyles)StaticStyles.Center, ExtendedWindowStyles.None, 0, 0, 0, 0, window, (IntPtr)i + 6, hInstance, IntPtr.Zero); OldScroll[i] = hwndScroll[i].SetWindowProcedure(s_ScrollProcedure); hBrush[i] = Windows.CreateSolidBrush(crPrim[i]); } hBrushStatic = Windows.GetSystemColorBrush(SystemColor.ButtonHighlight); cyChar = Windows.GetDialogBaseUnits().cy; return(0); case WindowMessage.Size: int cxClient = lParam.LowWord; int cyClient = lParam.HighWord; rcColor = new RECT(cxClient / 2, 0, cxClient, cyClient); hwndRect.MoveWindow(0, 0, cxClient / 2, cyClient, true); for (int i = 0; i < 3; i++) { hwndScroll[i].MoveWindow((2 * i + 1) * cxClient / 14, 2 * cyChar, cxClient / 14, cyClient - 4 * cyChar, true); hwndLabel[i].MoveWindow((4 * i + 1) * cxClient / 28, cyChar / 2, cxClient / 7, cyChar, true); hwndValue[i].MoveWindow((4 * i + 1) * cxClient / 28, cyClient - 3 * cyChar / 2, cxClient / 7, cyChar, true); } window.SetFocus(); return(0); case WindowMessage.SetFocus: hwndScroll[idFocus].SetFocus(); return(0); case WindowMessage.VerticalScroll: int id = (int)((WindowHandle)lParam).GetWindowLong(WindowLong.Id); switch ((ScrollCommand)wParam.LowWord) { case ScrollCommand.PageDown: color[id] += 15; goto case ScrollCommand.LineDown; case ScrollCommand.LineDown: color[id] = Math.Min(255, color[id] + 1); break; case ScrollCommand.PageUp: color[id] -= 15; goto case ScrollCommand.LineUp; case ScrollCommand.LineUp: color[id] = Math.Max(0, color[id] - 1); break; case ScrollCommand.Top: color[id] = 0; break; case ScrollCommand.Bottom: color[id] = 255; break; case ScrollCommand.ThumbPosition: case ScrollCommand.ThumbTrack: color[id] = wParam.HighWord; break; default: return(0); } hwndScroll[id].SetScrollPosition(ScrollBar.Control, color[id], true); hwndValue[id].SetWindowText(color[id].ToString()); BrushHandle brush = Windows.CreateSolidBrush((byte)color[0], (byte)color[1], (byte)color[2]); brush.SetHandleAsInvalid(); // We'll dispose when we set the next brush window.SetClassBackgroundBrush(brush).Dispose(); window.InvalidateRectangle(rcColor, true); return(0); case WindowMessage.ControlColorScrollBar: return((IntPtr)hBrush[(int)((WindowHandle)lParam).GetWindowLong(WindowLong.Id)]); case WindowMessage.ControlColorStatic: id = (int)((WindowHandle)lParam).GetWindowLong(WindowLong.Id); if (id >= 3 && id <= 8) { DeviceContext dc = new DeviceContext((IntPtr)wParam, false); dc.SetTextColor(crPrim[id % 3]); dc.SetBackgroundColor(Windows.GetSystemColor(SystemColor.ButtonHighlight)); return(hBrushStatic.DangerousGetHandle()); } break; case WindowMessage.SystemColorChange: hBrushStatic = Windows.GetSystemColorBrush(SystemColor.ButtonHighlight); return(0); case WindowMessage.Destroy: window.SetClassBackgroundBrush(StockBrush.White).Dispose(); for (int i = 0; i < 3; i++) { hBrush[i].Dispose(); } Windows.PostQuitMessage(0); return(0); } return(Windows.DefaultWindowProcedure(window, message, wParam, lParam)); }
/// <summary> /// Sets the background brush for the window class. Returns the previous background brush. /// </summary> /// <param name="ownsHandle"> /// Whether or not the returned brush should own the handle. If true the brush handle /// will be deleted when disposed / finalized. /// </param> public static BrushHandle SetClassBackgroundBrush(this WindowHandle window, BrushHandle brush, bool ownsHandle = true) => new BrushHandle(window.SetClassLong(ClassLong.BackgroundBrush, brush.DangerousGetHandle()), ownsHandle);