How can I create a 'REAL' windows application from my console application?

I want it to look and feel like a windows program, not like a dos program. I'm already somewhat familiar with very basic elements of windows.h, but I can still only program console apps.

I want to take my existing code and create a windows program with pull down menus, file, options etc...

how can i do this?

any easy tutorial on the subject?
theForger's Win32 API Tutorial is an excellent place to start
http://www.winprog.org/tutorial/

Another winner is the FunctionX Win32 Programming tutorials
http://www.functionx.com/win32/index.htm

Outside of that, I don't know. Google around a bit.

If you have the cash, I also recommend you getting Programming Windows by Charles Petzold. http://www.charlespetzold.com/pw5/index.html

Hope this helps.
OH i forgot to mention I'm not using borland or dev-c++ or visual studio, nothing fancy just codeblocks. So I want to write a 'windows' program just as I would a console app, (there are no wizards or anything like to select program type as windows program....) i got so far a textbox to popup with "hello world", but there is still a console window in the background...

How can I make the console window dissappear?

Hope
Why don't you make it in assembly then?
You find plenty of tutorials for that by googling.
well i already have 99% of my project done in c++, and wanna just implement it into a windows version. I don't know assembly, and I don't have an assembly compiler.
jmc: Are you trying to break some sort of "most bad advices given in a single day" record or something?

wtf: You'll find among the project options (right click on the project, properties), build targets tab, an option called "type" with the following options:
GUI application
Console application
Static library
Dynamic library
Commands only
Native
You want the first one. You'll likely get linker errors if you try to compile the code as it is. Windows GUI apps use a different entry than regular console apps.

Also, FYI, Dev C++ uses the same compiler as Code::Blocks. And Code::Blocks is actually fancier than Dev C++.

EDIT: Assembly is assembled, and if you have a C/++ compiler, then you also have an assembler.
Last edited on
ok well I had to restart and create a file as a "project" which i'm not used to. but after I did that I found the build targets tab like you said and checked "GUI application", but it still displays the console window.
I don't know the cause in your case, but if it is only about hiding the window in C++ you can use ShowWindow from the WinAPI:
1
2
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
Last edited on
wtf read Helios' post again...

Windows GUI apps use a different entry than regular console apps.
There's a word missing, there. It was supposed to be "entry point".
I don't think that's the problem. If that was the case, he wouldn't even be able to run.
I tried:

HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);

but i get error: 'GetConsoleWindow()' was not declared in this scope
For GetConsoleWindow you need to include the Windows.h header file (Winbase.h).
ok well now that I got my console to disappeare, when I go to close the main window the program never stops executing (because the console is still open, just invisible. )

is there a way I can make the console close when the main window closes?

I already tried adding control handlers, but I only know how to use SetConsoleCtrlHandler.

It would be nice if there was a SetCtrlHandler too, or better yet if instead of hiding the window it wouldn't be generated at all.

Here is what I have so far.

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>

using namespace std;

HWND hWnd = GetConsoleWindow();
BOOL WINAPI TrapCtrlHandler( DWORD dwCtrlType );
BOOL WINAPI CtrlHandler (DWORD dwCtrlType );

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
if( SetConsoleCtrlHandler( TrapCtrlHandler, TRUE ))
cout << "The Control Handler is installed." << endl;
else
cout << "ERROR: Could not set control handler";
if (SetCtrlHandler( CtrlHandler, TRUE ))
cout << "The Handler is functioning. " << endl;
else
cout << "MISTAKE: Something went wrong. " << endl;
Sleep(2000);
ShowWindow(hWnd, SW_HIDE);

MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
return 0;
}

BOOL WINAPI CtrlHandler (DWORD dwCtrlType )
{
switch( dwCtrlType )
{
case CTRL_CLOSE_EVENT:

cout << "Processing Ctrl+Close event\n" << endl ;
ShowWindow(hWnd, SW_SHOW);
break;

default:
return false;
}
return true;
}

BOOL WINAPI TrapCtrlHandler( DWORD dwCtrlType )
{
switch( dwCtrlType )
{
case CTRL_CLOSE_EVENT:
cout << "Processing Ctrl+Close event\n" << endl ;
ShowWindow(hWnd, SW_SHOW);
break;
default:
return FALSE; // unhandled.Some other in the call back list can process
}

return TRUE; // handled the events
}

I get error: 'SetCtrlHandler' was not declared in this scope.
if I comment out the line referrencing SetCtrlHandler it will compile, but the event will only be handled if the console is closed, not the main window.

What can I do?
Last edited on
Firstly, I think there shouldn't be any console with this code if you are trying to make a windows win32 gui application.

Why do you use the console specific function "SetConsoleCtrlHandler" in a program without console?
SetCtrlHandler is not really a function of the official WinAPI, is it?
Cout could be used in Win32 GUI Apps as well, but most of the time it doesn't make any sense. For further information: http://www.halcyon.com/~ast/dload/guicon.htm

EDIT:
Here a little example for message handling in Win32 GUI Apps from the web. It looks quite OK to me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <windows.h>

const WCHAR g_szClassName[] = L"myWindowClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
 switch(msg){
  case WM_LBUTTONDOWN:
  {
   WCHAR szFileName[MAX_PATH];
   HINSTANCE hInstance = GetModuleHandle(NULL);

   GetModuleFileName(hInstance, szFileName, MAX_PATH);
   MessageBox(hwnd, szFileName, L"This program is:", MB_OK | MB_ICONINFORMATION);
  }
  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;

 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 = g_szClassName;
 wc.hIconSm    = LoadIcon(NULL, IDI_APPLICATION);

 if(!RegisterClassEx(&wc)){
  MessageBox(NULL, L"Window Registration Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
  return 0;
 }

 hwnd = CreateWindowEx(
  WS_EX_CLIENTEDGE,
  g_szClassName,
  L"The title of my window",
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
  NULL, NULL, hInstance, NULL);

 if(hwnd == NULL){
  MessageBox(NULL, L"Window Creation Failed!", L"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;
}
Last edited on
Topic archived. No new replies allowed.