What could cause my C++ win32 app to run in the background? - c++

I have been working on this app for at least 3-4 months and just recently it hasn't been working.
It started today, because yesterday it was running fine and smooth.
Now it runs, but the window doesn't become visible(I checked the process list in Task Manager and it was running). I have made sure that the
ShowWindow(hwnd, nFunsterStil); was not accidentally deleted, but it is still there.
I believe the problem to be something with WM_PAINT because that is what I was working on when the program stopped working.
This is what I had when it compiled and first started being invisible:
PAINTSTRUCT ps;
HDC hdc;
case WM_PAINT: {
hdc = BeginPaint(hwnd, &ps);
static RECT Background, Background2;
SetRect(&Background, 0, 0, 476, 555);
SetRect(&Background2, 300, 300, 200, 400);
FillRect(hdc, &Background, (HBRUSH) (INT_PTR)CreateSolidBrush(RGB(50,55,50)));
FillRect(hdc, &Background2, (HBRUSH) (INT_PTR)CreateSolidBrush(RGB(100,200,100)));
EndPaint(hwnd, &ps);
}
Now I tried getting rid of some of the lines in there, making the whole block into a comment, and even entirely deleting it but it did not help anything. I know I probably messed something up with my computer's memory. So my question is: What is wrong, and How can I fix it? Thanks in advance for any help.
Here is the full Message Switch:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CTLCOLORSTATIC: {
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(50,255,50));
SetBkColor(hdcStatic, RGB(0,0,0));
return (INT_PTR)CreateSolidBrush(RGB(0, 0, 0));
}
case WM_CTLCOLOREDIT: {
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(50,255,50));
SetBkColor(hdcStatic, RGB(0,0,0));
return (INT_PTR)CreateSolidBrush(RGB(0, 0, 0));
}
case WM_CREATE:
{
// ------------------MENU-------------------
HMENU hMenubar = CreateMenu();
HMENU hFile = CreatePopupMenu();
HMENU hCPP = CreatePopupMenu();
HMENU hHELP = CreatePopupMenu();
HMENU hCUSTOMIZE = CreatePopupMenu();
HMENU hTHEME = CreatePopupMenu();
HMENU hNEW = CreatePopupMenu();
AppendMenu(hFile, MF_STRING, ID_HOME, "Ho&me");
AppendMenu(hFile, MF_SEPARATOR, 0, 0);
AppendMenu(hFile, MF_POPUP, (UINT) hNEW, "N&ew...");
AppendMenu(hNEW, MF_STRING, ID_NEW, "Not&e");
AppendMenu(hFile, MF_STRING, ID_SAVE, "Sa&ve");
AppendMenu(hFile, MF_STRING, ID_SHOWCPP, "Ed&it");
AppendMenu(hFile, MF_SEPARATOR, 0, 0);
AppendMenu(hFile, MF_STRING, ID_CLOSE, "Cl&ose");
AppendMenu(hCPP, MF_STRING, ID_SHOWNOTES, "Li&st all notes");
AppendMenu(hCPP, MF_STRING, MENU_COREFILES, "Open 'Notes' fol&der");
AppendMenu(hCPP, MF_SEPARATOR, 0, 0);
AppendMenu(hCPP, MF_STRING, ID_SAVEFILE, "Sa&ve");
AppendMenu(hCPP, MF_STRING, ID_SHOWCPP, "Ed&it");
AppendMenu(hHELP, MF_STRING, 0, "Help Fil&es...");
AppendMenu(hHELP, MF_STRING, MENU_ABOUT, "Ab&out");
AppendMenu(hHELP, MF_SEPARATOR, 0, 0);
AppendMenu(hHELP, MF_STRING, 0, "Pro&duct Website");
AppendMenu(hCUSTOMIZE, MF_POPUP, (UINT) hTHEME, "Current& Theme");
AppendMenu(hTHEME, MF_STRING | MF_CHECKED, 0, "Pr&ompt");
AppendMenu(hTHEME, MF_STRING, 0, "No&te Pad");
AppendMenu(hMenubar, MF_STRING | MF_POPUP, (UINT) hFile, "Fi&le");
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_STRING | MF_POPUP, (UINT) hCPP, "No&tes");
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_STRING | MF_POPUP, (UINT) hHELP, "He&lp");
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_STRING | MF_POPUP, (UINT) hCUSTOMIZE, "Custom&ise");
SetMenu(hwnd, hMenubar);
// WINDOW FEATURES!!!
TEST[1] = CreateWindow(TEXT("BUTTON"), TEXT("List all notes!"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
200,375,150,25,
hwnd,(HMENU) ID_SHOWNOTES, NULL, NULL);
MAIN = CreateWindow(TEXT("edit"), TEXT(""),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_READONLY | SBS_RIGHTALIGN | WS_VSCROLL,
10,20,516,350,
hwnd,(HMENU) 5001, NULL, NULL);
EDIT = CreateWindow(TEXT("edit"), TEXT(""),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE | SBS_RIGHTALIGN | WS_VSCROLL,
10,20,516,350,
hwnd,(HMENU) 5001, NULL, NULL);
ShowWindow(EDIT, SW_HIDE);
NAME=CreateWindow(TEXT("edit"), TEXT("-> Title <-"),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL,
10, 0, 516, 20,
hwnd, (HMENU) ID_FILENAME, NULL, NULL);
ShowWindow(NAME, SW_HIDE);
TEST[2] = CreateWindow(TEXT("BUTTON"), TEXT("-> Home <-"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
200,375,150,25,
hwnd,(HMENU) ID_SHOWHOME, NULL, NULL);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(MAIN, DEFTEXT);
break;
}
case WM_COMMAND: {
// MENU ITEMS!!!
//Close
if( LOWORD(wParam) == ID_CLOSE ) {
PostQuitMessage (0);
}
if(LOWORD(wParam) == MENU_COREFILES) {
ShellExecute(NULL, "explore", "Notes", NULL, NULL, SW_SHOW);
}
if(LOWORD(wParam) == MENU_ABOUT) {
MessageBox(hwnd, "© 2012 Joseph Meadows. \r\nThis program is essentially a reference guide for whatever you want. It can be used to store school notes, or things completely opposite.", "About:",
MB_ICONINFORMATION | MB_SYSTEMMODAL | MB_OK);
}
if(LOWORD(wParam) == ID_SHOWCPP) {
if(home==3) {
MessageBox(hwnd, "Error: 9026. Cannot edit Home Page.", "Invalid Action!",
MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_OK);
}
else if(home==1) {
MessageBox(hwnd, "Error: 6502 Cannot edit System pages.", "Invalid Action!",
MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_OK);
}
else {
ShowWindow(EDIT, SW_SHOW);
ShowWindow(NAME, SW_SHOW);
ShowWindow(MAIN, SW_HIDE);
SetWindowText(StatButts[0],"List all notes!");
mode=2;
}
}
if(LOWORD(wParam) == ID_SAVE) {
char* tittle=SaveFile(hwnd, NAME, EDIT);
SetWindowText(MAIN, tittle);
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
mode=1;
}
if(LOWORD(wParam) == ID_HOME) {
if(mode==2){
int Savebox=MessageBox(hwnd, "Would you like to save this page?", "Save Page",
MB_ICONQUESTION | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_YESNOCANCEL);
switch(Savebox){
case IDYES: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
SetWindowText(StatButts[0],"List all notes!");
int len = GetWindowTextLength(EDIT)+1;
static char title[1000];
GetWindowText(EDIT, title, len);
SetWindowText(MAIN, title);
mode=1;
home=1;
break;
}
case IDNO: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
SetWindowText(StatButts[0],"List all notes!");
SetWindowText(MAIN, DEFTEXT);
mode=1;
home=1;
}
case IDCANCEL: {}
}
}
if(mode==1){
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
SetWindowText(MAIN, DEFTEXT);
SetWindowText(StatButts[0],"List all notes!");
}
}
if(LOWORD(wParam)==ID_NEW) {
if(mode==1) {
ShowWindow(EDIT, SW_SHOW);
ShowWindow(NAME, SW_SHOW);
ShowWindow(MAIN, SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
SetWindowText(EDIT, "");
mode=2;
}
}
if(LOWORD(wParam)==ID_SHOWHOME) {
if(mode==1) {
ShowWindow(MAIN, SW_SHOW);
ShowWindow(NAME, SW_HIDE);
ShowWindow(EDIT, SW_HIDE);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
home=1;
SetWindowText(MAIN, DEFTEXT);
SetWindowText(TEST[1], "List all notes!");
}
if(mode!=1) {
int Savebox=MessageBox(hwnd, "Would you like to save this page?", "Save Page?",
MB_ICONQUESTION | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_YESNOCANCEL);
switch(Savebox){
case IDYES: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
int len = GetWindowTextLength(EDIT)+1;
static char title[1000];
GetWindowText(EDIT, title, len);
SetWindowText(MAIN, title);
mode=1;
home=1;
break;
}
case IDNO: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
mode=1;
home=1;
}
}
}
}
if(LOWORD(wParam)==ID_SHOWNOTES) {
if(mode==1) {
std::string umm="";
ShowWindow(NAME,SW_SHOW);
ShowWindow(MAIN,SW_SHOW);
ShowWindow(EDIT,SW_HIDE);
ShowWindow(TEST[1], SW_HIDE);
ShowWindow(TEST[2], SW_SHOW);
SetWindowText(NAME, "-> Title <-");
WIN32_FIND_DATA File_Buffer;
HANDLE hFind = FindFirstFile(TEXT("Notes/*.NNF"), &File_Buffer);
PTSTR Files[8999];
int num=0;
SetWindowText(MAIN, "");
if(hFind != INVALID_HANDLE_VALUE) {
do {
PTSTR FileName = File_Buffer.cFileName;
Files[num] = FileName;
num=num+1;
string kkk(FileName);
stringstream nam(kkk);
string name;
getline(nam, name, '.');
umm = Stringadd(umm, name);
//================================================================================================
} while (FindNextFile(hFind, &File_Buffer));
std::stringstream out;
out << num;
std::string numb=out.str();
umm=umm+" \r\n \r\n "+"Number of Notes: "+numb;
const char * lol=umm.c_str();
SetWindowText(MAIN, lol);
FindClose(hFind);
}
}
if(mode!=1) {
int Savebox=MessageBox(hwnd, "Would you like to save this page?", "Save Page",
MB_ICONQUESTION | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_YESNOCANCEL);
switch(Savebox){
case IDYES: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
int len = GetWindowTextLength(EDIT)+1;
static char title[1000];
GetWindowText(EDIT, title, len);
SetWindowText(MAIN, title);
mode=1;
home=1;
break;
}
case IDNO: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
mode=1;
home=1;
}
}
}
}
// BUTTONS!!!
//Show CPP
break;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
static RECT Background;
SetRect(&Background, 0, 0, 476, 555);
FillRect(hdc, &Background, (HBRUSH) (INT_PTR)CreateSolidBrush(RGB(50,55,50)));
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
default: /* for messages that we don't deal with */
break;
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
And here is the parts relevant to making the window:
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd, hwnd2; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) (INT_PTR)CreateSolidBrush(RGB(0,0,0));
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"VirtualNotes", /* Title Text */
WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
554, /* The programs width */
475, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-

You should perform an investigation to identify the problem. Then - ask for the solution.
In case you're creating a standard "overlapped" window (i.e. with title bar, system menu and min/max/close buttons) - all of the above should be drawn by the system, only the client area (i.e. the internals of the window) is drawn within WM_PAINT handling. So that if you see nothing on the screen - the problem is probably elsewhere.
There's a nice tool within the MSVC, called Spy++. Using it find your process/thread, check if it *really created the window, check your window style and placement.
You may also add a breakpoint within your WM_PAINT processing. You'll get there iff your window indeed exists and visible.
Some notes regarding your code:
No error checking (such as testing retval of BeginPaint). How do you know your window is indeed created (or at least its class is registered)? At least some debug-time macro (such as ASSERT) would be helpful.
If there a reason Why are you using static RECT? (in some very specific cases this may save some stack memory).
CreateSolidBrush returns you a brush handle, which you must free by DeleteObject. You have a resource leak actually.

Looks like you are missing a break at the end of your case.

When you fill your WNDCLASS struct, register and create your main window, you'll then call ShowWindow(handle, SW_SHOW/SW_HIDE); presumably. Make sure this call isn't saying SW_HIDE
Given that this section of your code isn't show in your current post I could be wrong but it would be a wise place to look.
EDIT: On line 55ish, ShowWindow(); change nFunsterStil to "SW_SHOW" and see if that solves your problem.
#Joseph

Related

How to resize window after adding menubar

When I add a menubar to a window, the window didn't resize accordingly because the top portion is hidden by the menubar. I want to resize the window while taking account of menubar's height. This is what I tried.
/* g++ test.cpp -o test -Wl,-subsystem,windows */
#include <Windows.h>
#define ID_OPEN 0
#define ID_EXIT 1
#define ID_ABOUT 2
const int width = 500;
const int height = 400;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szWndClassName[] = TEXT("hellowin");
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
//Step 1: Registering the Window Class
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szWndClassName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program only works in Windows NT or greater!"), TEXT("Hey dude!"), MB_OK | MB_ICONERROR);
return 1;
}
// Step 2: Creating the Window
hWnd = CreateWindow(szWndClassName, /* window class name */
TEXT("Hey!"), /* window title (or caption) */
WS_OVERLAPPEDWINDOW, /* window style */
CW_USEDEFAULT, /* initial x position */
CW_USEDEFAULT, /* initial y position */
width, /* initial window width */
height, /* initial window height */
NULL, /* parent window handle */
NULL, /* window menu handle */
hInstance, /* program instance handle */
NULL); /* creation parameters */
if (hWnd == NULL)
{
MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error!"),
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
// Step 3: The Message Loop (heart)
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
// Step 4: the Window Procedure (brain)
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
HMENU hMenuBar = CreateMenu();
HMENU hFile = CreatePopupMenu();
HMENU hHelp = CreatePopupMenu();
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR) hFile, "File");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR) hHelp, "Help");
AppendMenu(hFile, MF_STRING, ID_OPEN, "Open");
AppendMenu(hFile, MF_STRING, ID_EXIT, "Exit");
AppendMenu(hHelp, MF_STRING, ID_ABOUT, "About");
SetMenu(hWnd, hMenuBar);
PMENUBARINFO pmbi;
GetMenuBarInfo(hWnd, OBJID_MENU, 0, pmbi);
RECT menuRect;
RECT windRect;
GetWindowRect(pmbi->hwndMenu, &menuRect);
GetWindowRect(hWnd, &windRect);
int width = menuRect.right - menuRect.left;
int height = (menuRect.bottom - menuRect.top) + (windRect.bottom - windRect.top);
SetWindowPos(hWnd,
0,
0,
0,
width,
height,
SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rect;
GetClientRect(hWnd, &rect);
DrawText(hdc, TEXT("Hello World!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
I tried to retrieve rectangles of both menubar and the window and calculate the new dimension like this
int width = menuRect.right - menuRect.left;
int height = (menuRect.bottom - menuRect.top) + (windRect.bottom - windRect.top);
Unfortunately the code crashes. I have no idea which part I did wrong. Any ideas?
pmbi is an uninitialised pointer variable. Passing it to GetMenuBarInfo is therefore the likely cause of your crash. If that doesn't crash, then attempting to access pmbi->hwndMenu probably will.
Instead, you want:
MENUBARINFO mbi;
mbi.cbSize = sizeof (mbi); // see documentation
GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi);
...
which tells GetMenuBarInfo to fill in the (stack-based) variable mbi.
Then replace pmbi->hwndMenu with mbi.hwndMenu and that should fix your problem.

Why does clicking on edit box close win32 window(C++)?

The code below should create a simple window with a menu, static text, and an edit box but When I try to create an edit box it either does not run at all (with no errors) or when it does run and I try to click on the edit box the window closes. I am really stuck and any help would be greatly appreciated! Side note: I am a beginner at winapi/gui's with c++ so please be respectful.
#include <Windows.h>
#include <iostream>
#define FILE_MENU_NEW 1
#define FILE_MENU_OPEN 2
#define FILE_MENU_EXIT 3
struct WindowPosition
{
int x, y = 0;
} winPos;
struct WindowSize
{
int width = 600;
int height = 500;
} winSize;
HMENU hMenu;
// Window proc function
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
void addMenus(HWND);
void AddControls(HWND);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow)
{
WNDCLASSW wc = { 0 };
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = L"myWindowClass";
wc.lpfnWndProc = WindowProcedure;
if (!RegisterClassW(&wc))
{
return -1;
}
CreateWindowW(L"myWindowClass", L"My Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, winPos.x, winPos.y, winSize.width, winSize.height,
NULL, NULL, NULL, NULL);
// Window loop
MSG msg = { 0 };
while (GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_COMMAND:
if (wp == 1)
{
MessageBeep(MB_OK);
}
else if (FILE_MENU_EXIT)
{
DestroyWindow(hWnd);
}
break;
case WM_CREATE:
addMenus(hWnd);
AddControls(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, msg, wp, lp);
}
}
void addMenus(HWND hWnd)
{
hMenu = CreateMenu();
HMENU hFileMenu = CreateMenu();
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_NEW, L"New");
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_OPEN, L"Open");
AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_EXIT, L"Exit");
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
AppendMenu(hMenu, MF_STRING, 2, L"Help");
SetMenu(hWnd, hMenu);
}
void AddControls(HWND hWnd)
{
CreateWindowW(L"static", L"Username: ", WS_VISIBLE | WS_CHILD, 200, 100, 100, 50, hWnd, NULL, NULL, NULL);
// This is where the problem is... (When I remove the second parameter and put L"Enter Username") the program does not run at all.
CreateWindowW(L"edit", NULL, WS_VISIBLE | WS_CHILD, 0, 0, 100, 50, hWnd, NULL, NULL, NULL);
}
When you use else if (FILE_MENU_EXIT), this result is always true. You can try:
case WM_COMMAND:
if (wp == 1)
{
MessageBeep(MB_OK);
}
else if (wp == FILE_MENU_EXIT)
{
DestroyWindow(hWnd);
}
break;
Also you can use:
case WM_COMMAND:
switch (wp)
{
case FILE_MENU_NEW:
MessageBeep(MB_OK);
break;
case FILE_MENU_EXIT:
DestroyWindow(hWnd);
break;
}
break;

C++ Jump to case label error while doing windows.h

I was programming C++ and came across an error that said "Jump to case label" that i could not fix.I searched the internet and found no solution that worked. How do i fix this error?
#include<iostream>
#include<windows.h>
using namespace std;
LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam);
INT WINAPI WinMain(HINSTANCE currentInstance, HINSTANCE previousInstance, PSTR cmdLine, INT cmdCount){
const char *CLASS_NAME = "myWin32WindowClass";
WNDCLASS wc{};
wc.hInstance = currentInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpfnWndProc = WindowProcessMessages;
RegisterClass(&wc);
CreateWindow(CLASS_NAME, "Operating System", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, nullptr, nullptr, nullptr, nullptr);
MSG msg{};
while(GetMessage(&msg, nullptr, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
#define ID_BUTTON 1
#define ID_BUTTON2 2
#define TEXTBOX 3
#define FILE_MENU_NEW 4
#define FILE_MENU_OPEN 5
#define FILE_MENU_EXIT 6 //<-"Jump to case label" error is right here
static HWND hwndTextbox;
LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam){
switch(msg){
case WM_CREATE:{
CreateWindow(TEXT("STATIC"), TEXT("value"), WS_VISIBLE | WS_CHILD, 10, 10, 100, 25, hwnd, (HMENU) NULL, NULL, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("testButton"), WS_CHILD | WS_VISIBLE, 10, 30, 80, 20, hwnd, (HMENU) ID_BUTTON, NULL, NULL);
CreateWindow(TEXT("EDIT"), TEXT("VALUE"), WS_VISIBLE | WS_CHILD | WS_BORDER, 10, 70, 200, 20, hwnd, (HMENU) NULL, NULL, NULL );
CreateWindow(TEXT("BUTTON"), TEXT("Title change"), WS_CHILD | WS_VISIBLE, 10, 130, 80, 20, hwnd, (HMENU) ID_BUTTON2, NULL, NULL);
hwndTextbox = CreateWindow(TEXT("EDIT"), TEXT("Change To What?"), WS_VISIBLE | WS_CHILD | WS_BORDER, 10, 100, 200, 20, hwnd, (HMENU) TEXTBOX, NULL, NULL );
HMENU hMenubar = CreateMenu();
HMENU hFile = CreateMenu();
HMENU hOptions = CreateMenu();
HMENU hSubmenu = CreateMenu();
AppendMenu(hSubmenu, MF_POPUP, NULL, "SubMenu Item");
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hFile, "File");
AppendMenu(hMenubar, MF_POPUP, NULL, "Edit");
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hOptions, "Options");
AppendMenu(hFile, MF_STRING, FILE_MENU_EXIT, "Exit");
AppendMenu(hFile, MF_POPUP, (UINT_PTR)hSubmenu, "Open Submenu");
AppendMenu(hOptions, MF_STRING, NULL, "Option 1");
AppendMenu(hOptions, MF_SEPARATOR, NULL, NULL);
AppendMenu(hOptions, MF_STRING, NULL, "Option 2");
SetMenu(hwnd, hMenubar);
break;
}
case WM_COMMAND:{
switch(LOWORD(param)){
case ID_BUTTON:
MessageBox(hwnd, "button has been clicked", "title for popup", MB_ICONINFORMATION);
break;
case ID_BUTTON2:
int len = GetWindowTextLength(hwndTextbox) + 1;
static char title[500];
GetWindowText(hwndTextbox, title, len);
SetWindowText(hwnd, title);
case FILE_MENU_EXIT:
DestroyWindow(hwnd);
break;
case
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, msg, param, lparam);
}
}
Here, you are creating variables that exist in the same scope as other cases, but whose initialization is skipped by those cases.
That's what the error is telling you.
case ID_BUTTON2:
int len = GetWindowTextLength(hwndTextbox) + 1;
static char title[500];
GetWindowText(hwndTextbox, title, len);
SetWindowText(hwnd, title);
You can fix it by limiting the scope of these variables.
case ID_BUTTON2:
{
int len = GetWindowTextLength(hwndTextbox) + 1;
static char title[500];
GetWindowText(hwndTextbox, title, len);
SetWindowText(hwnd, title);
}
You may have also overlooked a break; for that case.

Win32 API: transparency doesn't work

I wrote a simple program; it's made of a main window and TextBox window above it. TextBox is 50% transparent. When TextBox gets a message, it draws a blue line on the main window.
The problem is that "transparent" actually is not transparent at all. If the blue line crosses a text in TextBox, the text just erased, despite the fact that the text is above. And vice versa: if I start typing, a part of the line in a row of a text just disappears instead of shine through.
Is this a bug? Or am I missing something?
#include <windows.h>
#include <stdio.h>
#define IDC_MAIN_EDIT 101
void DrawInWindow(HWND hWndToPaint){
HDC hdc = GetDC(hWndToPaint);
if(!hdc)printf("Invalid handle\n");
HPEN hPen = CreatePen(PS_SOLID,5,RGB(0, 0, 255));
SelectObject(hdc, hPen);
static float x=620, y=1, tg=0.5, ctg=2;
static int Xone = 1, Yone = 1;//depending on later recalculation this may become negative
MoveToEx(hdc,(int)x,(int)y,NULL);
if(tg<1){
y+=tg;
x+=Xone;
}else{
y+=Yone;
x+=ctg;
}
if(!LineTo(hdc, (int)x, (int)y) )printf("There are paint problem\n");
ReleaseDC(hWndToPaint,hdc);
//Now recalculate direction
RECT WndRect;
GetClientRect(hWndToPaint,&WndRect);
if(x>=WndRect.right){
if(ctg>0)ctg*=-1;//make negative
Xone=-1;
}
if(x<=WndRect.left){
if(ctg<0)ctg*=-1;//make positive
Xone=1;
}
if(y>=WndRect.bottom){
if(tg>0)tg*=-1;//make negative
Yone=-1;
}
if(y<=WndRect.top){
if(tg<0)tg*=-1;//make positive
Yone=1;
}
}
int CALLBACK EnumWindowsFunc(HWND hWnd, LPARAM lParam){
DrawInWindow(hWnd);
return false;
}
void PaintInMainWnd(){
EnumWindows(EnumWindowsFunc,0L);//Getting the handle of main window to draw
}
LRESULT __stdcall MyMainCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
switch(msg){
case WM_KEYDOWN:
if(wp == VK_ESCAPE)PostQuitMessage(0);
break;
case WM_DESTROY:
printf("\ndestroying window\n");
PostQuitMessage(0);
return 0;
case WM_SIZE:{
HWND hEdit;
RECT rcClient;
GetClientRect(window, &rcClient);
hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
break;
}
default:
return DefWindowProc( window, msg, wp, lp ) ;
}
}
WNDPROC lpEditWndProc;
LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
{
PostQuitMessage(0);
return 0;
}
PaintInMainWnd();
lpEditWndProc(hWnd, uMsg, wParam, lParam);
}
bool CreateWindows(){
const char* const myclass = "myclass";
WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, myclass, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&wndclass)<0){
printf("ERR: in registering window class\n");
return false;
}
//Creating window
HWND window = CreateWindowEx( 0, myclass, "title",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!window){
printf("ERR: in creating window\n");
return false;
}
ShowWindow( window, SW_SHOWDEFAULT );
//creating TextBox on the window
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(0, "edit", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL){
MessageBox(window, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
return false;
}
hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
//Now resize TextBox to fill whole parent window
RECT RectSize;
GetClientRect(window,&RectSize);
hEdit = GetDlgItem(window,IDC_MAIN_EDIT);
SetWindowPos(hEdit, 0,0,0,RectSize.right,RectSize.bottom,SWP_NOZORDER);
//Let's try to catch some messages in TextBox...
lpEditWndProc = (WNDPROC)SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
//Making hEdit transparent
SetWindowLongPtr(hEdit,GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(hEdit, GWL_EXSTYLE) );
SetLayeredWindowAttributes(hEdit, 0, (255*50)/100, LWA_ALPHA);
return true;
//###
}
int main(){
if(!CreateWindows() ){printf("Something gone wrong\n");return 1;}
MSG msg;
while(GetMessage(&msg,0,0,0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Don't know is it important, but I should also mention, that I have tested only under Ubuntu with Wine, as my Windows is screwed up by this bug. Anyway, I hope the problem is not in Wine itself.
And sorry for amount of code, I really don't know what to remove to make it smaller.
I found the workaround. I created background top level window, and made foreground window 50% trasparent. I draw the lines in background window. If the front window moved or resized, the back window reacts accordingly with help of WM_WINDOWPOSCHANGED message, it sent on every little move/resize.
Anyway, this workaround is little dirty because of:
Linux/wine specific problems: 1) Display Manager do not decorate transparent window of wine(but this could be evaded by making second window 0% transparent) 2) Dragged window wobbling, but the second moving straight. All OS specific problem: second window is visible in the taskbar. Theoretically the last could be avoided by adding WS_EX_TOOLWINDOW to unowned window. The quote
To prevent the window button from being placed on the taskbar, create
the unowned window with the WS_EX_TOOLWINDOW extended style.
But, at least, in wine it doesn't work. Well, I hope this is a bug :)
#include <windows.h>
#include <stdio.h>
#define IDC_MAIN_EDIT 101
HWND hBackWnd;
void DrawInWindow(HWND hWndToPaint){
HDC hdc = GetDC(hWndToPaint);
if(!hdc)printf("Invalid handle\n");
HPEN hPen = CreatePen(PS_SOLID,5,RGB(0, 0, 255));
SelectObject(hdc, hPen);
static float x=620, y=1, tg=0.5, ctg=2;
static int Xone = 1, Yone = 1;//depending on later recalculation this may become negative
MoveToEx(hdc,(int)x,(int)y,NULL);
if(tg<1){
y+=tg;
x+=Xone;
}else{
y+=Yone;
x+=ctg;
}
if(!LineTo(hdc, (int)x, (int)y) )printf("There are paint problem\n");
ReleaseDC(hWndToPaint,hdc);
//Now recalculate direction
RECT WndRect;
GetClientRect(hWndToPaint,&WndRect);
if(x>=WndRect.right){
if(ctg>0)ctg*=-1;//make negative
Xone=-1;
}
if(x<=WndRect.left){
if(ctg<0)ctg*=-1;//make positive
Xone=1;
}
if(y>=WndRect.bottom){
if(tg>0)tg*=-1;//make negative
Yone=-1;
}
if(y<=WndRect.top){
if(tg<0)tg*=-1;//make positive
Yone=1;
}
}
LRESULT __stdcall MyMainCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
switch(msg){
case WM_KEYDOWN:
if(wp == VK_ESCAPE)PostQuitMessage(0);
break;
case WM_DESTROY:
printf("\ndestroying window\n");
PostQuitMessage(0);
return 0;
case WM_SIZE:
HWND hEdit;
RECT rcClient;
GetClientRect(window, &rcClient);
hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
break;
case WM_WINDOWPOSCHANGED:{//LPARAM is a ptr to WINDOWPOS
RECT BckWndRect;
if(!GetWindowRect(hBackWnd, &BckWndRect) )printf("ERR: getting backwnd rectangle\n");
bool IsRepaint;
WINDOWPOS* pNewPos = (WINDOWPOS*)lp;
if(BckWndRect.left+BckWndRect.right != pNewPos->cx
|| BckWndRect.top+BckWndRect.bottom != pNewPos->cy)IsRepaint = true;
else IsRepaint = false;
MoveWindow(hBackWnd, pNewPos->x, pNewPos->y, pNewPos->cx, pNewPos->cy, IsRepaint);
break;
}
default:
return DefWindowProc( window, msg, wp, lp ) ;
}
}
WNDPROC lpEditWndProc;
LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
{
PostQuitMessage(0);
return 0;
}
DrawInWindow(hBackWnd);
lpEditWndProc(hWnd, uMsg, wParam, lParam);
}
bool CreateWindows(){
//creating back window
const char* backwnd = "backwnd";
WNDCLASSEX backwndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, backwnd, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&backwndclass)<0){
printf("ERR: in registering second window class\n");
return false;
}
hBackWnd = CreateWindowEx( 0, backwnd, "title", WS_EX_TOOLWINDOW |
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!hBackWnd){
printf("ERR: in creating background window\n");
return false;
}
ShowWindow( hBackWnd, SW_SHOWDEFAULT );
//Creating front window
const char* const frontwnd = "frontwnd";
WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, frontwnd, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&wndclass)<0){
printf("ERR: in registering foreground window class\n");
return false;
}
HWND window = CreateWindowEx( 0, frontwnd, "title",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!window){
printf("ERR: in creating foreground window\n");
return false;
}
ShowWindow( window, SW_SHOWDEFAULT );
//creating textbox
HWND hEdit = CreateWindowEx( 0, "edit", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL
| ES_MULTILINE | ES_AUTOVSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, 640,
480, window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(0), 0 );
HFONT hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
//Let's try to catch some messages in TextBox...
lpEditWndProc = (WNDPROC)SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
//Making foreground window transparent
SetWindowLongPtr(window,GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(window, GWL_EXSTYLE) );
SetLayeredWindowAttributes(window, 0, (255*50)/100, LWA_ALPHA);
return true;
//###
}
int main(){
if(!CreateWindows() ){printf("Something gone wrong\n");return 1;}
MSG msg;
while(GetMessage(&msg,0,0,0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

Child window Problem in C++

I'm having a problem in my program whenever I close the child window, the main window also exits.
I am a newbie in this programming.
The Correct Code:
/**
#file MainWindow.cpp
#author Andro Bondoc
#date 2011-02-11
*/
/** #file MainWindow.cpp #author Andro Bondoc #date 2011-02-11 */
#include "tray.h"
#define WM_USER_SHELLICON WM_USER + 1
HWND hWnd, Button, LoadNew, TextBox;
HINSTANCE hInst;
HICON hMainIcon;
HMENU hPopMenu;
NOTIFYICONDATA structNID;
long PASCAL WndProcParent(HWND,UINT,UINT,LONG);
long PASCAL WndProcChild(HWND,UINT,UINT,LONG);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
WNDCLASS wc, ws;
hInst = hInstance;
if(!hPrevInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProcParent;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance,(LPCTSTR)MAKEINTRESOURCE(IDI_TRAYICON));//LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wc.lpszMenuName = NULL;
wc.lpszClassName = "Data Retriever Parent";
RegisterClass(&wc);
ws.style = CS_HREDRAW | CS_VREDRAW;
ws.lpfnWndProc = WndProcChild;
ws.cbClsExtra = 0;
ws.cbWndExtra = 0;
ws.hInstance = hInstance;
ws.hIcon = LoadIcon(hInstance,(LPCTSTR)MAKEINTRESOURCE(IDI_TRAYICON));//LoadIcon(NULL,IDI_APPLICATION);
ws.hCursor = LoadCursor(NULL,IDC_ARROW);
ws.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
ws.lpszMenuName = NULL;
ws.lpszClassName = "Data Retriever Child";
RegisterClass(&ws);
}
hWnd = CreateWindowEx(0, wc.lpszClassName, "Data Retriever",
WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
LoadNew = CreateWindowEx(0, ws.lpszClassName, "Help Program",
WS_BORDER | WS_CAPTION | WS_CHILD,
120,
80,
500,
300,
hWnd, NULL, hInstance, NULL);
hMainIcon = LoadIcon(hInstance,(LPCTSTR)MAKEINTRESOURCE(IDI_TRAYICON));
structNID.cbSize = sizeof(NOTIFYICONDATA);
structNID.hWnd = (HWND) hWnd;
structNID.uID = IDI_TRAYICON;
structNID.uFlags = NIF_ICON | NIF_MESSAGE;
structNID.hIcon = hMainIcon;
structNID.uCallbackMessage = WM_USER_SHELLICON;
Shell_NotifyIcon(NIM_ADD, &structNID);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
long FAR PASCAL WndProcParent(HWND hwnd,UINT message,UINT wParam,long lParam)
{
HDC hdc = NULL;
POINT lpClickPoint;
char buff[100] = "";
switch(message){
case WM_CREATE:
Button = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Close",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 680,480, 80, 30, hwnd, (HMENU)ID_CLOSE,
GetModuleHandle(NULL), NULL);
Button = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Help",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 680,450, 80, 30, hwnd, (HMENU)ID_HELPDAW,
GetModuleHandle(NULL), NULL);
break;
/*case WM_DESTROY:
PostQuitMessage(0);
return 0;*/
case WM_USER_SHELLICON:
switch(LOWORD(lParam))
{
case WM_LBUTTONDBLCLK:
ShowWindow(hwnd, SW_RESTORE);
break;
case WM_RBUTTONDOWN:
//get mouse cursor position x and y as lParam has the message itself
GetCursorPos(&lpClickPoint);
//place the window/menu there if needed
hPopMenu = CreatePopupMenu();
InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,ID_OPEN,"&Open");
InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,ID_HELPDAW,"&Help");
InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,ID_CLOSE,"&Exit");
//workaround for microsoft bug, to hide menu w/o selecting
SetForegroundWindow(hWnd);
TrackPopupMenu(hPopMenu,TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_BOTTOMALIGN,lpClickPoint.x, lpClickPoint.y,0,hWnd,NULL);
SendMessage(hWnd,WM_NULL,0,0);
//MessageBox(NULL,"TEST rightclick","Testing ...",MB_OK);
return TRUE;
}
break;
case WM_COMMAND:
if(ID_CLOSE == LOWORD(wParam))
{
int iRes = MessageBox(NULL,"Do you want to Exit","Data Retriever",MB_YESNO|MB_ICONQUESTION);
if(IDYES == iRes)
{
Shell_NotifyIcon(NIM_DELETE,&structNID);
DestroyWindow(hWnd);
PostQuitMessage(0);
}
}
else if(ID_HELPDAW == LOWORD(wParam))
{
ShowWindow(LoadNew, SW_SHOW);
//MessageBox(NULL, "Help","Data Retriever",MB_OK|MB_ICONQUESTION);
}
else if(ID_OPEN == LOWORD(wParam))
{
ShowWindow(hWnd, SW_NORMAL);
}
break;
case WM_CLOSE:
Shell_NotifyIcon(NIM_DELETE,&structNID);
DestroyWindow(hWnd);
PostQuitMessage(0);
break;
case WM_SYSCOMMAND:
if(SC_MINIMIZE == wParam)
{
ShowWindow(hWnd,SW_HIDE);
return TRUE;
}
break;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
long FAR PASCAL WndProcChild(HWND hwnd,UINT message,UINT wParam,long lParam){
HDC hdc = NULL;
PAINTSTRUCT ps;
char buff[100] = "";
switch(message){
case WM_CREATE:
Button = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Unload",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20,20, 80, 30, hwnd, (HMENU)ID_MESSAGE,
GetModuleHandle(NULL), NULL);
break;
case WM_COMMAND:
if(ID_RETURN == LOWORD(wParam))
{
ShowWindow(hWnd, SW_SHOW);
ShowWindow(LoadNew,SW_HIDE);
//MessageBox(NULL, "Help","Data Retriever",MB_OK|MB_ICONQUESTION);
}
case WM_CLOSE:
ShowWindow(hWnd, SW_SHOW);
ShowWindow(LoadNew,SW_HIDE);
break;
case WM_PAINT:
RECT rect;
GetClientRect(LoadNew,&rect);
hdc = BeginPaint(LoadNew,&ps);
strcpy_s(buff,"TOP");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_CENTER|DT_TOP);
strcpy_s(buff,"RIGHT");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_VCENTER|DT_RIGHT);
strcpy_s(buff,"BOTTOM");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_CENTER|DT_BOTTOM);
strcpy_s(buff,"LEFT");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_VCENTER|DT_LEFT);
strcpy_s(buff,"CENTER");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
strcpy_s(buff,"BOTTOM-LEFT");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_BOTTOM|DT_LEFT);
strcpy_s(buff,"BOTTOM-RIGHT");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_BOTTOM|DT_RIGHT);
strcpy_s(buff,"TOP-LEFT");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_TOP|DT_LEFT);
strcpy_s(buff,"TOP-RIGHT");
DrawText(hdc,buff,strlen(buff), &rect,DT_SINGLELINE|DT_TOP|DT_RIGHT);
EndPaint(LoadNew,&ps);
break;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
You have the same wndproc code for each window. For WM_CLOSE, you DestroyWindow(hWnd), where hWnd is the global that stores your main window.
So, closing your child window closes the main window because you told it to.
Your modified code is still using the same WndProc for both windows. You tried to call RegisterClass twice with the same class name. The WndProcs will not be separated until you change the name.
ws.lpszClassName = "Data Retriever CHILD";
This problem would have been more readily apparent if you had checked the return value from RegisterClass - currently, the second call fails. There's other problems with this code (the MessageBox call in the child WndProc is really obnoxious, for example), but this change should be enough to get you headed in the right direction.