Cant create window using CreateWindowEx function - c++

I cant create the window using code below although i get no error in compilation.Only a window with title of my project name appears.I also set hwnd=NULL after CreateWindowEx invocation to see whether MessageBox works but it does not work too.
#include <windows.h>
#include <StdAfx.h>
const char* myClassName="myWindowsClassName";
//The window procedure
LRESULT CALLBACK WndProc( HWND hwnd , UINT msg , WPARAM wParam , LPARAM lParam){
switch(msg){
case WM_CLOSE:
DestroyWindow( hwnd );
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc( hwnd , msg , wParam , lParam );
}
return 0;
}
//Registering window
int WINAPI WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lPCmdLine , int nCmdShow ){
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
wc.cbSize=sizeof( WNDCLASSEX );
wc.style=0;
wc.lpfnWndProc= WndProc;
wc.cbClsExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon( NULL , IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL , IDC_ARROW);
wc.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName= (LPCWSTR) myClassName;
wc.hIconSm=LoadIcon( NULL , IDI_APPLICATION );
if( !RegisterClassEx( &wc ) ){
MessageBox( NULL ,L"Window registeration failed" ,L"Error!" , MB_ICONEXCLAMATION | MB_OK );
return 0;
}
//Creating the window
hwnd=CreateWindowEx( WS_EX_CLIENTEDGE , (LPCWSTR)myClassName , L"The title of my window" , WS_OVERLAPPEDWINDOW , CW_USEDEFAULT , CW_USEDEFAULT, 320 , 240 , NULL , NULL , hInstance , NULL );
hwnd=NULL;
if( hwnd==NULL ){
MessageBox( NULL ,L"Window creation failed",L"Error!", MB_ICONEXCLAMATION | MB_OK );
return 0;
}
ShowWindow ( hwnd , nCmdShow );
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Where is the problem?
Thanks

Unicode vs ANSI mismatch?
myClassName is char* but you cast it to (LPCWSTR). If you are compiling as Unicode (and you should) then you should define the class name as const WCHAR* myClassName=L"myWindowsClassName"; and remove the casts (or use const_cast<LPTSTR>().
You also fail to initialize .cbWndExtra. Change WNDCLASSEX wc; to WNDCLASSEX wc = {};
You should perform better error checking to diagnose failures. Check on MSDN to see if a function sets the last error when it fails. If it does, call GetLastError() after the function fails.

Related

Trying to learn winapi. made a first program which has to show me a window. CMD shows but there is no window

#include<windows.h>
LPSTR NazwaKlasy = "Klasa Okienka";
MSG Komunikat;
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX wc;
wc.cbSize = sizeof( WNDCLASSEX );
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 );
wc.lpszMenuName = NULL;
wc.lpszClassName = NazwaKlasy;
wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
//tutaj kłądż okienka
HWND hwnd;
hwnd = CreateWindowEx ( WS_EX_CLIENTEDGE,NazwaKlasy,"Okienko",WS_EX_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,400,400,NULL,NULL,hInstance,NULL);
//koniec obszaru okienek
ShowWindow( hwnd, nShowCmd ); // Pokaż okienko...
UpdateWindow( hwnd );
while( GetMessage( & Komunikat, NULL, 0, 0 ) )
{
TranslateMessage( & Komunikat );
DispatchMessage( & Komunikat );
}
return Komunikat.wParam;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
DestroyWindow( hwnd );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hwnd, msg, wParam, lParam );
}
return 0;
}
the editor shows no errors and runs the program without any problems but there are no signs of the window
im not very experienced at programming so its most probably a stupid error which i cant just find but everyone has to start somewhere
i think i formated the post correctly
You're not registering the window class. You fill out the WNDCLASSEX structure ok, but you neglect to call RegisterClassEx to actually register it.

IDD_ABOUT: undeclared identifier

I'm new to c / c++ programing language and am following an online tutorial about Win32. When I build and compile my project ( MS VS C++ 2010 Express ), i get this error: 'IDD_ABOUT' : undeclared identifier. I'm not sure how to fix this. :(
Main.cpp:
#include <Windows.h>
#define ID_FILE_EXIT 9001
#define ID_HELP_ABOUT 9002
const char g_szClassName[] = "myWindowClass";
BOOL CALLBACK AboutDlgProc( HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam )
{
switch( Message )
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch( LOWORD( wParam ) )
{
case IDOK:
EndDialog( hwnd, IDOK );
break;
case IDCANCEL:
EndDialog( hwnd, IDCANCEL );
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CREATE:
{
HMENU hMenu, hSubMenu;
HICON hIcon, hIconSm;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu( hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit" );
AppendMenu( hMenu, MF_STRING | MF_POPUP, ( UINT )hSubMenu, "&File" );
hSubMenu = CreatePopupMenu();
AppendMenu( hSubMenu, MF_STRING, ID_HELP_ABOUT, "&About" );
AppendMenu( hMenu, MF_STRING | MF_POPUP, ( UINT )hSubMenu, "&Help" );
SetMenu( hwnd, hMenu );
hIcon = ( HICON )LoadImage( NULL, "menu_one.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE );
if( hIcon )
SendMessage( hwnd, WM_SETICON, ICON_BIG, ( LPARAM )hIcon );
else
MessageBox( hwnd, "Could not load large icon!", "ERROR!", MB_OK | MB_ICONERROR );
hIconSm = ( HICON )LoadImage( NULL, "menu_one.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE );
if( hIconSm )
SendMessage( hwnd, WM_SETICON, ICON_SMALL, ( LPARAM )hIconSm );
else
MessageBox( hwnd, "Could not load small icon!", "ERROR!", MB_OK | MB_ICONERROR );
}
break;
case WM_COMMAND:
switch( LOWORD( wParam ) )
{
case ID_FILE_EXIT:
PostMessage( hwnd, WM_CLOSE, 0, 0 );
break;
case ID_HELP_ABOUT:
{
int ret = DialogBox( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDD_ABOUT ), hwnd, AboutDlgProc );
if( ret == IDOK )
MessageBox( hwnd, "Dialog exited with IDOK.", "Notice", MB_OK | MB_ICONINFORMATION );
else if( ret == IDCANCEL )
MessageBox( hwnd, "Dialog exited with IDCANCEL.", "Notice", MB_OK | MB_ICONINFORMATION );
else if( ret == -1 )
MessageBox( hwnd, "Dialog failed!", "Error", MB_OK | MB_ICONINFORMATION );
}
break;
}
break;
case WM_LBUTTONDOWN:
break;
case WM_CLOSE:
DestroyWindow( hwnd );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hwnd, msg, wParam, lParam );
}
return 0;
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
// Step 1: Registering the Window Class
wc.cbSize = sizeof( WNDCLASSEX );
wc.style = 1;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = NULL;
if( !RegisterClassEx( &wc ) )
{
MessageBox( NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK );
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
500, 200, 800, 600,
NULL, NULL, hInstance, NULL );
if( hwnd == NULL )
{
MessageBox( NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK );
return 0;
}
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
Resource.rc:
#include <Windows.h>
#ifndef IDC_STATIC
#define IDC_STATIC -1
#endif
IDD_ABOUT DIALOG DISCARDABLE 0, 0, 239, 66
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My About Box"
FONT 8, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON "&OK",IDOK,174,18,50,14
PUSHBUTTON "&Cancel",IDCANCEL,174,35,50,14
GROUPBOX "About this program...",IDC_STATIC,7,7,225,52
CTEXT "An example program showing how to use Dialog Boxes\r\n\r\nby theForger",IDC_STATIC,16,18,144,33
END
I have a feeling that Visual Studio Express doesn't include a proper resource editor that auto-generates resource includes, so I'm guessing you've created the .rc file manually. If so, you probably just need to define the symbol. Create an include file called resource.h and add this line to it:
#define IDD_ABOUT 100
Then include it at the top of your .cpp and .rc files:
#include <Windows.h>
#include "resource.h" // add this line
The windows headers define generic certain resource IDs (IDOK, IDCANCEL, IDC_STATIC, etc) but for anything else you'll need to add your own symbol. The values don't really matter - any number up to 65534 is ok as long as it doesn't clash with any other ID in use in the same "scope" (e.g. two controls in the one dialog should have different IDs, but you can re-use the same ID in another dialog).

Adding child window to Metatrader4's chart via DLL - blinking (not redrawing)

I would like to add a child window to Metatrader4's chart window which always stays on top, without blinking, just statically there all the time upon eveything (in parent window). I am doing this from a DLL (C++).
I call this method from the mql side:
MT4_EXPFUNC int __stdcall testWindow(HWND hwnd) {
prnt_hWnd = hwnd;
CreateThread(0, NULL, ThreadProc, (LPVOID)L"Window Title", NULL, NULL);
return 0;
}
The parent window's (chart) handle is given as a parameter.
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
MSG messages;
/*
... in createWindowClass:
WNDCLASSEX wc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = (LPCWSTR)L"MyClass";
wc.lpszClassName = (LPCWSTR)szClassName;
wc.lpfnWndProc = DLLWindowProc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
...
*/
CreateWindowClass(L"MyClass");
HWND hwnd = CreateWindowEx (0, L"MyClass", NULL, WS_VISIBLE | WS_CHILD , CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, prnt_hWnd, NULL, GetModuleHandle(NULL), NULL );
ShowWindow (hwnd, SW_SHOWNORMAL);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return 1;
}
I handle window's messages this way:
LRESULT CALLBACK DLLWindowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_PAINT: {
PAINTSTRUCT ps;
BeginPaint( hwnd, &ps );
EndPaint( hwnd, &ps );
return 0;
}
case WM_COMMAND:
/* */
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
My child window at the begining appears, then after (I guess) the parent window is getting being redrawn, suddenly it disappears, then it is just blinking (fastly appears-disappear).
My goal is to have a child window on that chart statically, so always topmost, without blinking. I could achieve this only without the WS_CHILD property. But then my child window is not ON the parent.
Try adding the WS_CLIPCHILDREN style to the chart window. I would pass the handle on the MQL4 side in init() via some MT4 export function. For example, SetChartWnd( HWND hChartWnd ) and passing WindowHandle( Symbol(), Period() ) as the parameter.
Then inside that function, I would try doing something like the following:
if ( ::IsWindow( hChartWnd ) ) {
DWORD style = GetWindowLong( hChartWnd, GWL_STYLE );
style |= WS_CLIPCHILDREN;
SetWindowLong( hChartWnd, GWL_STYLE, style );
}
}

Visual studio "cannot find the path specified" error

I am having a problem creating a simple window in C++ visual studio. I started a new "empty project" and only created one .cpp file. When I try to run the program, I get this error:
Unable to start program C:\...\Project1.exe. The system cannot find the file specified.
Why does this happen? I'm using visual studio 2010.
Here is my code:
#include <windows.h>
// Function prototypes.
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR szCmdLine,
int iCmdShow )
#pragma region part 1 - STARTUP STUFF
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TEXT("Philip");
wc.lpszMenuName = 0; // no menu - ignore
wc.style = CS_HREDRAW | CS_VREDRAW; // Redraw the window
RegisterClass( &wc );
HWND hwnd = CreateWindow(
TEXT("Philip"),
TEXT("window's title!"),// appears in title of window
WS_OVERLAPPEDWINDOW,
10, 10,
200, 200,
NULL, NULL,
hInstance, NULL );
ShowWindow(hwnd, iCmdShow );
UpdateWindow(hwnd);
#pragma endregion
#pragma region part 2 - ENTER A LOOP TO CONTINUALLY KEEP CHECKING WITH WIN O/S FOR USER INTERACTION
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
#pragma endregion
return msg.wParam; // return from WinMain
}
LRESULT CALLBACK WndProc( HWND hwnd, // "handle" to the window that this message is for
UINT message, // TYPE of message (e.g. WM_PAINT is a message asking to paint the window)
WPARAM wparam, // information about the actual message
LPARAM lparam ) // MORE info about the message
{
switch( message )
{
case WM_CREATE:
// upon creation, let the speaker beep at 50Hz, for 10ms.
Beep( 50, 10 );
return 0;
break;
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint( hwnd, &ps );
// draw a circle and a 2 squares
Ellipse( hdc, 20, 20, 160, 160 );
Rectangle( hdc, 50, 50, 90, 90 );
Rectangle( hdc, 100, 50, 140, 90 );
EndPaint( hwnd, &ps );
}
return 0;
break;
case WM_DESTROY:
PostQuitMessage( 0 ) ;
return 0;
break;
}
return DefWindowProc( hwnd, message, wparam, lparam );
}
If you want to create a window then your kind of project is Windows Form Application (see picture above).
The solutions are an abstract concept to get together several projects. For example you could want to have a Windows Form Application using the features of a Class Library.

Console in C++ application

I want to implement a console inside my C++ application. Like ftp for example. Or (IIRC) sql, once you've connected to a Server.
Does anybody know a library which implements this? Ideally with auto-completion and such? My searches for this only come up with "how to build a C++ console application", which I do know how to do.
GNU Readline implements the features you want. If filename auto-completion is not the sort you need, use a custom auto-complete routine.
For Windows: Use AllocConsole() to attach a text console to your GUI app and freopen( "CON", "w", stdout ) ; to redirect and printf() to output text.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944(v=vs.85).aspx
Sample code:
#include <stdio.h>
#include <stdlib.h>
// Function prototypes.
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );
// In a C++ Windows app, the starting point is WinMain().
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
{
// these next few lines create and attach a console
// to this process. note that each process is only allowed one console.
AllocConsole() ;
freopen( "CON", "w", stdout ) ;
printf("HELLO!!! I AM THE CONSOLE!\n" ) ;
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof( WNDCLASSEX ) ;
wc.cbClsExtra = 0; // ignore for now
wc.cbWndExtra = 0; // ignore for now
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TEXT(" ");
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW; // Redraw the window
RegisterClassEx( &wc );
HWND hwnd = CreateWindowEx( 0, TEXT(" "), TEXT("window's title!"), WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL, hInstance, NULL );
ShowWindow(hwnd, iCmdShow );
UpdateWindow(hwnd);
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam; // return from WinMain
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
switch( message )
{
case WM_CREATE:
// upon creation, let the speaker beep at 50Hz, for 10ms.
Beep( 50, 10 );
printf("HELLO!!! I AM THE CONSOLE!\n" ) ;
return 0;
break;
case WM_PAINT:
{
// we would place our Windows painting code here.
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint( hwnd, &ps );
// draw a circle and a 2 squares
Ellipse( hdc, 20, 20, 160, 160 );
Rectangle( hdc, 50, 50, 90, 90 );
Rectangle( hdc, 100, 50, 140, 90 );
printf("HELLO!!! I AM THE CONSOLE!\n" ) ;
EndPaint( hwnd, &ps );
}
return 0;
break;
case WM_LBUTTONDOWN:
printf("STOP POKING MEEE!!!\n") ;
break;
case WM_DESTROY:
PostQuitMessage( 0 ) ;
return 0;
break;
}
return DefWindowProc( hwnd, message, wparam, lparam );
}
If you want also autocomplete you could
check the example of linenoise (a lightweight readline alternative).
Basically you have to parse the Userinput Line in a Loop.
Example for a very basic CommadLineInterface :
show prompt and read input in a while loop,
call something like parseLine() on \n
split the Line in Tokens by at least Space (then ;) take the first String as Command cmd and the rest as args.
call dispatch(cmd, args);