CreateWindow
-
Hi, In my application I need to create an empty window just to receive messages and treat them. I wrote the following code: HINSTANCE hApi = (HINSTANCE) GetModuleHandle(NULL); if (! hApi) ...// manage error CHAR * WindowClassName = "MyWindowClass"; WNDCLASS WindowClassInfo; if (! GetClassInfo(hApi, WindowClassName, &WindowClassInfo)) { WindowClassInfo.hCursor = NULL; WindowClassInfo.hIcon = NULL; WindowClassInfo.lpszMenuName = NULL; WindowClassInfo.lpszClassName = WindowClassName; WindowClassInfo.hbrBackground = NULL; WindowClassInfo.hInstance = hApi; WindowClassInfo.style = CS_OWNDC | CS_VREDRAW | S_HREDRAW; WindowClassInfo.lpfnWndProc = (WNDPROC) MyProc; WindowClassInfo.cbClsExtra = 0; WindowClassInfo.cbWndExtra = 0; if (! RegisterClass(&WindowClassInfo)) ...//manage error } HWND hWnd = CreateWindow(WindowClassName, "MyWindow", WS_OVERLAPPEDWINDOW, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hApi, NULL); if (! hWnd) ...//manage error But, unfortunately, CreateWindow return NULL and GetLastError return 0 !!! Did anybody could help me ? Thanks in advance. Robin.
-
Hi, In my application I need to create an empty window just to receive messages and treat them. I wrote the following code: HINSTANCE hApi = (HINSTANCE) GetModuleHandle(NULL); if (! hApi) ...// manage error CHAR * WindowClassName = "MyWindowClass"; WNDCLASS WindowClassInfo; if (! GetClassInfo(hApi, WindowClassName, &WindowClassInfo)) { WindowClassInfo.hCursor = NULL; WindowClassInfo.hIcon = NULL; WindowClassInfo.lpszMenuName = NULL; WindowClassInfo.lpszClassName = WindowClassName; WindowClassInfo.hbrBackground = NULL; WindowClassInfo.hInstance = hApi; WindowClassInfo.style = CS_OWNDC | CS_VREDRAW | S_HREDRAW; WindowClassInfo.lpfnWndProc = (WNDPROC) MyProc; WindowClassInfo.cbClsExtra = 0; WindowClassInfo.cbWndExtra = 0; if (! RegisterClass(&WindowClassInfo)) ...//manage error } HWND hWnd = CreateWindow(WindowClassName, "MyWindow", WS_OVERLAPPEDWINDOW, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hApi, NULL); if (! hWnd) ...//manage error But, unfortunately, CreateWindow return NULL and GetLastError return 0 !!! Did anybody could help me ? Thanks in advance. Robin.
Everything seems OK. The only possible problem I can think of is that
MyProc
is not properly declared as aWINDPROC
should. Could you show us this function? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Hi, In my application I need to create an empty window just to receive messages and treat them. I wrote the following code: HINSTANCE hApi = (HINSTANCE) GetModuleHandle(NULL); if (! hApi) ...// manage error CHAR * WindowClassName = "MyWindowClass"; WNDCLASS WindowClassInfo; if (! GetClassInfo(hApi, WindowClassName, &WindowClassInfo)) { WindowClassInfo.hCursor = NULL; WindowClassInfo.hIcon = NULL; WindowClassInfo.lpszMenuName = NULL; WindowClassInfo.lpszClassName = WindowClassName; WindowClassInfo.hbrBackground = NULL; WindowClassInfo.hInstance = hApi; WindowClassInfo.style = CS_OWNDC | CS_VREDRAW | S_HREDRAW; WindowClassInfo.lpfnWndProc = (WNDPROC) MyProc; WindowClassInfo.cbClsExtra = 0; WindowClassInfo.cbWndExtra = 0; if (! RegisterClass(&WindowClassInfo)) ...//manage error } HWND hWnd = CreateWindow(WindowClassName, "MyWindow", WS_OVERLAPPEDWINDOW, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hApi, NULL); if (! hWnd) ...//manage error But, unfortunately, CreateWindow return NULL and GetLastError return 0 !!! Did anybody could help me ? Thanks in advance. Robin.
Try this, if it work, check your WindowProc function like Joaquim suggested:
LPCTSTR szWindowClassName = \_T("MyWindowClass"); WNDCLASS WindowClassInfo; if (!GetClassInfo(hApi,szWindowClassName, &WindowClassInfo)) { WindowClassInfo.hCursor = NULL; WindowClassInfo.hIcon = NULL; WindowClassInfo.lpszMenuName = NULL; WindowClassInfo.lpszClassName = szWindowClassName; WindowClassInfo.hbrBackground = NULL; WindowClassInfo.hInstance = hApi; WindowClassInfo.style = CS\_OWNDC | CS\_VREDRAW | CS\_HREDRAW; WindowClassInfo.lpfnWndProc = (WNDPROC) ::DefWindowProc; WindowClassInfo.cbClsExtra = 0; WindowClassInfo.cbWndExtra = 0; if (!RegisterClass(&WindowClassInfo)) { TRACE( " ERROR \\n"); } } HWND hWnd = CreateWindow(szWindowClassName, "MyWindow", WS\_OVERLAPPEDWINDOW, 0, 0, CW\_USEDEFAULT, CW\_USEDEFAULT, NULL, NULL, hApi, NULL); ASSERT( hWnd != NULL );
-
Everything seems OK. The only possible problem I can think of is that
MyProc
is not properly declared as aWINDPROC
should. Could you show us this function? Joaquín M López Muñoz Telefónica, Investigación y DesarrolloMyProc is declared in a header as following: LRESULT CALLBACK fidLinkEventProc(HWND _hWnd, UINT _iMsg, WPARAM _wParam, LPARAM _lParam); and MyProc is defined in a .c file: LRESULT CALLBACK MyProc(HWND _hWnd, UINT _iMsg, WPARAM _wParam, LPARAM _lParam) { switch (_iMsg) { ... } return 0; }
-
Try this, if it work, check your WindowProc function like Joaquim suggested:
LPCTSTR szWindowClassName = \_T("MyWindowClass"); WNDCLASS WindowClassInfo; if (!GetClassInfo(hApi,szWindowClassName, &WindowClassInfo)) { WindowClassInfo.hCursor = NULL; WindowClassInfo.hIcon = NULL; WindowClassInfo.lpszMenuName = NULL; WindowClassInfo.lpszClassName = szWindowClassName; WindowClassInfo.hbrBackground = NULL; WindowClassInfo.hInstance = hApi; WindowClassInfo.style = CS\_OWNDC | CS\_VREDRAW | CS\_HREDRAW; WindowClassInfo.lpfnWndProc = (WNDPROC) ::DefWindowProc; WindowClassInfo.cbClsExtra = 0; WindowClassInfo.cbWndExtra = 0; if (!RegisterClass(&WindowClassInfo)) { TRACE( " ERROR \\n"); } } HWND hWnd = CreateWindow(szWindowClassName, "MyWindow", WS\_OVERLAPPEDWINDOW, 0, 0, CW\_USEDEFAULT, CW\_USEDEFAULT, NULL, NULL, hApi, NULL); ASSERT( hWnd != NULL );
Thank for your help. I solved my problem: MyProc was always returning 0 whatever the message was, whereas it has to treat Windows messages such as "WM_CREATE", ... (traditional messages to create a window). Now, I use "return DefWindowProc(_hWnd,_iMsg,_wParam,_lParam)" when I receive a Windows message different from my messages.