Dialog Box Closes Immediately After It Opens - c++

I have a dialog box that I am using for user input, and it was working just fine, now no matter what I do the dialog pops up for one second and then the program dies with no warning, calling WM_DESTROY even after I comment out all the possible exits. There is only one error, which is the no_init_all error, but that shows up every time there is a runtime error, and is pretty useless in trying to find the solution.
Here is my code:
project.cpp
BOOL CreateMyDialog(HINSTANCE hInstance) {
HWND hWnd = CreateDialog(NULL, MAKEINTRESOURCE(myDialogResource), NULL, (DLGPROC)myDialogProcess);
if (!hWnd) {
return FALSE;
}
myDialogGlobalHandle = hWnd;
ShowWindow(hWnd, SW_SHOW);
//UpdateWindow(hWnd);
return TRUE;
}
INT_PTR CALLBACK myDialogProcess(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&InitCtrlEx);
switch(msg) {
case WM_INITDIALOG: {
//Do stuff to prep the dialog
return TRUE;
}
case WM_DESTROY: {
PostQuitMessage(WM_QUIT);
break;
}
}
return TRUE;
}
project.rc
myDialogResource DIALOGEX 600, 400, 286, 108
STYLE DS_SYSMODAL | DS_SETFONT | DS_SETFOREGROUND | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION
EXSTYLE WS_EX_OVERLAPPEDWINDOW /*| WS_EX_APPWINDOW*/
CAPTION "My Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
COMBOBOX dropdownList,112,7,61,30,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
//... it's all syntactically correct controls that still work the split second the dialog is on the screen
END
This is the shortened code, and even with this the dialog opens on top of itself and glitches on the screen. With my full code, it appears normally for a split-second before the app closes. I believe if I can fix this small bit, I can find the issue with my dialog. So my question is, what's the issue?
EDIT:
Here is my wWinMain:
HWND myDialogGlobalHandle;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&InitCtrlEx);
if (!CreateMyDialog(hInstance)) {
//return FALSE;
}
else {
MSG message = { 0 };
while (GetMessage(&message, nullptr, 0, 0)) {
if (!IsDialogMessage(myDialogGlobalHandle, &message)) {
DispatchMessage(&message);
}
}
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PROJECT));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}

As it turns out, all my code was correct. The issue was with the case statements in my dialog procedure, for some reason Visual Studio didn't like brackets in the case statements. After trying everything and then just replacing them with break; statements, my code started working again.

Related

Close popup menu programmatically on Windows

I have a simple example for creating popup menu. I want to close this menu programmatically when pressing on button, is there any possibility to do that? Or maybe I should use different class?
I need to open this menu when button is pressed and close it the same way when pressing the button. Here is simple code example that I have.
#define MAX_LOADSTRING 100
#define IDM_FILE_NEW 1
#define IDM_FILE_OPEN 2
#define IDM_FILE_QUIT 3
#define BUTTON_ID 1
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HWND gButton = NULL;
HWND mainHwnd = NULL;
HMENU hMenu;
bool gMenuHidden = false;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_POPUPMENU, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_POPUPMENU));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_POPUPMENU));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
mainHwnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!mainHwnd)
{
return FALSE;
}
ShowWindow(mainHwnd, nCmdShow);
UpdateWindow(mainHwnd);
return TRUE;
}
HMENU CreateAndInitializeMenu()
{
HMENU menu = CreatePopupMenu();
AppendMenuW(menu, MF_STRING, IDM_FILE_NEW, L"&New");
AppendMenuW(menu, MF_STRING, IDM_FILE_OPEN, L"&Open");
AppendMenuW(menu, MF_SEPARATOR, 0, NULL);
AppendMenuW(menu, MF_STRING, IDM_FILE_QUIT, L"&Quit");
return menu;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_COMMAND:
{
if (wParam == BUTTON_ID)
{
//gMenuHidden = !gMenuHidden;
if (gMenuHidden)
{
hMenu = CreateAndInitializeMenu();
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, 150, 250, 0, mainHwnd, NULL);
DestroyMenu(hMenu);
}
else
{
// TODO:
}
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
gButton = CreateWindowW(L"button", L"Show/Hide menu", WS_VISIBLE | WS_CHILD, 100, 120, 120, 25, hWnd, (HMENU)BUTTON_ID, NULL, NULL);
break;
}
}
return DefWindowProcW(hWnd, message, wParam, lParam);
}
Maybe the right way is to always create menu when button is pressed and destroy it right after that? But in that case we should create menu all the time when pressing the button. I was trying to create the menu once and show it when we need it. Also the menu is not shown right now. Trying to figure out why.
Thanks in advance.
First, you can certainly avoid repeating the creation/destruction of your popup menu: just create it (once) on window creation (by handling the WM_CREATE message) and destroy it when handling WM_DESTROY. But note: you will need to make your hMenu a static variable, so that its value is maintained across multiple calls of your WndProc function.
As for hiding the menu programmatically – you can do this by sending the parent window the WM_CANCELMODE message, as suggested in this related question: How to CLOSE a context menu after a timeout?
You can keep a 'flag' variable (also must be static) to keep track of the current display status of the popup menu: if this is not shown, call TrackPopupMenu; if it is shown, send the WM_CANCELMODE message. You will also need to add the TPM_RETURNCMD flag in your call to TrackPopupMenu, so that you can reset the flag if/when the user selects a command from the menu.
Here's a version of your WndProc function that implements this approach:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HMENU hMenu; // Make "static" so we can reuse across calls
static bool hasMenu = false; // Flag to indicate current menu status
POINT point;
switch (msg) {
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDM_FILE_NEW:
case IDM_FILE_OPEN:
MessageBeep(MB_ICONINFORMATION);
break;
case IDM_FILE_QUIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
break;
case WM_CREATE:
// Create the menu (once only) on window creation...
hMenu = CreatePopupMenu();
AppendMenuW(hMenu, MF_STRING, IDM_FILE_NEW, L"&New");
AppendMenuW(hMenu, MF_STRING, IDM_FILE_OPEN, L"&Open");
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenuW(hMenu, MF_STRING, IDM_FILE_QUIT, L"&Quit");
hasMenu = false; // Reset here in case of multiple uses.
break;
case WM_RBUTTONUP:
if (!hasMenu) { // Only show menu if it's not already active...
hasMenu = true;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
ClientToScreen(hwnd, &point);
// Add the "TPM_RETURNCMD" flag so we can reset "hasMenu" when a command is sselected (return non-zero):
BOOL cmd = TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_RETURNCMD, point.x, point.y, 0, hwnd, NULL);
if (cmd) {
hasMenu = false;
SendMessage(hwnd, WM_COMMAND, cmd, 0); // Send the command (not done automatically with TPM_RETURNCMD)
}
}
else {
hasMenu = false;
SendMessage(hwnd, WM_CANCELMODE, 0, 0);
}
break;
case WM_DESTROY:
DestroyMenu(hMenu); // Destroy the menu.
PostQuitMessage(0);
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}

Program window doesn't show up

I'm starting programming in C++ and want to start making applications with graphics and user interface.
I've watched many tutorials on the subject,
and have finished the first part of the tutorial
but the window refused to pop up, and I know that it's working because there are no errors,
and I can see it running in task manager.
Please help.
Hear is the code:
#include <Windows.h>
bool running = true;
LRESULT CALLBACK windows_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;
switch (uMsg) {
case WM_CLOSE:
case WM_DESTROY: {
running = false;
}break;
default: {
result = DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return result;
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
//compile window
WNDCLASS window_class = {};
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpszClassName = 0;
window_class.lpfnWndProc = windows_callback;
//register clases
RegisterClass(&window_class);
// create window
HWND window = CreateWindowA(0, "game stuff", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 720, 360, 0, 0, hInstance, 0); {
while (running) {
// input
// simulate
MSG mesage;
while (PeekMessage(&mesage, window, 0, 0, PM_REMOVE)) {
TranslateMessage(&mesage);
DispatchMessage(&mesage);
}
// render
}
};
}
window refused to pop up, and I know that it's working because there
are no errors, and I can see it running in task manager.
You don't check the error for RegisterClass and CreateWindowA functions calls.
Check the return value of RegisterClass() you will find that the return value is zero, that indicates failure. To get extended error information, call GetLastError.
CreateWindowA() also fails, the return value is NULL. To get extended error information, call GetLastError.
If you use ANSI version of these functions, change WNDCLASS to WNDCLASSA, RegisterClass to RegisterClassA.
You missed class name in both two functions. The following is an example based on your presented code to make the window show up. You can have a try.
CHAR clsName[] = "test";
WNDCLASSA window_class = {};
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpszClassName = clsName;
window_class.lpfnWndProc = windows_callback;
//register clases
ATOM atom = RegisterClassA(&window_class);
if (0 == atom)
{
DWORD err = GetLastError();
return 0;
}
// create window
HWND window = CreateWindowA(clsName, "game stuff", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 720, 360, 0, 0, hInstance, 0);
if (NULL == window)
{
DWORD err = GetLastError();
return 0;
}
More references: "Your First Windows Program", "Unicode in the Windows API".
Update:
why does it have a Chinese name (window's name displays unexecpted)
For using above sample code to display window's name expected you can change project setting to "Use Multi-Byte Character Set" like this:
Or you can use UNICODE APIs (RegisterClassW and CreateWindowW) with project setting "Use Unicode Character Set".
Or you can use the macros like RegisterClassEx and CreateWindowEx, it will choose UNICODE or ASCII version API along with your project setting automatically.

Win32 API CreateWindowW conflicts with Menu Creation

I'm trying to add some application controls into my Windows API code (Note: I am using Visual Studio) I am experiencing a problem where I try to add in a CreateWindowW() function that generates text and a field ("static" and "edit") and a menu at the same time. The menu works fine on its own ("Calculations"):
However, adding the CreateWindow() function "erases" the menu entirely but yields the CreateWindowW() outputs (also flickers a bit):
The code I have right now is this (the menu function and the CreateWindowW() functions are at the bottom):
#define MAX_LOADSTRING 100
#define FILE_MENU_DESTROY 1
#define FILE_MENU_ABOUT 2
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//Custom Application Function Forwrd Declarations
void WinControls(HWND);
void WinMenu(HWND);
HMENU hMenu;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_RANKTOOLADVANCED, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance); //Generates an instance of a window class
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RANKTOOLADVANCED));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)//Class properties of the window
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc; //assigns a window to the instance of the class
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance; //creates an instance
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RANKTOOLADVANCED));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); //defines a type of cursor (arrow, help, cross, etc.)
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_RANKTOOLADVANCED);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{//initializes instance of window class (invocation)
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, L"test", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 900, 600, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{/*
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
case FILE_MENU_NEW:
{
MessageBox(hWnd, L"task failed successfully", L"you done goofed", MB_OK | MB_ICONEXCLAMATION);
break;
}*/
case FILE_MENU_DESTROY:
{
DestroyWindow(hWnd);
break;
}
case FILE_MENU_ABOUT:
{
MessageBox(hWnd, L"about", L"About", MB_OK);
break;
}
break;
}
}
case WM_CREATE:
{
WinControls(hWnd);
WinMenu(hWnd);
break;
}/*
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
TextOut(hdc, 10, 10, rekt, _tcslen(rekt));
TextOut(hdc, 10, 40, reverse, _tcslen(reverse));
EndPaint(hWnd, &ps);
break;
}*/
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void WinMenu(HWND hWnd) {
hMenu = CreateMenu();
HMENU hFileMenu = CreateMenu();
HMENU hSubMenu = CreateMenu();
HMENU hSubMenu2 = CreateMenu();
//XP Calculations
AppendMenuW(hSubMenu, MF_STRING, NULL, L"Rank -> XP");
AppendMenuW(hSubMenu, MF_STRING, NULL, L"XP -> Rank");
//Credit Calculations
AppendMenuW(hSubMenu2, MF_STRING, NULL, L"Rank -> Cred");
AppendMenuW(hSubMenu2, MF_STRING, NULL, L"Cred -> Rank");
AppendMenuW(hFileMenu, MF_POPUP, (UINT_PTR)hSubMenu, L"Points"); //option that popups submenu of hSubMenu
AppendMenuW(hFileMenu, MF_POPUP, (UINT_PTR)hSubMenu2, L"Credits"); //option that popups submenu of hSubMenu
AppendMenuW(hFileMenu, MF_SEPARATOR, NULL, NULL); // separator
AppendMenuW(hFileMenu, MF_STRING, FILE_MENU_ABOUT, L"About");
AppendMenuW(hFileMenu, MF_STRING, FILE_MENU_DESTROY, L"Exit"); // option
AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"Calculations"); //popups up submenu of hFileMenu
SetMenu(hWnd, hMenu);
}
void WinControls(HWND hWnd) {
CreateWindowW(L"Static", L"Enter text here: ", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER, 400, 100, 100, 50, hWnd, NULL, NULL, NULL);
CreateWindowW(L"Edit", L"...", WS_VISIBLE | WS_CHILD, 400, 155, 100, 50, hWnd, NULL, NULL, NULL);
}
Please do note that the setup is the Visual Studio basic setup for the program.
Any help is appreciated!
I have done some debugging and figured out it was that the CreateWindowW() I used for the static and edit controls was conflicting with the definition of the window properties.
Since the window was defined by WNDCLASSEXW wcex, I just had to change the CreateWindowW() with CreateWindowExW().
Only new problem is that the static control (and the others) works but not the edit control, which kills the menu.

Command Prompt window appears with gui window c++

Whenever I try to make a c++ gui program/window the command prompt window appears with the window like this:
but all I did was this::
#include <iostream>
#include "define.h"
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd){
window win;
MSG msg;
win.cbClsExtra = NULL; //Additional parameters
win.cbWndExtra = NULL; //Additional parameters
win.hbrBackground = (HBRUSH)COLOR_WINDOW ; //Sets background color for the window
win.hCursor = LoadCursor(NULL, IDC_ARROW); //The cursor that will appear in the window
win.hIcon = NULL; //Icon for the window
win.hInstance = hInst; //Handle to the application instance
win.lpszClassName = "Window Class"; //The unique name of the window class
win.lpszMenuName = NULL; //Used for menus
win.style = NULL; //The look and feel of the window
win.lpfnWndProc=(WNDPROC)WinProc; //The callback procedure (more on that later)
RegisterClass(&win);
HWND hwnd = CreateWindow("Window Class", "Title", WS_OVERLAPPEDWINDOW, 200, 200, 640, 480, NULL, NULL, hInst, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
switch (message){
case WM_CREATE:{
HWND button = CreateWindow("BUTTON", "ok", WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 50, 30, hWnd, NULL, NULL, &lParam);
break;
}
case WM_DESTROY:{
PostQuitMessage(0);
return 0;
break;
}
default:{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
}
I followed the code just on internet and don't think I did something wrong I want to remove this annoying cmd window by any way...
tysm for all repliers much appreciated...........
EDIT
and btw the define.h is a header file I made and define WNDCLASS as window

Win32 GUI and callback to C++ function

So, basically I'm using a code like this one. It's a little simple window where you can only change text inside of an edit box and press a button that will make a callback to a function (DoSomethingCallback(text)).
#include <windows.h>
#define ID_EDIT 1
#define ID_BUTTON 2
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit;
static HWND hwndButton;
static int len;
static TCHAR text[30];
switch(msg)
{
case WM_CREATE:
hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
50, 50, 150, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndButton = CreateWindow(
TEXT("button"), TEXT("Set Title"),
WS_VISIBLE | WS_CHILD,
50, 100, 80, 25,
hwnd, (HMENU) ID_BUTTON, NULL, NULL);
break;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED) {
SetWindowText(hwnd, "Working...");
GetWindowText(hwndEdit, text, len);
DoSomethingCallback(text);
SetWindowText(hwnd, "Finished");
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "Edit Control" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
CreateWindow( wc.lpszClassName, TEXT("Edit control"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
220, 220, 280, 200, 0, 0, hInstance, 0);
while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
The problem is that when running that Callback the window will appear as "Not responsive" and even laggy if you try to close or press the button. I understand the reason why this might be happening, the callback takes time and the receiving loop isn't there to check the input of the user. I've searched a way to 'fix' this and I couldn't find any. I'm pretty sure it's something dumb, but I have to try and ask.
An obvious way would be making a faster callback. But is there another one like checking the user input inside of a DoSomethingCallback() while, or I have to use multiple threads?
Sorry for the confusing question.
I would go for calling QueueUserWorkItem() to handle it. If your DoSomethingCallback() is too long, there is no way to make your window responsive while DoSomethingCallback() is working since there is only one thread to run the code. Good Luck!
If possible, you can process the message queue at intervals within DoSomethingCallback which will keep the UI responsive. Just run the original message loop i.e. while( GetMessage(... whenever you can but make sure you disable the button so the user does not click a second time... this will lead to recursion.
You can peek at the message queue to convince windows that you are still alive and kicking, by calling PeekMessage() from time to time. This assumes that you can do this in your DoSomethingCallback(), i.e. that the main thread doesn't hang completely. Regard the loop below as pseudo code and use your imagination to transform it to your needs.
void DoSomethingCallback()
{
// Loop that takes a long time
while (true) {
DoSomeStuff();
MSG msg;
PeekMessage(&msg, nil, 0, 0, PM_NOREMOVE);
if (ShouldBreak()) {
break;
}
}
}