Winapi Multiple Windows same WindowProc - c

I am developing a GUI with WINAPI and I've a question.
I made a custom progress bar with the respective Procedure for handling it's messages.
I paint the progress bar myself. For the progress bar percentage I use a static variable that I update using a custom message and then I repaint the progress bar by using InvalidateRect.
Now how could I optimize my code so I could create multiple windows of my ProgressBar class.
The problem is that I can't use that same static percentage variable for all of them! So each instance should have it's own percentage variable.
Thank you

All windows have at least one pointer-sized user data variable that you can use for whatever purpose you like - it is accessed via GetWindowLongPtr/SetWindowLongPtr with the index GWLP_USERDATA.
Additionally, when you register a window class, you can specify additional user data to be allocated for each window in your class, using the WNDCLASS member cbWndExtra. For example, if you set this to sizeof(DWORD_PTR) when you registered your class, you could also store a DWORD_PTR-sized value using SetWindowLongPtr with index 0.
Depending on how much data you want to store per-window, you can store it directly using the above methods, or allocate your own structure and store a pointer to it (remembering to free the data when the window is destroyed).
An additional method of storing data per-window is using window properties via the SetProp and GetProp functions, which let you store one or more pointer-sized name/value pairs.

Dont make the percentage variable static. Make it part of the class and read/write from getter/setters

Related

Handling variables across multiple UIs in CodenameOne

I have a following scenario:
I have two forms (UI). From the first UI, user is navigated to the second one.
In the second UI, I have a label and a button. When user presses the button, the text of label is increased (from 1,2,3, and so on.)
I would like to achieve this by adding a integer variable whose value is 0 initially. When I press the button, I will increase the integer value by 1 and set the integer text in the label.
This logic may make no sense but I am trying out just to be clear on concept.
My question is, where should i keep this variable. Usually, we have separate classes for each UIs. On each UI, we declare necessary variables which are private and not visible to other classes and use that variable within that UI class.
But in code name one, the structure seem to be different. We have a class StateMachine, which handles all the things of all the UIs (like click event, page load event, etc.). It seems we do not have any privacy between the UIs.
Is there any specific way that I can use different classes for each UIs?
The StateMachine is just the GUI Builder way, you can also create a "manual" project and code all the UI by yourself.

Best practice passing Linq2Sql between WPF-Forms (Windows)

I often have the situation that I need to pass a linq2sql object from one WPF window to another one.
For example I have a window with a list of linq2SQL objects. The list is bound from a public declared DataContext in the "Window_Loaded"-event. With a double click on one of these items a second window opens and the object can be edited. While opening the selected object is passed to a property of the second window. After the user has made some changes he decides to discard the changes and closes the second window.
Now because the input fields are bound directly to the Linq2SQL object the changed values are still present.
What is the best practice in this situation? Is it better to pass a newly created object from a new created DataContext to the second window? Then I have to somehow refresh the list on the first window when the changes are wanted.
Or can I use the already bound object from the list of objects from the first window and pass it directly to the second window?
I know that I can refresh the object when I have to reload the object
DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, MyObject);
What is the best practice?
The question is more general and touches the important issue: what lifetime policy best suits the linq2sql datacontext in different types of applications.
In case of a web application, this question has a simple answer: the "per-httprequest" lifetime policy is the most convenient.
In your case, a WPF application, there are two approaches I am aware of:
in a "per-the-lifetime-of-the-application" policy you would create a single data context for the lifetime of your application. Although technically this works, there are potential issues with the memory consumption - as the application retrieves more and more data, the first level cache in the data context grows without control and could possibly just run out of resources at some point
in a "per-presenter (view)" policy you create a new data context in each of your presenter (view model) (or view, if you don't follow mvvm) which means that two different views do not share the same context. I'd say this is recommended approach, there are no risks of unwanted resource issues, however, you need an additional mechanism to pass events between views so that views could be refreshed when the data changes.
Note, that all your business logic should be unaware of the actual policy. To do so, let your data context be injected (by constructor for example) to any class that uses it. Depending on the actual policy, a proper context is injected into a business class. This way, you could even change the lifetime management policy someday with no need to refactor your business code.

Gtk+Glade: widgets using same signal handler. How to differ them from each other?

Now that I'm having a window including many buttons which perform similar behavior so it's natural to use same signal handler for them. But however after the signal happens I just can't differ them from each other. Yes, I have the pointer to the object, but I can't tell is it the first button, the second button, or something else.
I supposed The 'Name' attribute (I set it to 'togglebutton1') in glade can be obtained with gtk_widget_get_name function, but I'm wrong, I got 'GtkToggleButton' instead. The xml file says: <object class="GtkToggleButton" id="togglebutton1">.
So is there anyway to make use of the 'id' property, or is there any better solution to identify thoses buttons? thanks a lot.
The reason you're getting a value back like "GTKToggleButton" is because you have not manually set the name using gtk_widget_set_name. According to the GTK documentation:
Widget names are the type of the widget by default (e.g. "GtkButton") or can be set to an application-specific value with gtk_widget_set_name().
So use gtk_widget_set_name to make customized names for your buttons, and if you need more specific differentiation between objects, use gtk_widget_path to get the full path-name for the object. i.e., It will return the full window hierarchy, allowing you to differentiate between similarly named buttons in separate windows, or allow you to group buttons by their parent windows names, etc.
Edit: You can also use gtk_buildable_get_name in order to get the actual ID value of a widget from a XML UI definition file. So that's another option if you want to avoid the use of gtk_widget_set_name. I'm assuming you're XML UI definition is GTKBuilder compatible.

Can a thread be accessed from two forms in a Windows forms application

I wish to run a thread to update the Image in the picturebox . The image is streamed form the camera.
I require to stream images to two pictureboxes in two different forms, but one at a time.
Is it possible to create a single thread which can be accessed by both forms.
I think a backgroundworker would be appropriate. But how do I update the images in the picturebox of the respective forms?
I am using VC++ CLI/CLR
A Thread is an object which represent an independent path of execution (often run in parallel to another). I'm not really sure what you mean by "calling" a Thread but you can instantiate separate threads and run methods on them. Then between the Threads you have created you can use some kind of synchronisation such as Monitors, Mutexes and Events and a shared resource (being careful with cross-thread access).
For your problem I would be more tempted to use some kind of subscription pattern where the class which receives the images from the camera can update any observers of the camera. You may want an interface called ICameraObeserver with a method such as ReceiveImage, then any class could register with your camera class via some kind of method:
public void Register(ICameraObserver ico)
Then when the camera receives a new image, it can iterate through any subscribers of type ICameraObserver and call ReceiveImage passing the image it just received.
Just an idea. Be careful with updating the UI if you have multiple threads running - there is lots of information on this.
What I think you wanna do is this: when creating the new form, send to the constructer the first form as an object , then , and make a setter/ getter or just make the thread public, then you can "access" it from both forms as you requested.

Need to get back form controls' information externally

Are there any tutorials or guides out there that anyone knows of that will show me how to read forms from an external program and get back information about the controls on the form? Currently, I can get the handle to the form, and I can get the class name, but I need to get more information such as a persistent name and contained data. Thanks.
Edit:
I now have a way to read the contained data (with the WM_GETTEXT message), however, I still need a persistent name/ID that I can be sure will not change from instance to instance. One way I can think of for doing this is to take the handle, find the position of the control on the window, and then get the handle from the position from then on. Another way is to determine a static ID for the control and then use that to get the handle from then on. The new scope of my problem is how to implement either of these. Any Ideas?
I would look at UI Automation; in particular, the RuntimeID property, the NativeWindowHandle property, and the Name property.

Resources