Menu disappears after running program later - c++

Okay, so at first when i run my win32 program the menu works fine, however when i open the application later the next day or such the menu is gone but the code never changed. im making the menu with a .rc file. is this the recommended way?
resource.rc
#include "resource.h"
IDR_MYMENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", ID_FILE_EXIT
END
END
resource.h
#define IDR_MYMENU 101
#define IDI_MYICON 201
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
main.cpp
#include "resource.h"
wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
also i noticed that MSVC++ has a very very complex windows templates, vs bloodshed. should i maybe give up on bloodshed and use MSVC++? I am use to blooshed, but i want to have an edge when i finally learn this stuff?
HWND hwnd; /* 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 (GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wincl.hIconSm = (HICON) LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16 ,0);
wincl.hCursor = LoadCursor (NULL, IDC_CROSS);
wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU); /* 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) GetStockObject(BLACK_BRUSH);
/* 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 */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* 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 */
);

The content of your RC file looks fine, so I don't think the problem is there. I doubt the problem is in Bloodshed either -- while I'm not particularly fond of Dev-C++, I doubt it's causing anything like this. That leaves your code for the application as the most likely culprit for causing the problem. Unfortunately, you haven't shown enough of that to even guess at likely sources of the problem.

Related

Getting a/d converter example program to work in a C++ windows program

I got the following example code for a C windows programm from the developer of my analog to digital converter and want to make it run as a windows c++ project in Visual Studio 2016.
As a beginner in C++, windows API and this universal library for the converter I'm overwhelmed with the troubleshooting right now. The plan was to learn by making the code work but right now it's really hard to find out where the problems are and there's not the time to learn everything from scratch (though I try next to it)
This is my setup: Windows 7 64bit, MS Visual Studio Community 2017, USB 1608fs plus a/d converter with its universal library
These are the errors I'm currently getting:
a value of type "HANDLE" cannot be used to initialize an entity of type "HINSTANCE" (row 83)
a value of type "HGDIOBJ" cannot be used to initialize an entity of type "HBRUSH"(row 86)
the argument of type "HANDLE" is incompatible with parameter type "HINSTANCE" (row 93)
Actions I took until now:
creating a new empty win32 project in MS Visual Studio
copying the example code
copying the header file and the library from the converter into the same folder
switching the character set from unicode to multibyte
switching off the precompiled header
include the missing headers from the precompiled header
Here is the code:
#include <windows.h> /* Compiler's include files's */
#include <string.h>
#include <stdio.h>
#include "cbw.h" /* Universal Library's include file */
#include "targetver.h"
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#define BOARD_NUM 0 /* Number of A/D board as defined with InstaCal */
#define ADRANGE BIP5VOLTS /* A/D voltage range */
#define TIMER_NUM 1 /* Windows timer used by this program */
HWND hWndMain; /* handle for main window */
LONG FAR PASCAL MainMessageHandler(HWND, UINT, WPARAM, LPARAM);
/************************************************************************
*
* Name: WinMain
*
* Arguments: hInstance - the instance handle for the program
* hPrevInstance - the class name of the application (not used)
* CmndLine - command line was called with (not used)
* CmndShow - indicates how to display window
*
* This is the entry point to the program. It gets called by Windows
* to start the program up. This routine creates the main window,
* initializes a timer, and then falls into the main Windows Get
* message/Dispatch message loop.
*
************************************************************************/
int PASCAL
WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR CmdLine, int nCmdShow)
{
MSG msg; /* MSG structure to pass to windows proc */
WNDCLASS wndclass;
char *AppName; /* Name for the window */
cbErrHandling(PRINTALL, STOPALL); /* Set library's error handling */
CmdLine = NULL; /* Not used */
AppName = "WINCDEMO"; /* The name of this application */
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = MainMessageHandler;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, AppName);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = AppName;
wndclass.lpszClassName = AppName;
RegisterClass(&wndclass);
}
/* create application's Main window */
hWndMain = CreateWindow(AppName, /* Window class name */
"AIn Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* Use default X, Y */
CW_USEDEFAULT, /* Use default X, Y */
GetSystemMetrics(SM_CXSIZE) * 12, /* x - fit text */
GetSystemMetrics(SM_CYSIZE) * 10, /* y - fit text */
NULL, /* Parent window's handle */
NULL, /* Default to Class Menu */
hInstance, /* Instance of window */
NULL); /* Create struct for WM_CREATE */
if (hWndMain == NULL)
{
MessageBox(NULL, "Could not create window in WinMain", NULL, MB_ICONEXCLAMATION);
return (1);
}
ShowWindow(hWndMain, nCmdShow); /* Display main window */
UpdateWindow(hWndMain);
// /* Start a 500ms timer to update display */
if (!SetTimer(hWndMain, TIMER_NUM, 50, NULL))
{
MessageBox(NULL, "Error starting Windows timer", NULL, MB_OK |
MB_ICONEXCLAMATION);
return (1);
}
while (GetMessage(&msg, NULL, 0, 0)) /* Main message loop */
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass(AppName, hInstance);
return (msg.wParam);
}
/************************************************************************
*
* Name: MainMessageHandler
*
* Arguments: hWnd - handle to the window
* Message - message that needs to be handled
* hParam - message parameter
* lParam - message parameter
*
* This is the message dispatcher that gets called whenever Windows
* sends a message to this window. WinMain started up a timer that
* sends a message every 1/2 sec. When the message (WM_TIMER)is received
* by this routine, it reads the A/D.
* It also causes a screen update which will automatically generate a
* WM_PAINT message. The WM_PAINT handler takes care of converting the
* raw A/D values to voltages and printing them in the Window.
*
************************************************************************/
LONG FAR PASCAL
MainMessageHandler(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HDC hDC; /* handle for the display device */
PAINTSTRUCT ps; /* holds PAINT information */
TEXTMETRIC tm; /* holds TEXT information */
static HRGN hRgn; /* Rectangle region Handles */
static int CharWidth, CharHeight;
static unsigned short DataVal;
int x, y;
char OutString[80], *str;
float Voltage;
switch (Message) /* Windows Message Loop */
{
case WM_CREATE:
hDC = GetDC(hWndMain); /* Get the device context for window */
GetTextMetrics(hDC, &tm); /* From context, get size of font */
CharWidth = tm.tmAveCharWidth;
CharHeight = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hWndMain, hDC);
hRgn = CreateRectRgn(0, 0, CharWidth * 30, CharHeight * 12);
break;
case WM_TIMER: /* All Timer Events Processed Here */
InvalidateRgn(hWndMain, hRgn, FALSE); /* Force screen update */
break;
case WM_PAINT: /* Repaint client area of window */
hDC = BeginPaint(hWndMain, &ps);
x = CharWidth * 2; /* Position cursor within window */
y = CharHeight; /* One line down and 2 chars in */
str = " A/D Info"; /* Print title */
TextOut(hDC, x, y, str, strlen(str));
y += CharHeight; /* Print current index */
cbAIn(BOARD_NUM, 0, ADRANGE, &DataVal);
y += CharHeight * 2; /* Print raw data value */
sprintf(OutString, "Raw A/D value = %u ", DataVal);
TextOut(hDC, x, y, OutString, strlen(OutString));
y += CharHeight; /* Convert raw A/D to volts and print */
cbToEngUnits(BOARD_NUM, ADRANGE, DataVal, &Voltage);
sprintf(OutString, "Voltage = %.2f ", Voltage);
TextOut(hDC, x, y, OutString, strlen(OutString));
SetTextAlign(hDC, TA_LEFT | TA_TOP);
EndPaint(hWndMain, &ps);
break;
case WM_CLOSE: /* Close the window */
DestroyWindow(hWnd);
if (hWnd == hWndMain)
PostQuitMessage(0); /* Send message to Quit application */
break;
default:
return (DefWindowProc(hWnd, Message, wParam, lParam));
}
return (0l);
}
Instead of randomly roaming around copypasting staff and switching things on and off you should first understand the errors you are facing. For example first error: You should lookup on MSDN what HANDLE and HINSTANCE are and figure out where are you getting them from. You get hInstance from WinMain. You should lookup on MSDN for WinMain and figure out that signature you used is somewhat wrong and you are supposed to use
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
So the error with wndclass.hInstance = hInstance; assignment of HANDLE to HINSTANCE should be gone (assuming that I've pointed the correct line of this error because you only specified row).
And so on.

If I use CreateEx( ... ) after the main window was created I cannot FindWindow( ... )

I have the following code to create a 'message only' window, the window is always created fine, the problem happens when it is created.
Process A
...
auto hInstance = ::GetModuleHandle( nullptr );
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = 0;
wcx.lpfnWndProc = MyWinProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH);
wcx.lpszMenuName = L"MainMenu";
wcx.lpszClassName = L"MyDummyClass";
wcx.hIconSm = (HICON)LoadImage(hInstance,
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
if (!RegisterClassEx(&wcx))
{
throw "Bad 1.";
}
if (!this->CWnd::CreateEx(0, L"MyDummyClass", L"MyDummyWindow", 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr))
{
throw "Bad 2.";
}
Process B
auto myWnd = FindWindow( L"MyDummyClass");
Now the problem I have is if process A create the window before OnInitDialog is called then process B can find the window.
But if process A creates the window at any other time after the main window was created then process B cannot find the window any longer.
In both cases the message pump is called fine, the window is created as expected, I just cannot FindWindow when I create it after the main application is started.
Why would that be, and how can I work around that?
Use FindWindowEx instead:
To find message-only windows, specify HWND_MESSAGE in the hwndParent
parameter of the FindWindowEx function. In addition, FindWindowEx
searches message-only windows as well as top-level windows if both the
hwndParent and hwndChildAfter parameters are NULL.

How do I use LoadIcon and MAKEINTRESOURCE correctly when changing the title bar icon? [duplicate]

This question already has an answer here:
How to change the title bar icon using winapi
(1 answer)
Closed 7 years ago.
I am working on a project in Code:Blocks c++ win32. I have read on this honorable page the use of LoadIcon and MAKEINTRESOURCE. However, it seems I am not using them correctly. I have created an icon with Greenfish and named it 'dvc icon'. It is in the format '.ico'.
When I execute the program I get an error code "can't open icon file 'dvc icon.ico': No such file or directory.
Below is the code.
#include <resource.h>
#define IDI_OWNERDRAW 103
#define IDI_BUTTON_ICO 201
#define IDI_dvc icon 205
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadImage (GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_dvc icon));
wincl.hIconSm = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_dvc icon), IMAGE_ICON, 16, 16, 0);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No me u */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
What should I do next?
Any help is much appreciated. Thanks.
There are the fields hIcon and hIconSm of type HICON in the WNDCLASSEX struct used by RegisterClassEx. It specifies what icon to use in the task bar and in the title bar of the window respectively. You can use LoadImage to get a valid icon handle. Use it together with the MAKEINTRESOURCE to get the icon from an embedded resource.
See the LoadImage documentation for details.

Tooltip is not coming for a control

One of my application is ported from windows Xp to WIN7. For this application tool tip control is not working in win7 while it is working for XP.
Code logic:
We are setting tooltip in a const string. aToolTipText =anImageTypeStr;
and passing in a function SetToolTipText(LPCTSTR tooltiptext, long Id) which is calling ActivateToolTipText(int Id, bool activateFlag) which is actually handling Tooltip based on id.
bool ActivateToolTipText(int Id, bool activateFlag)
{
CSA_TRY
{
// struct specifying info about tool in ToolTip control
TOOLINFO ti;
unsigned int uid = Id; // for ti initialization
LPTSTR lptstr = (LPTSTR)(LPCTSTR)m_strToolTipText[Id];
// CREATE A TOOLTIP WINDOW
if(activateFlag)
{
m_ToolTipHWND[Id] = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
m_hWnd,
NULL,
0,
NULL
);
}
// INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS ;
ti.hwnd = m_hWnd;
ti.hinst = 0;
ti.uId = uid;
ti.lpszText = lptstr;
// ToolTip control will cover the rect of Id
CRect rect_out;
GetIdArea(Id, &rect_out);
ti.rect = rect_out;
if( activateFlag )
::SendMessage(m_ToolTipHWND[Id], TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO)&ti);
}
else
{
::SendMessage(m_ToolTipHWND[Id], TTM_DELTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
}
return TRUE;
}
}
The problem is with common controls version 6. for xp+, the header file CommCtrl.h assume we will use comctl version 6, but if we dont enable it explictly with manifest file, we'll still use the old comctl version 5.x. problem starts here, the size of TOOLINFO of version 5.x is different to version 6.x. So if you need to use comctl version 5 under windows xp+, you should init TOOLINFO with follwing code, TOOLINFO ti; ti.cbSize = sizeof(TOOLINFO) - 4;

Successful build with createWindowEx, window still won't appear

I'm trying to learn some windows and directX programming and I was messing around trying some different things. When suddently my the windows stopped appearing, even tho it was a successful build. I figured I must have messed something up and I undid everything until i got back to the place where I last managed to get the window to appear, but now when I run (with a successful build) it still doesn't show :( And I'm starting to run out of ideas what the problem could be, it so strange. One of the thing I did since last time I got it to work was add some libs directories but I have a hard time seeing how that would affect the program this way. Have anyone of you run in to this problem before, and if so how did you solve it? Here is the code of the func creating the window (and yes I am aware of the infinite loop, it shouldn't cause this problem tho, right?) :
ps. I have also tried changing between WINDCLASSEX and WINDCLASS, with all the functions that need to be change with it, didn't make any difference ds.
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow){
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
RegisterClass(&wc);
RECT wr = {0, 0, 500, 400}; // set the size, but not the position
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); // adjust the size
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"My first window", // Window text
WS_OVERLAPPEDWINDOW, // Window style
CW_USEDEFAULT, CW_USEDEFAULT,//position x,y
wr.right-wr.left, wr.bottom-wr.top,//width, height
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL){
return 0;
}
InitD3D(hwnd);
// Run the message loop.
MSG msg = { };
while (true){
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
}
}
return 0;
}
looks like you need a ShowWindow call in there (unless InitD3D does that, you haven't shown the code)
windows are by default created non-visible, so that you can do various initialization without the user seeing what goes on
as an alternative you can create the window already visible, but generally it's a good idea to keep to a single convention
by the way, you can just use a standard int main, no need to use the Microsoft monstrosity
with GNU toolchain that's all, with Microsoft's tools you then have to tell the linker to accept the standard code, if you use the GUI subsystem, via linker option /entry:mainCRTStartup.
also, the call to non-blocking PeekMessage means your message loop will most likely be a CPU hog
instead, use blocking GetMessage
and remember to exit the loop when GetMessage returns 0 (which indicates a WM_QUIT message has been posted)