Categories: c++, c, windows, getch, createwindow

Reason for crashing of created window in C

1 answer

When I create a window in C using the CreateWindow() function, it works fine it just disappears instantly, so I used the getch() function to try to resolve the issue but it does not work. The window does not display the button, and crashes.

But when I used MessageBox() instead of getch(), it stays and functions normally. I am trying to figure out why this happens.

I tried many things, like using MessageBox() and getch() together, using getch() before and after ShowWindow(), but every time it gives me some interesting result but not the normal functionality of the window.

Code that works:

#include <windows.h>  int _stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {     HWND h;     HINSTANCE i;     h = CreateWindow("Button", "XYZ", WS_OVERLAPPEDWINDOW, 15, 20, 250, 200, 0, 0, i, 0);     ShowWindow(h, nCmdShow);     MessageBox(0, "Stop", "Wait", MB_OK);     return 0; } 

Code that does not work:

#include <windows.h>  int _stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {     HWND h;     HINSTANCE i;     h = CreateWindow("Button", "XYZ", WS_OVERLAPPEDWINDOW, 15, 20, 250, 200, 0, 0, i, 0);     ShowWindow(h, nCmdShow);     getch();     return 0; } 

I want to know the reason for this error. I think it is because getch() is a DOS function, but still the compiler should at least show a warning.

All answers to this question, which has the identifier 60797688

The best answer:

There is no crash in this code.

Your getch() example simply lacks a message loop needed to service the window, and also getch() is meaningless in a non-console app. So your WinMain() exits immediately after showing the button window.

Whereas your MessageBox() example has a message loop (inside of MessageBox() itself) which keeps WinMain() running, and the button window processong UI messages, until the MessageBox dialog is closed.

Also, it doesn't make sense to try to display a button as its own overlapped window. You should be registering and creating a separate overlapped window that then creates the button as a child. User actions on the button are sent to the button's parent window, so you need to create a parent window for it.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data