Using Windows Form in Win32 applications - windows

I need to create a Win32 application. I am using visual studio. I really like designing my application in Windows Form, which allows graphically designing my window.
However, I cannot use Windows Form (only). I have to use Win32 applicaition. Now, when I create Win32 application, I cannot figure to graphically design my window.
Is there a way to incorporate Windows Form in Win32 application? Or graphically design Win32 window?
(If there is a way to design Win32 applications GUI graphically and it's just me who could not figure it, I would appreciate resources or methods to do so.)

In a Win32 application, you can include a dialog resource that contains controls and layout information for a particular window. Visual Studio includes a graphical dialog resource editor, as do many other Win32 resource editing applications. This will be somewhat similar to what you're used to with the WinForms designer, albeit with a few limitations. Win32 is a much older technology than WinForms and wasn't really designed with RAD (rapid application development) principles in mind.
When you create a new Win32 project based on the template in Visual Studio, you already get one dialog resource created for you: the About box. If you double-click on your "ProjectName.rc" file in the Solution Explorer, then expand the "Dialog" resources folder, you'll see it. Double-clicking on it will bring up the Dialog Editor for this dialog window. You can add controls using the familiar Toolbox window, and customize them using the familiar Properties window.
You will still need to write code that displays the dialog window, of course. There are two basic methods of doing so:
The DialogBox function will display the dialog modally, which means that the user has to close the dialog before they can interact with the rest of your application. Inconvenient for the user, but rather convenient for the programmer because Windows runs the dialog's message loop for you.
The CreateDialog function will display a normal (non-modal) dialog, but requires that you write your own message loop for the dialog window, just as you would for a normal non-dialog window.
You could conceivably design all of the windows for your application this way, by adding separate dialog resources for each of them, but it is not necessarily a good idea. Part of learning a new UI framework is learning how to use that framework the way it is meant to be used. Creating controls and setting their properties through code at runtime is not really that difficult, and it is a lot more powerful than limiting yourself to a fixed layout. (In fact, even when you have a dialog resource with a fixed layout, you'll often want to write similar code to allow customizing those controls at runtime.)

Related

Does anyone know what this is called on Visual Studio?

I want to figure out what this is called on visual studio.
I am trying to develop a small program that shows different lists depending on the time. This list references a database
I am trying to change the list that appears when i right click the program on the background task. Similar to this example where i right click outlook's background app and receive a list
This (system tray icon and its context menu) dates back at least two decades, and has been extensively covered in Windows programming books a long while ago.
You can still find legacy materials like NotifyIcon in Windows Forms,
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/notifyicon-component-windows-forms?view=netframeworkdesktop-4.8
If you are going to write your own program, make sure you choose a proper UI framework first (Windows Forms is too old but still supporteD), and then use the equivalent control to achieve the same functionalities.

Program to drive a windows application

I would like to know is there a way to drive an existing windows application? I want to execute operations in an application like filling out text fields in a form, hitting next and submit buttons, etc. Basically what a user would do, I wanted to automate those operations. What would be the best way to achieve this?
Thanks
Mukul
It is possible (with limitations and quirks), if that particular Windows application uses native windows (so-called) controls (UI elements). Qt, for example, paints UI elements "by hand", while MFC applications uses Win API UI native (and expanded) elements. So, it depends.
You can explore application and it's UI elements using Spy++ tool inside Visual Studio (there are free alternatives available). Using these tools, you can look up target window class name, ID and other attributes that would help you to find and identify elements of interest using Windows API functions.
One can use EnumDesktopWindows, FindWindowEx, FindWindow, and others, to find window and it's inner control of your interest. Then, using SendMessage you can send various messages to set focus, emulate mouse clicks, set text for Edit control, simulate button clicks, etc, etc.
You can write such a program using UI Automation, which allows a program to discover and use the GUI of another application. It's how accessibility tools like screen readers interact with your applications.

MFC: Address Bar control like Windows Explorer

In my MFC app, I'm attempting to make a window that resembles the Windows 7 Open File dialog, but it browses a virtual/fake file system. It doesn't need to be pixel-perfect, but I'd like parity with the native OS dialog where possible.
Probably the most challenging part is the address bar the runs along the top of an Open dialog. The address bar control is also atop all Windows Explorer windows. It shows the folder names that make up your path. It shows and hides buttons when moused over (including an attractive fade animation), changes the active directory when names are clicked, and shows submenus when the triangles between names are clicked. This doesn't seem to correspond to any MFC control (or group of controls). Spy++ shows it as an "AddressDisplay Control" but I can't find much documentation beyond that.
Is there a way to access a control like this, or to mimic it, in MFC? Also, I am not browsing the real file system, so I have to be able to tell the control what to display--I can't just point it at C:\ and let the system do the rest.
Here's a picture of the control in question.
Unfortunately, I think this is one of those controls that Microsoft has decided not to expose to developers through the Feature Pack. The Feature Pack was developed from the BCG control library. And, that library contains the control you want. However, it's not free. The only other alternative is to code it yourself.

Dialog as main Window?

Is it usual to use a Dialog as main Windows? So without registering any user class via RegisterClassEx? Can I do everything I do via CreateWindow()? Why should I create controls such as buttons,editboxes etc via CreateWindow() instead of just making a Dialog and use it as main Window?
I'd also like to know main difference between a dialog and a windows and why use one the first instead of the second.
Thanks
Is it usual to use a Dialog as main Windows?
Yes, it is quite common.
So without registering any user class via RegisterClassEx?
A dialog is usually a predefined window class, so there usually no need for registering.
I'd also like to know main difference between a dialog and a windows and why use one the first instead of the second.
Well, two big differences would be that you cannot resize a dialog box and it has no minimize or maximize buttons (by default, but there are workarounds for this). Keep in mind the name, dialog box. In other words they are used for having a dialog with the user (receive input and displays messages to user). In a sense they are just like any other window, underneath CreateWindowxx, etc. is called, etc. However, they are somewhat predefined windows which can be made quickly and there are limitations to what you can do with them.
Also, a dialog uses a dialog procedure rather than a window procedure, which does some default processing for you, such as initializing some controls, etc.
Yes, an application can be dialog-based. There's even a Wizard for that if your'e using VisualStudio and MFC.
In VS2010, Create New Project > MFC Application. In "Application Type" select Dialog Based. Click through the rest of the Wizard, and you're off to the races.
Dialog-based applications are much simpler, architectually, than other designs such as Document/View. As such, simple things are much easier to "bang out" quickly, but the limitations of the design become apparent when you try to do more complex things. You could end up replicating much of the Doc/View architecture in your dialog-based app in order to build a production-quality Dialog-based application. In that case, did you really save yourself anything?
A dialog is a kind of window just as all of the various controls like buttons are really just windows. You can think of a dialog as being a kind of window with a lot of extra functionality to support the kinds of things that dialogs are used for.
There are two types of dialogs, modal which display and expect you to use them and then dismiss them, and non-modal which display but which do not capture and keep the input focus until they are dismissed. You can see these two types used in applications where a modal dialog is used to display an error or require the user to make some setting and a non-modal acts as a kind of tool box that stays displayed and when you need it, you click on it to do something and other times you are using some other window in the application.
Normally a dialog would not have a menu bar but would instead have all of its controls visible or easily accessible via tabs or some other type of presentation. Visual Studio and other IDEs have dialog designers to allow the placement of various controls along with wizards to allow the controls to be tied to classes and class members.
Which brings up a major difference between a dialog and a window. A window is kind of an empty page and to do things with the page requires more work. A dialog has tools that make the design easy however you are also constrained in large part by the toolbox.
If you have an application that is focused on basically allowing a user to specify certain settings and then do some task, a dialog works fairly well. If you have something that requires more complicated user interaction, an application window as the base from which all of your other dialogs and controls will be managed and manipulated will be more necessary.

Win32: CreateDialog instead of multiple calls to CreateWindow - any downsides?

I'm currently working on a Win32 program which requires a main window containing many child window controls - buttons, listviews and so on. I believe the standard way to build such a window is to first call CreateWindow for the main window, then again for each of the controls.
As an easier option, I'm considering designing the main window using the resource editor's dialog box designer, then using CreateDialog to build the main window in one go.
By using a CLASS statement in the dialog box template, I should be able to get the main window to use a custom window class (and hence a custom window procedure) and thus avoid the window having any dialog-like behaviour. An example of this technique can be found in Charles Petzold's "Programming Windows": the HEXCALC program in chapter 11.
Are there any downsides to creating my main window in this way? If so, what are they? If not, why is this approach rarely used?
You don't get control of your main window message loop - the dialog manager handles it for you. On the other hand, the dialog manager handles keyboard accelerators, tab ordering and a number of other effects.
You'd be surprised what you can do with a standard dialog box - the windows volume control is implemented with about four different dialog boxes - it has a frame dialog box which in turn host hosts a tray window which in turn holds volume control dialog boxes, one for each app volume.
The only downside of CreateDialog I know of (as compared to repeated CreateWindow, not talking about some heavyweight framework, just Win32 vs Win32) is that dialog resources position child windows using dialog units. So the layout is dependent not only on DPI, but also on the user's theme settings (choice and size of font).
If any of your controls need to have fixed sizes in terms of pixels, you won't be happy with the dialog-provided positioning and will need to go through and move all the child windows after the fact.
So yes, you can use CreateDialog as a shortcut for creating a bunch of windows with specified classes and styles. But no, you can't do your layout in the dialog editor.
OTOH, you could store the DLU <-> pixel conversion used on your design machine, and then learn enough about parsing the DIALOG resource internal format to pull out the positioning information, then convert to pixels and correct the positioning in more automated fashion.
You will be able to have the full control over your window, even if it was created with CreateDialog.
Normally, when you create your own window (of your class), the window procedure used is the one that you registered with the class. OTOH windows created via CreateDialog will have the dialog standard window procedure (DefDlgProc), which will mostly invoke your supplied "dialog handler".
If you want to have full control of all the messages you may replace the window proc of the newly created window right after its creation. Just call SetWindowLongPtr with GWLP_WNDPROC parameter. Still, you may do the auto processing of some dialog-specific things by calling IsDialogMessage within your procedure.
There is no downside whatsoever.
Why is it rarely used? Because:
People normally use DialogBox instead, since that is easier for simpler cases.
For more complex cases, people use things like MFC or ATL (or some external library like GTk or Qt), and don't bother with native Win32 graphics.
There are no downsides using the Windows SDK, internally libraries like MFC use the Windows SDK .
People tend to use libraries like MFC over Windows SDK, as libaries have the readymade stuff. However Windows SDK calls are faster than library calls, so in some situations developers call Windows SDK directly .
CButton btnOk ;
btnOK.Create(_T("Press Me"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(100,100,300,300), pParentWnd, 1);
is similar to the following code ,
HWND hWnd = CreateWindow("BUTTON","Press Me",WS_CHILD|WS_POPUP|BS_DEFPUSHBUTTON,100,100,300,300,NULL,NULL,GetModuleHandle(NULL),NULL);
ShowWindow(hWnd,SW_SHOW);

Resources