What is the best way to prevent my Window Opening with a White Background?

JWG 146 Reputation points
2023-03-26T15:02:55.4466667+00:00

I tried showing the window, only after the paint message was received, painting the window black, but the window still appears white, before turning black.

How can I make sure the window always opens black, and a white background is never seen, even for a millisecond?

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,504 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,654 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Castorix31 85,956 Reputation points
    2023-03-29T10:45:55.1366667+00:00

    If I do a basic test with a black background (GetStockObject(BLACK_BRUSH)), I don't see any white background :

    #include <windows.h>
    #include <tchar.h>
    
    #pragma comment(linker,"\"/manifestdependency:type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
    
    HINSTANCE hInst;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    int nWidth = 600, nHeight = 400;
    #define IDC_STATIC 10
    #define IDC_BUTTON 11
    
    int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
    {
    
    	hInst = hInstance;
    	WNDCLASSEX wcex =
    	{
    		sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInst, LoadIcon(NULL, IDI_APPLICATION),
    		LoadCursor(NULL, IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH), NULL, TEXT("WindowClass"), NULL,
    	};
    	if (!RegisterClassEx(&wcex))
    		return MessageBox(NULL, TEXT("Cannot register class !"), TEXT("Error"), MB_ICONERROR | MB_OK);
    	int nX = (GetSystemMetrics(SM_CXSCREEN) - nWidth) / 2, nY = (GetSystemMetrics(SM_CYSCREEN) - nHeight) / 2;
    	HWND hWnd = CreateWindowEx(0, wcex.lpszClassName, TEXT("Test"), WS_OVERLAPPEDWINDOW, nX, nY, nWidth, nHeight, NULL, NULL, hInst, NULL);
    	if (!hWnd)
    		return MessageBox(NULL, TEXT("Cannot create window !"), TEXT("Error"), MB_ICONERROR | MB_OK);
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    	UpdateWindow(hWnd);
    	MSG msg;
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HWND hWndButton = NULL, hWndStatic = NULL;
    	int wmId, wmEvent;
    	switch (message)
    	{
    	case WM_CREATE:
    	{
    		hWndButton = CreateWindowEx(0, L"Button", L"Click", WS_CHILD | WS_VISIBLE | BS_PUSHLIKE, 100, 60, 60, 32, hWnd, (HMENU)IDC_BUTTON, hInst, NULL);
    		return 0;
    	}
    	break;
    	case WM_COMMAND:
    	{
    		wmId = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		switch (wmId)
    		{
    		case IDC_BUTTON:
    		{
    			if (wmEvent == BN_CLICKED)
    			{
    				Beep(1000, 10);
    			}
    		}
    		break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    	}
    	break;
    	case WM_DESTROY:
    	{
    		PostQuitMessage(0);
    		return 0;
    	}
    	break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    
    

    BackgroundTest

    1 person found this answer helpful.

  2. JWG 146 Reputation points
    2023-04-20T14:11:21.8833333+00:00

    Thanks to everyone who made an input. I have three options which are working.

    1. Moving the window off-screen and bringing it back after painting. (Not an ideal approach in my opinion)
    2. Using a bitmap with SendMessage(hWnd, WM_SETREDRAW, NULL, NULL); before showing the window. (Can't seem to draw on this)
    3. Using Microsoft's Direct2D Drawing Example. (Not sure why this works, as opposed the 'normal' approach, but it gives me what I want. Except for the memory leaks despite the objects being released).
    4. win32window0
    5. winDesktopApp
    6. winDesktopApp04

    I'll settle for the third, and probably start a new thread on preventing those memory leaks. Thanks again for the help offered, guys.

    1 person found this answer helpful.
    0 comments No comments

  3. Xiaopo Yang - MSFT 12,726 Reputation points Microsoft Vendor
    2023-03-27T01:37:14.0366667+00:00

    Hello,

    Welcome to Microsoft Q&A!

    According to Class Background Brush, Setting the hbrBackground member of the WNDCLASSEX structure to NULL makes the application paint its own background in the WM_ERASEBKGND message or WM_PAINT message.

    A realistic sample Flicker-Free Displays Using an Off-Screen DC.

    Thank you.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. David Warner 0 Reputation points
    2023-04-01T19:05:08.37+00:00

    If you're referring to the white background that appears when you open a new window in Windows 10, there are a few ways you can prevent it:

    1. Change the default app mode: You can switch your Windows 10 default app mode from "Light" to "Dark". This will change the default white background to a black or dark gray background, which may be easier on the eyes.

    To do this, go to Settings > Personalization > Colors, and select "Dark" under the "Choose your default app mode" option.

    1. Use a third-party app or theme: There are several third-party apps and themes available that allow you to customize the appearance of Windows 10, including the background color of new windows. Some popular options include Stardock's WindowBlinds, Rainmeter, and Nexus Dock.
    2. Adjust your display settings: You can adjust your display settings to reduce the brightness of your screen, which may make the white background less jarring. Go to Settings > System > Display, and use the "Brightness and color" settings to adjust your screen brightness.
    3. Install a blue light filter app: You can install a blue light filter app, such as f.lux or Windows 10's built-in Night Light feature, to reduce the amount of blue light emitted by your screen. This can help reduce eye strain and make the white background less harsh.

    By trying these methods, you can find the best way to prevent the white background in Windows 10 from causing eye strain or discomfort.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.