V.D
V.D

Reputation: 403

Can we get the class name of NSWindow which has been open?

We have the code to get the class name for c# like

// Get Window class
var windowClass = new StringBuilder(256);
Win32.GetClassName((IntPtr) hwnd, windowClass, windowClass.Capacity);
 String windowClassName = windowClass.ToString();

How to convert it in objective c?

Upvotes: 0

Views: 411

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96323

Short answer: There is no Mac version of that code, because a window does not have a “class name” in this sense on the Mac. You're asking about a Windows API detail that the Mac doesn't have.


Anoop Vaidya's answer is correct for the Objective-C meaning of “class”, but not for Windows's meaning of “window class”.

Here's what a window class in Windows looks like. A window class defines a lot of things; here are just some of them:

  • lpfnWndProc: A pointer to the window procedure. You must use the CallWindowProc function to call the window procedure. For more information, see WindowProc.

  • hIcon: A handle to the class icon. This member must be a handle to an icon resource. If this member is NULL, the system provides a default icon.

  • hCursor: A handle to the class cursor. This member must be a handle to a cursor resource. If this member is NULL, an application must explicitly set the cursor shape whenever the mouse moves into the application's window.

  • hbrBackground: A handle to the class background brush. [lots and lots of details follow on that one]

  • lpszMenuName: Pointer to a null-terminated character string that specifies the resource name of the class menu, as the name appears in the resource file. If you use an integer to identify the menu, use the MAKEINTRESOURCE macro. If this member is NULL, windows belonging to this class have no default menu.

  • hIconSm: A handle to a small icon that is associated with the window class. If this member is NULL, the system searches the icon resource specified by the hIcon member for an icon of the appropriate size to use as the small icon.

This is a very different thing from an Objective-C class.

A class (as Objective-C uses the term) is the recipe of an object. The class defines what instance variables (members), properties, and methods any object of that class possesses. Every object has a class associated with it; classes in the Objective-C sense are not specific to window objects.

A “window class” in Windows, by contrast, defines a lot of window-specific properties. Let's look at the ones above and see what their Mac equivalents are:

  • The window procedure: From what I can tell, defines how the window looks and responds to events. The rough Mac equivalent would be the window object's class (in the Objective-C sense): NSWindow or any subclass thereof. There are not many NSWindow subclasses and next to no custom ones, because we can customize most aspects of individual windows without having to create a custom NSWindow subclass.
  • The icon (small and otherwise): Most windows on the Mac don't have one. Document windows do, in which case it's called a proxy icon, because the icon in the title bar is a proxy for the file that the window is displaying. You set a window's proxy icon by telling it what file the icon should be a proxy for.
  • The cursor: On the Mac, you set the cursor at the view level. If you wanted the same cursor throughout the entire (content area of the) window, you'd set it in the window's content view.
  • The background brush: Every window has a backgroundColor property, which takes an NSColor object. NSColor, in turn, provides numerous predefined background colors for use as window backgrounds (among other purposes).
  • The menu: On Windows, every window has its own menu bar, and they all have different menus in them. On the Mac, there is one menu bar, and most applications never change the menu bar's contents between windows. (When they do, it's very often indicative of a port whose authors did not care enough to respect platform differences.) You could do that, but you would have to do it yourself by changing the contents of the application's mainMenu when the main window changes.

Most of the other members of a “window class” are more Windows API details that have no Objective-C equivalent.

You should read through Apple's OS X Human Interface Guidelines. There are a lot of differences in how windows (among other things) work between Windows and the Mac, and those interface choices inform the entire API design all the way down.

Upvotes: 2

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

NSString *className = [objectName className];

This will give you the class-name of the object named objectName.

Similarly you pass the object, and get the class-name.

Upvotes: 1

Related Questions