mostermand 0 Light Poster

Ok I am building a GUI Framework, where there is a Widget class,
which can both have mother widgets and be a mother widget.

When some event happens, the widget receives a message, in the form of an integer value(win32).
I want the widgets to be able to say to their children "I want to know when this happens", and then the mother widget's mother to be able do the same.

The alternative to this subscribe behavior, is to send some of the messages to the widget's mother

mostermand 0 Light Poster

Too time consuming. I would rather not do it at all then

And about the code snippet, if I create an instance of Mother,
and call HandleMessage<10> it would print "It works!\n".

But if I call it with any other value it would do nothing,
the reason I am not doing this, is that I don't know the values at compile time.

mostermand 0 Light Poster

I would recommend a disassembler, but requires you to know assembly.

mostermand 0 Light Poster

Well it is an integer, compared to a list of integers.

mostermand 0 Light Poster

Hi I have a problem, I hope you can help me with.

I have a number, which I need to compare to some values.

I do not wish to go through all the values, and compare them individually, as I there are too many for it to be efficient.

Neither do I want to create an array with the size of the range of numbers, as it can anything from 0 UINT_MAX

So I am hoping that you might have a better solution.

Here is an example of what I have tried, but didn't work,
because I get the values dynamicly

class Mother
{
public:
    Mother(){}
    ~Mother(){}

    template<int msg> inline void HandleMessage(){};
    
    template<> inline void HandleMessage<10>()
    {
        std::cout<<"It works!\n";
    }
};
mostermand 0 Light Poster

OK last try

class TestA;
#ifndef __TESTA_H
#define __TESTA_H

#include <stdio.h>
#include "testB.h"

class TestA {
  public:
    TestA();
    void testPrint();
    void test();
    void set(TestB* t);
    int getID();

  private:
    int id;
    TestB* b;
};

#endif
mostermand 0 Light Poster

Are you sure you want to have a pointer of the other class in them both?

mostermand 0 Light Poster

You should always include the header when you want to use it's class to avoid multiple inclusion add the following code.

#ifdef TESTB_H
#define TESTB_H

//class declaration

#endif

And remove testA declaration in testB.h

mostermand 0 Light Poster

Include testA.h in testB.h if you want it to have a pointer to a testA object

mostermand 0 Light Poster

put the declaration in the header file and the definition in the other

//declaration
class foo
{
    void bar();
};

//definition
void foo::bar()
{
}
mostermand 0 Light Poster

Possibly the fact that struct keyword mostly is used for containers?

mostermand 0 Light Poster

Well come to think about it I don't know why I wanted to do it.
Maybe I am just tired.

mostermand 0 Light Poster

Thank you for answering the questions I will just assume it is ok to make a main class.

I didn't think non heap variables could be said to be allocated because they are allocated by the OS at startuptime

mostermand 0 Light Poster

Yes I was assuming a destructor was implemented but my question still applies should I allocate all my resources in classes?

As for question number two here is an example of what I mean

int main()
{
    class mainClass
    {
        mainClass()
        {
            //initialization and run
        }
        ~mainClass()
        {
            //clean up
        }
    } Main;
}
mostermand 0 Light Poster

I have been testing a C++ based SDK and now i have a few questions for you that i hope you will answer
.

1. When passing objects between each other is it preffered to use references if not what then?

2. Is it good or bad to make a single object in main that initializes runs and cleans up?

3. Would that be the same as a singleton class without multiple instance protection and would the usual arguments for and against them apply?

4. When testing the SDK i found that member variables of classes was prefixed a m_ what does that mean and why would you do it?

5. When allocating resources should you always do it inside of a class?

Thank you for your time

mostermand 0 Light Poster

Thank You!

this is what I've got.
There are some problems and you can't replace a function but I can handle that myself

class msghandler
{
public:
    void HandleMessage(WNDPROC func, UINT msg);
    LRESULT CALLBACK handle(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
private:
    std::vector<msg_func> msglist;
};

LRESULT msghandler::handle(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    std::vector<msg_func>::iterator iter;

    for(iter = msglist.begin(); iter < msglist.end(); iter++)  //loop until find match
    {
        if((*iter).msg == msg)
        {
            return (*iter).function(hWnd, msg, wParam, lParam); //return user defined function's return value
        }
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);  //return DefWindowProc
}

void msghandler::HandleMessage(WNDPROC func, UINT msg)
{
    msg_func temp;

    temp.function = func;
    temp.msg = msg;

    msglist.push_back(temp);
}
mostermand 0 Light Poster

The problem is that it is the windows API we are talking about and I can't have a predefined function for every windows message.

I was thinking something like obtaining a function pointer and the message number from the user and let that overwrite the default behavior(DefWindowProc()).

so I was thinking maybe there was a way of connecting the function and the number

mostermand 0 Light Poster

Yes but in an automated way so I can add functions at runtime and make it have a value.

I know I could do this with a loop and an if statement but it would be optimal if could check if the message is being handled and abort if not

btw should I use function pointers for this?

mostermand 0 Light Poster

Hi and sorry for the inconvenience

I am trying to make an interface where a function is run given a number(windows API).

I want a default function to be run if it hasn't been overwritten by the user.

So my question is can i assign a number to a function/method
like using this number I am going to access this function.

Any help is deeply appreciated

mostermand 0 Light Poster

I don't think you can't assign values to functions/methods.

I don't know what you want to do but as I see it you use the assignment as a way to make the method do nothing until you find out how to implement the method.

To do this in C++ you can just add an empty body either in the class declaration or in the definition then you can redefine it later

mostermand 0 Light Poster

Just use <iostream> there is nearly no syntax difference and your program might not compile on all compilers

mostermand 0 Light Poster

Use this

ofstream out;
out.open("\\reg.txt");
mostermand 0 Light Poster

Ok i have been thinking.

Would it be possible to intercept the messages sent to the desktop window?

Like after we are done painting the screen go to this program.

mostermand 0 Light Poster

Does the sun rise in the east ?

Why would I care?

If you lie are you telling the truth?

mostermand 0 Light Poster

The one and only "Charles Petzold Programming Windows"

mostermand 0 Light Poster

Try this.

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED BACKGROUND_INTENSITY);

You can change RED to GREEN or BLUE and BACKGROUND to FOREGROUND.

If you want to mix colors you just separate them with a |

mostermand 0 Light Poster

What he meant(i think) is that you could do this. ShellExecute(0, 0, "<program to open program with>", "<file to open>", 0, SW_MAXIMIZE); This is ofcourse not optimal but it can be used as an emergency solution.

mostermand 0 Light Poster

Hi.

I am making an application that will use the screen and do something with it(live).

My problem is that when i do that i get the previous screen made by my program last time i updated the window

what would you suggest i do? any help would be appreciated.

mostermand 0 Light Poster

When you double click the *.pps file does it then open?

If it does try checking what app is used to open *.pps files.

mostermand 0 Light Poster

Thank you everyone.

It worked when i used. SetWindowText(GetDlgItem(hWnd, 112), buffer); It ignores newlines but i think i can handle that.


Hope you had a wonderful christmas

mostermand 0 Light Poster

Hi i am trying to make a notepad like program.

The problem is that when i try to display some text nothing happens.
If someone could help me i would really appreciate it.

The function call:

...
SetWindowText(edit, "TEST");
...

Creation of edit:

...
HMENU Menu, SubMenu;

Menu = CreateMenu();
SubMenu = CreatePopupMenu();

AppendMenu(SubMenu, MF_STRING, 9001, "&Open");
AppendMenu(SubMenu, MF_STRING, 9002, "&Save");
AppendMenu(SubMenu, MF_STRING, 9003, "Save &As...");
AppendMenu(Menu, MF_STRING | MF_POPUP,(UINT)SubMenu,"&File");

SetMenu(hWnd, Menu);

edit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_MULTILINE, 0, 0, 100, 100, hWnd, (HMENU)112, GetModuleHandle(0), 0);
if(!edit)
{
    int DEBUG = GetLastError();
    MessageBoxA(0, "Failed to create edit", "ERROR", MB_OK | MB_ICONERROR);
    ExitProcess(1);
}
SetFocus(edit);
...

The edit is being created in the message loop when a WM_CREATE message. is send

Thanks in advance

mostermand 0 Light Poster

thank you everyone

mostermand 0 Light Poster

For what?

for misunderstanding the concepts of classes i thought that it was the core of an optimal program (like code at bottom of post) is it really only to reuse code?

class someClass
{
public:
    someClass();
    void someFunc();
    void someOtherFunc();
private:
    int someVariable;
    int someOtherVariable;
};

int main()
{
    someClass someObject;
    someObject.someFunc();
    someObject.someOtherFunc();
}
mostermand 0 Light Poster

i am sorry

mostermand 0 Light Poster

ok thank you.

mostermand 0 Light Poster

hi i was wondering how much i should use classes in C++.
Should i use it as much as possible?