I'm trying to set an image to background to the main windows of my win32 application, I'm trying to set in in the main function wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
but I can't find the correct syntax??
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
is the correct syntax. To set an image as window background, you can use:
hbr=CreatePatternBrush(LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BGROUND)));
wcex.hbrBackground = bhr
If you prefer to control the drawing, you can handle WM_ERASEBKGND and draw your image in this function and then return non-zero for the DefWindowProc to ignore this message.
Related
I want to add a child window to a parent window,make the content of child window is below the parent window(Z order).just like compose two layer,not replacement.Because I want some UI element in parent window can be seen within the child window's area,but not cover all of the child window.The child window should be a window,so I can't just using parent window's UI system to implement the child window's UI.
Is there any api to achieve this?
I found something like "UpdateLayeredWindow",But I failed to create a layer child window,CreateWindowEx return NULL and GetLastError return 0.This code was copy from Windows's example.and the demo project (DirectCompositionLayeredChildWindow) works well.
HRESULT hr = S_OK;
// Register the window class.
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
wc.lpszClassName = L"DirectComposition Window Class";
RegisterClassEx(&wc);
// Creates the m_hMainWindow window.
HWND m_hMainWindow = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW| WS_EX_COMPOSITED, // Extended window style
wc.lpszClassName, // Name of window class
L"DirectComposition Layered Child Window Sample", // Title-bar string
WS_OVERLAPPED | WS_SYSMENU, // Top-level window
CW_USEDEFAULT, // Horizontal position
CW_USEDEFAULT, // Vertical position
1000, // Width
700, // Height
NULL, // Parent
NULL, // Class menu
GetModuleHandle(NULL), // Handle to application instance
NULL // Window-creation data
);
if (!m_hMainWindow)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
if (SUCCEEDED(hr))
{
ShowWindow(m_hMainWindow, SW_SHOWDEFAULT);
}
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WindowProc;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = L"DirectCompositionChildWindow-Child";
RegisterClassEx(&wcex);
// !!! m_hControlChildWindow is always NULL
HWND m_hControlChildWindow = CreateWindowEx(WS_EX_LAYERED, // Extended window style
wcex.lpszClassName, // Name of window class
NULL, // Title-bar string
WS_CHILD, // Child window
30,30, 100, 100, // Window will be resized via MoveWindow
m_hMainWindow, // Parent
NULL, // Class menu
GetModuleHandle(NULL), // Handle to application instance
NULL); // Window-creation data
if (!m_hControlChildWindow)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
ShowWindow(m_hControlChildWindow, nCmdShow);
UpdateWindow(m_hControlChildWindow);
This cannot be done, as explained here:
No part of a child window ever appears outside the borders of its parent window.
Also:
A child window is grouped with its parent in z-order.
In other words: You cannot create a child window that's either outside the parent window's client area, or underneath the parent window in Z-order.
I'm trying to create a window with the client size the same as the display resolution (1366 x 768).
For this, I'm using AdjustWindowRectEx to get the calculated width and height of the window to be created.
The first red circle in the top right is the actual display resolution that I want the client size of the window to have.
The second red circle is the width and height that AdjustWindowRectEx calculated the window to be created with (client size + borders and title bar).
The last red circle in the bottom is the actual client size that I got after the window has been created.
The height is not what I ask Windows to give me. I asked 768 and I got 749.
Is there some limit height the window can be created with? No matter how much I ask the height to be, it never give me more than 749.
Thanks!
In CreateWindow(), WM_GETMINMAXINFO information will be sent to confirm the size of the current form can be generated, you can refer to MINMAXINFO to view its specific structure. If the size of the created form is larger than its default setting, it will be overwritten by the default size. So it will be smaller than the size you set when it is created.
According to the WM_GETMINMAXINFO:
An application can override the defaults by setting the members of MINMAXINFO, and return zero if an application processes this message.
Here is the sample:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT,WPARAM,LPARAM);
static int max_right, max_bottom;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
HWND hwnd = 0;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
DEVMODEW display_mode;
if (EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &display_mode))
{
DWORD window_style = WS_CAPTION | WS_MINIMIZE | WS_SYSMENU | WS_VISIBLE;
RECT window_rect;
window_rect.right = display_mode.dmPelsWidth;
window_rect.bottom = display_mode.dmPelsHeight;
window_rect.left = window_rect.top = 0;
AdjustWindowRectEx(&window_rect, window_style, FALSE, 0);
max_right = window_rect.right - window_rect.left;
max_bottom = window_rect.bottom - window_rect.top;
HWND window_handle = CreateWindowEx(0, wndclass.lpszClassName,
TEXT("the hello program"),
window_style,
CW_USEDEFAULT, CW_USEDEFAULT,
max_right,
max_bottom,
NULL, NULL, hInstance, NULL);
ShowWindow(window_handle, iCmdShow);
UpdateWindow(window_handle);
if (window_handle)
{
RECT client_rect;
GetClientRect(window_handle, &client_rect);
}
}
while (GetMessageW(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
EndPaint(hwnd, &ps);
return 0;
case WM_GETMINMAXINFO:
((MINMAXINFO*)lParam)->ptMaxSize = { max_right,max_bottom };
((MINMAXINFO*)lParam)->ptMaxTrackSize = { max_right,max_bottom };
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
Use function GetSystemMetrics(...).
This function calculate size(resolution) of screen.
For X and Y coordinate, see my example to set variables(use in CreateWindowEx, where set a width and height of window.)Example:
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
I am very new to the win10 dev, and I am trying to create a win10 app which will create a floating window in all the monitors the system is connected to. So far I am successful in using GDI API to create window in my primary monitor, but it is not working on my secondary monitor, I cannot get why it is not working even when I am using the API as mentioned in the microsoft page.
Below is my code
// Register the window class.
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = DXGIDraw::WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT;
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wcex.lpszClassName = L"DemoApp";
RegisterClassEx(&wcex);
m_hwnd = CreateWindowEx(WS_EX_LAYERED,
L"DemoApp",
L"Demo App",
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_DESKTOP,
NULL,
HINST_THISCOMPONENT,
NULL
);
const int nWidth = GetSystemMetrics(SM_CXSCREEN);
const int nHeight = GetSystemMetrics(SM_CYSCREEN);
CImage img;
DISPLAY_DEVICE ddd;
ZeroMemory(&ddd, sizeof(ddd));
ddd.cb = sizeof(ddd);
for (int i = 0; EnumDisplayDevices(NULL, i, &ddd, 0); i++)
{
if (ddd.StateFlags & DISPLAY_DEVICE_ACTIVE) {
//Active monitor
}
if (ddd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
//Primary monitor
}
else {
//other types
}
}
//The following API supposed to work for all the monitors, but this API is not working. This is where I need attention.
HDC hdcScreen = CreateDC(ddd.DeviceName, NULL, NULL, NULL);
//But if I use below API I can get it working for the primary monitor only, still trying to understand why
//HDC hdcScreen = CreateDC(L"DISPLAY", NULL, NULL, NULL);
HDC hDC = CreateCompatibleDC(hdcScreen);
HBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, nWidth, nHeight);
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);
img.Draw(hDC, 0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight);
//Add layered window
BLENDFUNCTION blend = { 0 };
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptLocation = { 0, 0 };
SIZE szWnd = { nWidth, nHeight };
POINT ptSrc = { 0, 0 };
BOOL status1 = UpdateLayeredWindow(m_hwnd, hdcScreen, &ptLocation, &szWnd, hDC, &ptSrc, 0, &blend, ULW_ALPHA);
//Set window's position
BOOL status = SetWindowPos(m_hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
hr = m_hwnd ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
BOOL status;
status = ShowWindow(m_hwnd, SW_MAXIMIZE);
status = UpdateWindow(m_hwnd);
}
SelectObject(hDC, hBmpOld);
DeleteObject(hBmp);
DeleteDC(hDC);
ReleaseDC(NULL, hdcScreen);
}
EnumDisplayDevices is the wrong API for this job. You need to use EnumDisplayMonitors which gives you the monitor coordinates directly.
You need to retreive the bounding rectangle for the desired monitor within the virtual screen, and then you can position a window within that rectangle as desired. Use EnumDisplayMonitors() to get the rectangle of each moinitor. Or, if you have an HMONITOR handle to a specific monitor, you can use GetMonitorInfo().
I'm trying to create a borderless window that fills the screen with an OpenGL viewport. The problem is, when I set the window and viewport to be the same size as the desktop, the window flashes black on losing and gaining focus, on exit, and on creation. This may have to do with Windows setting the window to some type of true "fullscreen" mode. This doesn't seem to happen in other applications (DirectX?) that use this type of borderless fullscreen window.
I believe SFML has/had a variant of this problem.
There is a workaround: don't set the window to the exact size of the desktop. For instance, make the width of the window one pixel more than the width of the desktop.
Below is an SSCCE without error checking that shows what I'm talking about. This will create a borderless fullscreen window with a desktop-sized OpenGL viewport displaying an ugly green color (exit with AltF4). You can change the line #define FIX_BUG 0 to #define FIX_BUG 1 which just adds one extra pixel to the window's width to see what the behavior I want looks like.
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#pragma comment(lib, "OpenGL32.lib")
#define FIX_BUG 0
void MyRegisterClass(HINSTANCE hInstance);
HWND MyCreateWindow(HINSTANCE hInstance);
void MySetupOpenGLContext(HWND hWnd);
void MySetPixelFormat(HDC hDC);
RECT MyGetDesktopRect();
void MyLoadOpenGLFunctions();
#define APIENTRYP APIENTRY *
#define GLAPI extern
#define GL_COLOR 0x1800
typedef int GLint;
typedef int GLsizei;
typedef unsigned int GLenum;
typedef float GLfloat;
typedef void (APIENTRYP PFNGLVIEWPORTPROC) (GLint x, GLint y, GLsizei width, GLsizei height);
PFNGLVIEWPORTPROC glViewport;
typedef void (APIENTRYP PFNGLCLEARBUFFERFVPROC) (GLenum buffer, GLint drawbuffer, const GLfloat *value);
PFNGLCLEARBUFFERFVPROC glClearBufferfv;
static const char *windowClass = "CLASS";
static const char *windowTitle = "TITLE";
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
MyRegisterClass(hInstance);
const HWND hWnd = MyCreateWindow(hInstance);
MySetupOpenGLContext(hWnd);
ShowWindow(hWnd, nCmdShow);
HDC hDC = GetDC(hWnd);
MSG msg;
while (IsWindow(hWnd)) {
while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
const GLfloat color[] = {0.5f, 0.5f, 0.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, color);
SwapBuffers(hDC);
}
return 0;
}
void MyRegisterClass(HINSTANCE hInstance) {
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = &DefWindowProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH) (COLOR_WINDOWFRAME + 1);
wcex.lpszClassName = windowClass;
RegisterClassEx(&wcex);
}
HWND MyCreateWindow(HINSTANCE hInstance) {
RECT dRect = MyGetDesktopRect();
return CreateWindow(windowClass, windowTitle, WS_POPUP,
0, 0, dRect.right + FIX_BUG, dRect.bottom,
NULL, NULL, hInstance, NULL);
}
void MySetupOpenGLContext(HWND hWnd) {
HDC hDC = GetDC(hWnd);
MySetPixelFormat(hDC);
HGLRC hGLRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hGLRC);
MyLoadOpenGLFunctions();
RECT dRect = MyGetDesktopRect();
glViewport(0, 0, dRect.right, dRect.bottom);
}
void MySetPixelFormat(HDC hDC) {
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof pfd;
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
int format = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, format, &pfd);
}
RECT MyGetDesktopRect() {
RECT desktopRect;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktopRect);
return desktopRect;
}
void MyLoadOpenGLFunctions() {
glViewport = (PFNGLVIEWPORTPROC) GetProcAddress(GetModuleHandleA("OpenGL32.dll"), "glViewport");
glClearBufferfv = (PFNGLCLEARBUFFERFVPROC) wglGetProcAddress("glClearBufferfv");
}
Github Gist | Raw Text
Why does this happen? Is there a way to get the behavior I want with the window set to the exact size of the desktop?
My platform: Visual Studio 2013 / 64-bit Windows 8 / AMD Radeon HD6850.
Can anyone tell me how to create a ComboBox with a DropDownList (like the C# ComboBox Control with DropDownStyle = DropDownList) without resources and without MFC? I googled around 20 minutes but all codessamples I found created strange Controls for me (for example a plain area with a checkbox which cant be checked...). My code looks like this:
WNDCLASSEX wcex;
HWND hwnd;
HWND button;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "GLSample";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
if (!RegisterClassEx(&wcex))
return 0;
/* create main window */
hwnd = CreateWindowEx(0,
"GLSample",
"OpenGL Sample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
256,
256,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
button = CreateWindow("GLSample", "knopf", WS_CHILD | WS_VISIBLE, 200, 200, 150, 60, hwnd, NULL, hInstance, NULL);
You have to call CreateWindow() again, specifying lpClassName = combobox and dwStyle = CBS_DROPDOWNLIST.
You can edit its content by sending messages to it, using SendMessage()