Tech Support > Microsoft Windows > Development Resources > Can't get layered window to appear on the screen.
Can't get layered window to appear on the screen.
Posted by Tron Thomas on December 3rd, 2003


I am writing a program that creates a layered window, and I am unable
to make the layered window visible on the screen. If I remove the
code related to layering the window shows up just fine.

What am I doing wrong?

The entire source code for the program is as follows:

#include <windows.h>

namespace {

bool ClassRegistered(const char* pszClass, WNDCLASSEX* pWndClass);
int MessageLoop();
LRESULT WINAPI WinProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM
lParam);

}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int nCmdShow)
{
char szClassName[] = "LayeredWindow";
WNDCLASSEX wndClass;
if(false == ::ClassRegistered(szClassName, &wndClass)){
return 0;
}

char szTitle[] = "Layered Window";
HWND hWnd = ::CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST,
szClassName,
szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640,
480,
::GetDesktopWindow(), NULL, wndClass.hInstance, NULL);

if(NULL == hWnd){
return 0;
}

::SetLayeredWindowAttributes(hWnd, 0, 255, LWA_ALPHA);
::ShowWindow(hWnd, nCmdShow);
::UpdateWindow(hWnd);

int nResult = ::MessageLoop();
::UnregisterClass(szClassName, wndClass.hInstance);

return nResult;
}

namespace {

bool ClassRegistered
(
const char* pszClass,
WNDCLASSEX* pWndClass
)
{
WNDCLASSEX& wndClass(*pWndClass);

wndClass.cbSize = sizeof(wndClass);
wndClass.style = CS_CLASSDC;
wndClass.lpfnWndProc = reinterpret_cast<WNDPROC>(WinProc);
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = ::GetModuleHandle(NULL);
wndClass.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = pszClass;
wndClass.hIconSm = NULL;

return (FALSE != ::RegisterClassEx(&wndClass));
}

int MessageLoop()
{
MSG msg;
while(::GetMessage(&msg, NULL, 0, 0)){
::TranslateMessage(&msg);
:ispatchMessage(&msg);
}

return static_cast<int>(msg.wParam);
}

LRESULT WINAPI WinProc
(
HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam
)
{
if(WM_DESTROY == nMsg){
::PostQuitMessage(0);
return 0;
}

return :efWindowProc(hWnd, nMsg, wParam, lParam);
}

} // namespace


Similar Posts