i'm building an app in pure c++/win32. it contains two separately used windows. when i switch to 2nd window first time, it shows perfectly. closing it and reverting to first window works fine too, but when i try to switch to 2nd window again, it never shows. i wonder, what am i doing wrong?
#include <windows.h>
#include <windowsx.h>
HWND mainWnd, subWnd;
HINSTANCE hInst;
HWND btnShowSubWindow;
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CREATE:{
btnShowSubWindow = CreateWindow("button", "Show 2nd window", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
50,50,150,20,
hwnd,(HMENU)1000, hInst, NULL);
break;
}
case WM_COMMAND:{
switch(LOWORD(wParam)){
case 1000:{
ShowWindow(mainWnd, SW_HIDE);
ShowWindow(subWnd, SW_SHOW);
break;
}
}
break;
}
case WM_CLOSE:{
if (MessageBox(hwnd, "Really quit?", "My application", MB_OKCANCEL) == IDOK)
{
DestroyWindow(hwnd);
}
break;
}
/* Upon destruction, tell the main thread to stop */
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK SubWndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE:{
CloseWindow(hwnd);
ShowWindow(mainWnd, SW_SHOW);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
void CreateMainWnd(){
WNDCLASSEX wc; /* A properties struct of our window */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = MainWndProc; /* This is where we will send messages to */
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "MainWindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return;
}
mainWnd = CreateWindowEx(WS_EX_CLIENTEDGE,wc.lpszClassName,"Main window",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
480, /* height */
NULL,NULL,hInst,NULL);
if(mainWnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return;
}
}
void CreateSubWnd(){
WNDCLASSEX wc; /* A properties struct of our window */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = SubWndProc; /* This is where we will send messages to */
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "SubWindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return;
}
subWnd = CreateWindowEx(WS_EX_CLIENTEDGE,wc.lpszClassName,"Secondary window",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
480, /* height */
NULL,NULL,hInst,NULL);
if(subWnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return;
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MSG msg; /* A temporary location for all messages */
hInst = hInstance;
CreateMainWnd();
CreateSubWnd();
ShowWindow(mainWnd, nCmdShow);
ShowWindow(subWnd, SW_HIDE);
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
return msg.wParam;
}
i'm using Dev-C++ 5.11.
Addition #1:
i read the answer and i changed MainWndProc's WM_COMMAND handler a bit. now it looks like that:
switch(LOWORD(wParam)){
case 1000:{
ShowWindow(mainWnd, SW_HIDE);
if(subWnd!=NULL)
ShowWindow(subWnd, SW_SHOW);
else
ShowWindow(hwnd, SW_SHOW);
break;
}
i also added a WM_DESTROY handler to SubWndProc:
case WM_DESTROY:{
return 1;
break;
}
if subWnd was destroyed then mainWnd should reappear but it doesn't. i guess that something screws up the contents of subWnd after it hides.
OK, you're calling CloseWindow which minimizes the subwindow. To restore a minimized window, you need to use SW_RESTORE rather than SW_SHOW.
The default action when a user closes a window (by pressing the × in the upper right or through the taskbar) is, that it get's destroyed and it's HWND turns invald. ShowWindow can hide and show existing windows, but it can't resurrect the "dead".
What you can do however is, that you implement a handler for the WM_CLOSE message that hides the window, instead of destroying it. But you must make sure, that once all windows are hidden you actually quit the program, or some other way give the user to interact.
However creating windows is kind of cheap, so the better solution is, to let the windows get destroyed and just to create a new window as replacement.
Related
I am new to win32 API GUI programming in C++. When the program is compiled and executed, It always fails at CreateWindowsEx. Sorry but this only what I can explain at this moment.
Aside from that, It passes the window registration, which is good. But from the time Windows Creation executed, it always return NULL.
Here's my C++ code:
#include <windows.h>
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow){
//MessageBox(NULL, "Hello ", " H", MB_OK);
WNDCLASSEX wc = {0};
//memset(&wc,0,sizeof(wc));
//wc.cbSize = sizeof(WNDCLASSEX);
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WindowProcedure; /* This is where we will send messages to */
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(! RegisterClassEx(&wc) ){
MessageBox(NULL, "Error 1", " H", MB_OK);
return -1;
}
HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
480, /* height */
NULL,NULL,hInst,NULL);
//returns 0x0
//return GetLastError();
if( hwnd == NULL){
MessageBox(NULL, "Error 2", " H", MB_OK);
return GetLastError();
}
MSG msg = {0};
while( GetMessage(&msg, NULL, 0, 0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
switch(msg){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, msg, wp, lp);
}
return 0;
}
Please approach in the level of a novice, so it easy for me to understand what should be done.
The reason why the CreatewindowEx fails is because of DefWindowProcW should be DefWindowProc.
DefwindowProcW is used with CreateWindowW, but since it is using CreateWindowEx, DefWindowProc should be the correct one to use.
I want to try and learn OpenGL, I know C++ and I found a site with C++/OpenGL tutorials, this site also teaches a few things about WinAPI,bcs "our" programms are going to be Windows applications. In this tutorial it has the code of "our" first Windows application. The code (it doesn't compile):
/* Trim fat from windows*/
#define WIN32_LEAN_AND_MEAN
#pragma comment(linker, "/subsystem:windows")
/* Pre-processor directives*/
#include "stdafx.h"
#include <windows.h>
/* Windows Procedure Event Handler*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paintStruct;
/* Device Context*/
HDC hDC;
/* Text for display*/
char string[] = "Hello, World!";
/* Switch message, condition that is met will execute*/
switch(message)
{
/* Window is being created*/
case WM_CREATE:
return 0;
break;
/* Window is closing*/
case WM_CLOSE:
PostQuitMessage(0);
return 0;
break;
/* Window needs update*/
case WM_PAINT:
hDC = BeginPaint(hwnd,&paintStruct);
/* Set txt color to blue*/
SetTextColor(hDC, COLORREF(0x00FF0000));
/* Display text in middle of window*/
TextOut(hDC,150,150,string,sizeof(string)-1);
EndPaint(hwnd, &paintStruct);
return 0;
break;
default:
break;
}
return (DefWindowProc(hwnd,message,wParam,lParam));
}
/* Main function*/
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX windowClass; //window class
HWND hwnd; //window handle
MSG msg; //message
bool done; //flag saying when app is complete
/* Fill out the window class structure*/
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
/* Register window class*/
if (!RegisterClassEx(&windowClass))
{
return 0;
}
/* Class registerd, so now create window*/
hwnd = CreateWindowEx(NULL, //extended style
"MyClass", //class name
"A Real Win App", //app name
WS_OVERLAPPEDWINDOW | //window style
WS_VISIBLE |
WS_SYSMENU,
100,100, //x/y coords
400,400, //width,height
NULL, //handle to parent
NULL, //handle to menu
hInstance, //application instance
NULL); //no extra parameter's
/* Check if window creation failed*/
if (!hwnd)
return 0;
done = false; //initialize loop condition variable
/* main message loop*/
while(!done)
{
PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE);
if (msg.message == WM_QUIT) //check for a quit message
{
done = true; //if found, quit app
}
else
{
/* Translate and dispatch to event queue*/
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
It didn't compile, it had a few errors so I changed it a bit:
/* Trim fat from windows*/
#define WIN32_LEAN_AND_MEAN
#pragma comment(linker, "/subsystem:windows")
/* Pre-processor directives*/
#include <tchar.h> //couldn't find stdafx.h
#include <windows.h>
/* Windows Procedure Event Handler*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paintStruct;
/* Device Context*/
HDC hDC;
/* Text for display*/
wchar_t string[] = L"F*CK!!!! It doesn't work, why am I getting Kanji? WTF\0"; // fixed error: char not compatible, char -> wchar_t
/* Switch message, condition that is met will execute*/
switch (message)
{
/* Window is being created*/
case WM_CREATE:
return 0;
break;
/* Window is closing*/
case WM_CLOSE:
PostQuitMessage(0);
return 0;
break;
/* Window needs update*/
case WM_PAINT:
hDC = BeginPaint(hwnd, &paintStruct);
/* Set txt color to blue*/
SetTextColor(hDC, COLORREF(0x00FF0000));
/* Display text in middle of window*/
TextOut(hDC, 150, 150, string, sizeof(string) - 1);
EndPaint(hwnd, &paintStruct);
return 0;
break;
default:
break;
}
return (DefWindowProc(hwnd, message, wParam, lParam));
}
/* Main function*/
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX windowClass; //window class
HWND hwnd; //window handle
MSG msg; //message
bool done; //flag saying when app is complete
/* Fill out the window class structure*/
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = L"MyClass"; // fixed error: char* not compatible with LPCWSTR
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
/* Register window class*/
if (!RegisterClassEx(&windowClass))
{
return 0;
}
/* Class registerd, so now create window*/
hwnd = CreateWindowEx(NULL, //extended style
L"MyClass", //class name | fixed error: char* not compatible with LPCWSTR
L"A Real Win App", //app name | fixed error: char* not compatible with LPCWSTR
WS_OVERLAPPEDWINDOW | //window style
WS_VISIBLE |
WS_SYSMENU,
100, 100, //x/y coords
400, 400, //width,height
NULL, //handle to parent
NULL, //handle to menu
hInstance, //application instance
NULL); //no extra parameter's
/* Check if window creation failed*/
if (!hwnd)
return 0;
done = false; //initialize loop condition variable
/* main message loop*/
while (!done)
{
PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);
if (msg.message == WM_QUIT) //check for a quit message
{
done = true; //if found, quit app
}
else
{
/* Translate and dispatch to event queue*/
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
Now it compiles, but, I get this:
The "F*CK!!!! it doesn't work, why am I getting Kanji, WTF" is correct(Forgive me if it is not Kanji)
Thank you for listening(reading actually) to my problems
You need to replace this:
TextOut(hDC, 150, 150, string, sizeof(string) - 1);
with this:
TextOut(hDC, 150, 150, string, wcslen(string));
sizeof (string) is in bytes, not characters.
Also, please don't spam language tags. Just pick the one(s) that are relevant.
The last parameter of TextOut() takes a character count, but you are passing it a byte count instead.
In the original code, the string array used char characters, and sizeof(char) is 1 byte, so the two counts were the same numeric value. But you changed the code to use wchar_t characters instead, and on Windows sizeof(wchar_t) is 2 bytes.
So, by using TextOut(..., string, sizeof(string) - 1) with a wchar_t string, you are telling TextOut() that there are 2 times as many wchar_ts in the string array as there really are, and thus TextOut() goes out of bounds of the string array printing out random garbage from surrounding memory (consider yourself lucky the code didn't just crash outright from accessing memory it didn't own).
If you are going to use sizeof() in this manner, then to get the correct array element count, you need to divide the array's byte size by its element's byte size, eg:
TextOut(..., string, (sizeof(string) / sizeof(string[0])) - 1)
You can replace that with the _countof() macro in Visual Studio (or equivalent in other compilers), eg:
TextOut(..., string, _countof(string) - 1)
In C++11 and later, you should use std::size() instead, eg:
TextOut(..., string, std::size(string) - 1)
Note that if the string array is ever replaced with a wchar_t* pointer in the future, these solutions will not work anymore. The sizeof() solution will still compile but will not produce the correct result. But the _countof() and std::size() solutions will fail to compile, which is a good thing. Then you will know that you need to replace it with a more appropriate solution, like lstrlenW(), std::wstring::size(), etc.
You're using sizeof(string). That's the number of bytes, not the number of characters. Since you're using C++, use std::wstring::size().
I am creating a college project in C++ language using Dev C++ SDK, with the help of Windows API (windows.h header).
I have to create a Window Interface which consist of a button and I want to use that button to open a batch file (.bat) or run batch code stored in the program.
I have created a Window with a button by using your Windows API tutorials found on Zetcode blog.
But I am not able to use that button to open a batch file (.bat) or run batch code stored in the program.
Please tell me the code to implement this in my project. I am adding my program code and screenshot image of my program.
#include <windows.h>
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CREATE:
CreateWindow(TEXT("button"), TEXT("Disable USB"),
WS_VISIBLE | WS_CHILD,
10, 10, 100, 25,
hwnd, (HMENU) 1, NULL, NULL
);
CreateWindow(TEXT("button"), TEXT("Enable Firewall"),
WS_VISIBLE | WS_CHILD,
120, 10, 120, 25,
hwnd, (HMENU) 2, NULL, NULL
);
break;
case WM_COMMAND:
// I need code which i can write here to execute batch code or run a bat file (.bat).
break;
/* Upon destruction, tell the main thread to stop */
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG msg; /* A temporary location for all messages */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (GetSysColorBrush(COLOR_3DFACE));
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","System Hardening Tool",WS_VISIBLE|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
480, /* height */
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
return msg.wParam;
}
Screenshots:
My Project Window with diaglog box and buttons
Program code
There are many options to launch a new process. For example, you can use the system function, the spawn function, or you can use CreateProcess from the Win32 API. They take different parameters, and may have slightly different behavior. Their respective documentation should detail the exact behavior, and it is dependent on the parameters you pass.
Note that, since you will be running the new process inside a windows function handler, if you use a blocking call (such as with system), the window launching the process will freeze until the executed process exits.
I'm trying to create two instances of a window class.
When the primary one is closed it should close the application but when the secondary one is closed it should just close that window.
However when either window is closed the application exits, and I'm not sure why. I've tried comparing the hWnd to check which window is being closed.
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
//Forgive me now
#define MAX_WINDOWS 1024
HWND hWindows[MAX_WINDOWS];
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
RegisterClassEx(&wc);
hWindows[0] = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Our First Windowed Program", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
300, // y-position of the window
500, // width of the window
400, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
hWindows[1] = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Our First Windowed Program", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
300, // y-position of the window
500, // width of the window
400, // height of the window
hWindows[0], // primary window
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
ShowWindow(hWindows[0], nCmdShow);
ShowWindow(hWindows[1], nCmdShow);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
// translate keystroke messages into the right format
TranslateMessage(&msg);
// send the message to the WindowProc function
DispatchMessage(&msg);
}
// return this part of the WM_QUIT message to Windows
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch (message)
{
case WM_CLOSE:
{
if (hWnd = hWindows[0]) {
// close the application entirely
PostQuitMessage(0);
}
else {
DestroyWindow(hWnd);
}
return 0;
} break;
}
// Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}
if (hWnd = hWindows[0])
That's assignment. Since hWindows[0] is non-zero, that expression always evaluates true.
You mean:
if (hWnd == hWindows[0])
You should call PostQuitMessage in response to WM_DESTROY. And since the default window procedure calls DestroyWindow in response to WM_CLOSE, you can write it like this:
switch (message)
{
case WM_DESTROY:
{
if (hWnd == hWindows[0]) {
// close the application entirely
PostQuitMessage(0);
}
return 0;
}
break;
}
// Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
I am a beginner c++ graphics programmer and using dev c++ as IDE. I created a simple frame with textbox and a button and called DwmExtendFrameIntoClientArea from dwmapi.dll.
But the textbox and button show ugly transparent text. I want opaque text on textbox and button. I've searched all around but found answers for .NET c++ and not dev c++. So please help me dealing with this matter using dev c++. Efforts will be appreciated.
Here's the code:
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <uxtheme.h>
HWND tb;
HWND btn;
typedef struct _DWM_BLURBEHIND
{
DWORD dwFlags;
BOOL fEnable;
HRGN hRgnBlur;
BOOL fTransitionOnMaximized;
} DWM_BLURBEHIND, *PDWM_BLURBEHIND;
HINSTANCE DLL = LoadLibrary("dwmapi.dll");
typedef HRESULT (*_DwmExtendFrameIntoClientArea)(HWND hwnd,MARGINS* margins);
_DwmExtendFrameIntoClientArea DwmExtendFrameIntoClientArea = (_DwmExtendFrameIntoClientArea)GetProcAddress(DLL,"DwmExtendFrameIntoClientArea");
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CREATE: {
ShowWindow(GetConsoleWindow(),SW_HIDE);
MARGINS margins = {-1,-1,-1,-1};
DwmExtendFrameIntoClientArea(hwnd,&margins);
tb = CreateWindow("EDIT","Text",WS_CHILD | WS_VISIBLE,5,5,574,30,hwnd,NULL,NULL,NULL);
btn = CreateWindow("BUTTON","Button",WS_CHILD | WS_VISIBLE,5,50,80,30,hwnd,NULL,NULL,NULL);
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG Msg; /* A temporary location for all messages */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOWTEXT);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_WINDOWEDGE,"WindowClass","Frame",WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
600, /* width */
350, /* height */
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&Msg); /* Translate key codes to chars if present */
DispatchMessage(&Msg); /* Send it to WndProc */
}
return (int) Msg.wParam;
}