What is wrong with this context creation? - c++

I would like to create a render context to use opengl.
The wglMakeCurrent(hdc, hrc) is returning true.
But for some reason OpenGL functions are ignored.
Here is the code for context creation:
int pixelFormat;
hdc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
pixelFormat = ChoosePixelFormat(hdc,&pfd);
SetPixelFormat(hdc,pixelFormat,&pfd);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
And the window creation:
hinst = GetModuleHandle(NULL);
WNDCLASSEX wnd;
memset(&wnd, 0, sizeof(WNDCLASSEX));
wnd.hInstance = hinst;
wnd.style = CS_HREDRAW | CS_VREDRAW;
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.lpszClassName = "__myGLApp__";
wnd.lpfnWndProc = WndProc;
RegisterClassEx(&wnd);
hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,
"__myGLApp__",
"valami",
WS_VISIBLE,
xpos,
ypos,
width,
height,
NULL,
NULL,
hinst,
NULL);
Where am going I wrong?

you can use the glGetError() function to get an error code. You can also get a textual description with the functions gluErrorString() and gluErrorUnicodStringEXT() from the utility library. Something like this:
char *message;
message = gluErrorString(glGetError());
printf("OpenGL error: %s\n", message);

Related

How to activate a win32 menu bar when using glut for OpenGL?

I bind the glut window with the win32 window as its subwindow. But then I want to use the win32 menu bar instead of the menu bar provided by glut. However, if I use glutMainLoop(), dispatchMessage() can't get invoked. And if I write codes as win32: while(GetMessage(&nMsg, NULL, 0, 0)){}, everythin in glut can't be implemented.
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = 0;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindowW(szAppName, // window class name
TEXT("The Hello Program"), // window caption
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT,// initial x position
CW_USEDEFAULT,// initial y position
CW_USEDEFAULT,// initial x size
CW_USEDEFAULT,// initial y size
NULL, // parent window handle
NULL, // window menu handle
0, // program instance handle
NULL); // creation parameters
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(800, 600);
glutCreateWindow("easy geometry tool with typed input");
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
RegisterClass(&wc);
HWND hWnd = GetActiveWindow();
HWND hwndGLFW = GetActiveWindow();
SetWindowLong(hwndGLFW, GWL_STYLE, WS_VISIBLE);
MoveWindow(hwndGLFW, 0, 0, 1150, 800, TRUE);
SetParent(hwndGLFW, hwnd);
HINSTANCE hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU1));
SetMenu(hWnd, hMenu);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
Init();
glutDisplayFunc(myDisplay);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutSpecialFunc(keyboardSpecial);
createGlutMenu();
MSG nMsg = { 0 };
while (GetMessage(&nMsg, NULL, 0, 0)) {
glutMainLoop();
TranslateMessage(&nMsg);
DispatchMessage(&nMsg);
};
If anyone who can tell me the solution to my problem, thanks a lot!!!!!!

How to draw partially transparent bitmap inside a fully transparent window?

I have a fully transparent window:
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEX);
wClass.cbWndExtra = NULL;
wClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0));
wClass.hCursor = LoadCursor(0, IDC_ARROW);
wClass.hIcon = LoadIcon(0, IDI_APPLICATION);
wClass.hIconSm = LoadIcon(0, IDI_APPLICATION);
wClass.hInstance = GetModuleHandleA(0);
wClass.lpfnWndProc = &Overlay::WinProc;
wClass.lpszClassName = overlay_window_name.c_str();
wClass.lpszMenuName = overlay_window_name.c_str();
wClass.style = CS_VREDRAW | CS_HREDRAW;
cid = RegisterClassEx(&wClass);
DWORD ex_style = 0;
DWORD style = 0;
ex_style = WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED;
style = WS_POPUP;
m_overlay_window = CreateWindowEx(ex_style, overlay_window_name.c_str(), overlay_window_name.c_str(), style, 0, 0, 0, 0, 0, 0, 0, this);
SetLayeredWindowAttributes(m_overlay_window, 0, 0, LWA_ALPHA);
SetLayeredWindowAttributes(m_overlay_window, 0, RGB(0, 0, 0), LWA_COLORKEY);
I am creating swap chain:
DXGI_SWAP_CHAIN_DESC desc{};
desc.Windowed = TRUE;
desc.BufferCount = 2;
desc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
desc.SampleDesc.Count = 1; //multisampling setting
desc.SampleDesc.Quality = 0; //vendor-specific flag
desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
desc.OutputWindow = hwnd;
D3D_FEATURE_LEVEL levels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_11_1
};
D3D_FEATURE_LEVEL feature_level;
UINT device_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
HRESULT hr = D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_HARDWARE,
nullptr,
device_flags,
levels,
ARRAYSIZE(levels),
D3D11_SDK_VERSION,
&desc,
&m_swap_chain,
&m_device,
&feature_level,
&m_context
);
and then I want to draw half-transparent bitmap(through which I can see content of other windows):
m_render_target->DrawBitmap(m_bitmap, rect, 0.5, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR);
but this bitmap always not transparent. Only thing is going for him he becomes darker. May be something wrong with window or swap chain Im creating?

Transparent bitmap 32bit as window background

How can I set a graphic in bmp 32bit format as window background with TRANSPARENCY? when i use bmp 28bit everything works but then there is no transparency. When i use 32bit bitmap with alpha the background is not rendered.
Application is in c ++ winapi.
bool ApplicationInit(HINSTANCE hinstance)
{
//HBITMAP hbmp = LoadBitmap(hinstance, MAKEINTRESOURCE(IDB_LAUNCHER_BG));
HBITMAP hbmp = (HBITMAP)LoadImage(hinstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
HBRUSH hbr = CreatePatternBrush(hbmp);
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = LauncherWndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hinstance;
wcx.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(IDI_ICON));
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = hbr;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = szWindowClass;
return RegisterClassEx(&wcx);
}
please help :)

C++, GDI+, Load Bitmap and use UpdateLayeredWindow to show Bitmap

Community, I have a small problem and I need help.
I want to load a bitmap and want to show this with UpdatelayerdWindow. I have written the following source code. But nothing appears.
When I use the BitBlt function, the picture appears.
Where do I have here the error in my source code?
ATOM SplashRegisterClass(HINSTANCE hInstance) {
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 = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
void ShowBitMap(HWND hwnd, HINSTANCE hInstance) {
int nuXPos = 0;
int nuYPos = 0;
int width = 0;
int height = 0;
BITMAP bm;
hbmpSplash = (HBITMAP)::LoadImage(hInstance, L"software.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hbmpSplash == NULL)
{
::MessageBox(NULL, __T("Image not Found"), __T("Error"), MB_OK);
}
GetObject(hbmpSplash, sizeof(bm), &bm);
SIZE size = { bm.bmHeight, bm.bmWidth };
width = bm.bmWidth;
height = bm.bmHeight;
POINT ptZero = { 0 };
HMONITOR hmonPrimary = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO monitorinfo = { 0 };
monitorinfo.cbSize = sizeof(monitorinfo);
GetMonitorInfo(hmonPrimary, &monitorinfo);
const RECT & rcWork = monitorinfo.rcWork;
nuXPos = rcWork.left + (rcWork.right - rcWork.left) / 2;
nuYPos = rcWork.top + (rcWork.bottom - rcWork.top) / 2;
nuXPos = nuXPos - (175 / 2);
nuYPos = nuYPos - (170 / 2);
HDC hdcScreen = GetDC(NULL);
HDC hdcneu = CreateCompatibleDC(hdcScreen);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpSplash);
POINT position = { nuXPos, nuYPos };
BLENDFUNCTION blend = { 0 };
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
//Next line only for test!
//::BitBlt(hdcScreen, nuXPos, nuYPos, width, height, hdcMem, 0, 0, SRCCOPY);
//Show the Bitmap
if (!UpdateLayeredWindow(splashOwner, hdcScreen, &position, &size, hdcMem, NULL, RGB(0, 0, 0), &blend, ULW_ALPHA))
{
//int LastError = GetLastError();
}
SelectObject(hdcMem, hbmpOld);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdcScreen);
}
HWND CreateSplashWindow(HINSTANCE hInstance, int nCmdShow) {
SplashRegisterClass(hInstance);
splashOwner = CreateWindow(szWindowClass, NULL, WS_POPUP, 0, 0, 100, 100, NULL, NULL, hInstance, NULL);
if (!splashOwner)
{
::MessageBox(NULL, __T("Error"), __T("Error"), MB_OK);
}
ShowBitMap(splashOwner, hInstance);
return CreateWindowEx(WS_EX_LAYERED, szWindowClass, 0, WS_POPUP, 0, 0, 100, 100, splashOwner, NULL, hInstance, NULL);
}
I want delete everything when the main programme is running. Can I make with this function?
void DeleteSplashWindow(){
ReleaseDC(splashOwner, 0);
}
Thanks for help!
hbmpSplash = (HBITMAP)::LoadImage(hInstance, L"software.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
This function fails, it should give an error. Change it to
hbmpSplash = (HBITMAP)::LoadImage(0, L"software.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
When creating layered window you must call SetLayeredWindowAttributes. Example
HWND CreateSplashWindow(HINSTANCE hInstance, int nCmdShow) {
...
HWND hwnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass,
0, WS_POPUP, 0, 0, 100, 100,
splashOwner, NULL, hInstance, NULL);
SetLayeredWindowAttributes(hwnd, transparentColor, alpha, LWA_COLORKEY | LWA_ALPHA);
return hwnd;
}
Thanks for the help, I arrive myself to the solution. 1. I forgot ShowWindow(hwnd,SW_SHOW); and
2.void DeleteSplashWindow(){
ReleaseDC(splashOwner, NULL);
ShowWindow(splashOwner, NULL);
}

How can I capture a transparent or translucent window in win32

I want to have a transparent window which has only borders and than capture it (and see all the windows behind it - since its transparent) to bitmap.
I managed to create the transparent window but when I try to capture it than it ceases to be transparent and I get it original background.
This is what I have so far,
creating the window:
//registering the window class with white backgournd
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = ScreenCaptureROIWinProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = _WPModule.GetHInstance();
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hbrBackground = CreateSolidBrush(0x00FFFFFF);//white brush;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = g_lpszClassName;
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
//creating a layered window
m_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_LAYERED,g_lpszClassName, NULL,
WS_THICKFRAME, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL,
NULL, hInstance(), NULL);
// Set white brushed sections of the window to be transparent
SetLayeredWindowAttributes(m_hWnd, 0x00FFFFFF, 0, LWA_COLORKEY);
capturing the window:
RECT ROIRect;
GetWindowRect(m_hWnd,ROIRect);
HDC hdcScreen = GetDC(m_hWnd);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
BitBlt(
hdcMem,
0,
0,
ROIRect.right - ROIRect.left, //width of window
ROIRect.bottom - ROIRect.top, //height of window
hdcScreen,
0,
0,
SRCCOPY);