It's been a while since I've been on this website. I've been doing a lot of Windows programming recently though, and I ran into a problem with some code that I've written to create a window.
I've tried running tests with the Window Procedure, such as catching WM_CREATE and WM_MOUSEMOVE and a few others, but of the one's that I've tested, only WM_CREATE has done anything (that was all I've tried as a test, I've been up for a few days and am blanking on anything else at the moment). The tests have all created a window successfully, but it won't close through the normal means of either clicking the 'X' button in the top right corner or by pressing Alt+F4 (nor do any of the other default buttons do anything), in order to close it I've had to go into the Window's Task Manager and shut it down through the Processes tab. Also, when the window is created, I cannot resize it, move it, or do just about anything with it besides watch it sit there. I am using the Microsoft Visual Studio Professional 2010 IDE with the default x64 compiler (not x86_x64 as my computer is 64-bit). My code for the implemented window class is below as is the code for the test. Any help would be much appreciated. Thanks!
The Test code:
#include <Windows.h>
#include "window.h"
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){
Window win;
win.SetSize(dimension2d<unsigned int>(1024, 768));
if(!win.IsCreateWindow()){
MessageBox(NULL, L"Could not create Window", L"ERROR", MB_OK | MB_IConerror);
return 1;
}
HWND hWnd = reinterpret_cast<HWND>(win.GetWindow());
assert(hWnd);
win.DisplayWindow();
MSG msg;
bool notDone = true;
while(notDone){
if(PeekMessage(&msg, hWnd, 0U, 0U, PM_REMOVE)){
if(msg.message == WM_QUIT){
MessageBox(hWnd, L"Quit message received", L"QUIT", MB_OK | MB_ICONINFORMATION);
notDone = false;
}
}else{
}
}
return 0;
}
The implemented Window code:
#include <Windows.h>
#include "window.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch(uMsg){
case WM_MOUSEMOVE:
MessageBox(hWnd, L"MOUSE MOVE", L"MESSAGE RECEIVED", MB_OK | MB_ICONINFORMATION);
return DefWindowProc(hWnd, uMsg, wParam, lParam);
case WM_CREATE:
MessageBox(hWnd, L"Created Window", L"Message Received", MB_OK | MB_ICONINFORMATION);
return DefWindowProc(hWnd, uMsg, wParam, lParam);
case WM_CLOSE:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
overdrive::Window::Window(): m_WindowSize(1024, 768), m_WindowPos(0,0){
m_bWindowExists = false;
m_bDisplayWindow = false;
m_pWindowTitle = L"Overdrive Engine";
m_pWindowClass = L"OverdriveEngine";
m_pWindowIcon = IDI_APPLICATION;
m_pWindowCursor = IDC_CROSS;
m_pWindowHandle = (void*)0;
m_pParentWindow = (void*)0;
m_WindowStyle = WS_OVERLAPPEDWINDOW;
m_WindowStyleEx = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
}
overdrive::Window::~Window(){
IsDestroyWindow();
}
void overdrive::Window::SetTitle(const wchar_t* pTitle){
m_pWindowTitle = pTitle;
if(m_bWindowExists){
SetWindowText(reinterpret_cast<HWND>(m_pWindowHandle), m_pWindowTitle);
}
}
void overdrive::Window::SetTemporaryTitle(const wchar_t* pTempTitle){
if(m_bWindowExists){
SetWindowText(reinterpret_cast<HWND>(m_pWindowHandle), pTempTitle);
}
}
void overdrive::Window::SetSize(overdrive::dimension2d<unsigned int> windowSize){
m_WindowSize = windowSize;
if(m_bWindowExists){
SetWindowPos(reinterpret_cast<HWND>(m_pWindowHandle), 0, 0,0,m_WindowSize.Width(), m_WindowSize.Height(), SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
void overdrive::Window::SetIcon(int resNum){
SetIcon(MAKEINTRESOURCE(resNum));
}
void overdrive::Window::SetIcon(const wchar_t* resLocation){
m_pWindowIcon = resLocation;
if(m_bWindowExists){
SendMessage(reinterpret_cast<HWND>(m_pWindowHandle), WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(GetModuleHandle(NULL), m_pWindowIcon));
}
}
void overdrive::Window::SetCursor(int resNum){
this->SetCursor(MAKEINTRESOURCE(resNum));
}
void overdrive::Window::SetCursor(const wchar_t* cursorLocation){
::SetCursor((HCURSOR)LoadCursor(GetModuleHandle(NULL), cursorLocation));
}
void overdrive::Window::SetPosition(overdrive::position2d<unsigned int> pos){
m_WindowPos = pos;
if(m_bWindowExists){
SetWindowPos(reinterpret_cast<HWND>(m_pWindowHandle), 0, m_WindowPos.getX(), m_WindowPos.getY(), 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
void overdrive::Window::SetParentWindow(void* pParentWindow){
HWND parent = reinterpret_cast<HWND>(pParentWindow);
if(!parent)return;
if(m_bWindowExists){
HWND hWnd = reinterpret_cast<HWND>(m_pWindowHandle);
if(hWnd == parent)return;
SetParent(hWnd, parent);
}
}
void overdrive::Window::SetWindowStyle(unsigned long style, unsigned long extendedStyle){
m_WindowStyle = style;
m_WindowStyleEx = extendedStyle;
if(m_bWindowExists){
HWND hWnd = reinterpret_cast<HWND>(m_pWindowHandle);
LONG lStyle = GetWindowLongPtr(hWnd, GWL_STYLE);
LONG lStyleEx = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
lStyle &= ~(lStyle);
lStyleEx &= ~(lStyleEx);
lStyle &= m_WindowStyle;
lStyleEx &= m_WindowStyleEx;
SetWindowLongPtr(hWnd, GWL_STYLE, lStyle);
SetWindowLongPtr(hWnd, GWL_EXSTYLE, lStyleEx);
SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
}
bool overdrive::Window::IsCreateWindow(){
if(m_bWindowExists)return true;
WNDCLASSEX WindowClass;
WindowClass.cbSize = sizeof(WNDCLASSEX);
WindowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
WindowClass.lpfnWndProc = (WNDPROC)&WndProc;
WindowClass.cbWndExtra = 0;
WindowClass.cbClsExtra = 0;
WindowClass.hInstance = GetModuleHandle(NULL);
WindowClass.hIcon = (HICON)LoadIcon(GetModuleHandle(NULL), m_pWindowIcon);
WindowClass.hIconSm = (HICON)LoadIcon(GetModuleHandle(NULL), m_pWindowIcon);
WindowClass.hCursor = (HCURSOR)LoadCursor(GetModuleHandle(NULL), m_pWindowCursor);
WindowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WindowClass.lpszMenuName = NULL;
WindowClass.lpszClassName = m_pWindowClass;
if(!RegisterClassEx(&WindowClass)){
MessageBox(NULL, L"Could not register Window Class.", L"Error", MB_OK | MB_IConerror);
return false;
}
RECT windowSize;
windowSize.top = (long)0;
windowSize.left = (long)0;
windowSize.right = (long)m_WindowSize.Width();
windowSize.bottom = (long)m_WindowSize.Height();
if(!AdjustWindowRectEx(&windowSize, m_WindowStyle, false, m_WindowStyleEx)){
UnregisterClass(m_pWindowClass, GetModuleHandle(NULL));
MessageBox(NULL, L"Could not adjust window rect", L"Error", MB_OK | MB_IConerror);
return false;
}
HWND parent = reinterpret_cast<HWND>(m_pParentWindow);
if(!parent)parent = NULL;
HWND hWnd = CreateWindowEx( m_WindowStyleEx,
m_pWindowClass,
m_pWindowTitle,
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | m_WindowStyle,
m_WindowPos.getX(), m_WindowPos.getY(),
windowSize.right-windowSize.left,
windowSize.bottom-windowSize.top,
parent,
NULL,
WindowClass.hInstance,
NULL);
assert(false && L"Could not create window.");
if(!hWnd){
UnregisterClass(m_pWindowClass, GetModuleHandle(NULL));
return false;
}
m_pWindowHandle = reinterpret_cast<void*>(hWnd);
m_bWindowExists = true;
if(m_bDisplayWindow)DisplayWindow();
return true;
}
bool overdrive::Window::IsDestroyWindow(){
if(!m_bWindowExists)return true;
HWND hWnd = reinterpret_cast<HWND>(m_pWindowHandle);
if(!DestroyWindow(hWnd)){
return false;
}
m_pWindowHandle = (void*)0;
m_bWindowExists = false;
m_bDisplayWindow = false;
return true;
}
void* overdrive::Window::GetWindow(){
return m_pWindowHandle;
}
void overdrive::Window::DisplayWindow(){
m_bDisplayWindow = true;
if(!m_bWindowExists)return;
HWND hWnd = reinterpret_cast<HWND>(m_pWindowHandle);
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
}
overdrive::dimension2d<unsigned int> overdrive::Window::GetWindowSize(){
return m_WindowSize;
}
const wchar_t* overdrive::Window::GetTitle(){
return m_pWindowTitle;
}
Again, thank you for any help!
This post has been edited by psyking: 26 August 2012 - 07:42 PM

New Topic/Question
Reply




MultiQuote







|