windows 核心编程(二)——窗口的秘密

Window Handles

Windows are objects — they have both code and data — but they are not C++ classes. Instead, a program references a window by using a value called a handle. A handle is an opaque(不透明) type. Essentially(本质上), it is just a number that the operating system uses to identify an object. You can picture Windows as having a big table of all the windows that have been created. It uses this table to look up windows by their handles. (Whether that's exactly how it works internally is not important.) The data type for window handles is HWND, which is usually pronounced "aitch-wind." Window handles are returned by the functions that create windows: CreateWindow and CreateWindowEx.

To perform an operation on a window, you will typically call some function that takes an HWND value as a parameter. For example, to reposition a window on the screen, call the MoveWindow function:

BOOL MoveWindow(HWND hWnd, int X, int Y, int nWidth, int nHeight, BOOL bRepaint);

The first parameter is the handle to the window that you want to move. The other parameters specify the new location of the window and whether the window should be redrawn.

Keep in mind that handles are not pointers. If hwnd is a variable that contains a handle, attempting to dereference the handle by writing *hwnd is an error.

Screen and Window Coordinates(坐标)

Coordinates are measured in device-independent pixels. We'll have more to say about the device independent part of device-independent pixels when we discuss graphics.(用像素来衡量)

Depending on your task, you might measure coordinates relative to the screen, relative to a window (including the frame), or relative to the client area of a window. For example, you would position a window on the screen using screen coordinates, but you would draw inside a window using client coordinates. In each case, the origin (0, 0) is always the top-left corner of the region.

 

Illustration showing screen, window, and client coordinates

Illustration(图示) showing screen, window, and client coordinates


WinMain: The Application Entry Point(入口函数)

Every Windows program includes an entry-point function that is named either WinMain or wWinMain. Here is the signature for wWinMain.

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);

The four parameters are:

  • hInstance is something called a "handle to an instance" or "handle to a module." The operating system uses this value to identify the executable (EXE) when it is loaded in memory. The instance handle is needed for certain Windows functions — for example, to load icons or bitmaps.
  • hPrevInstance has no meaning. It was used in 16-bit Windows, but is now always zero.
  • pCmdLine contains the command-line arguments as a Unicode string.
  • nCmdShow is a flag that says whether the main application window will be minimized, maximized, or shown normally.

The function returns an int value. The return value is not used by the operating system, but you can use the return value to convey a status code to some other program that you write.

WINAPI is the calling convention. A calling convention defines how a function receives parameters from the caller. For example, it defines the order that parameters appear on the stack. Just make sure to declare your wWinMain function as shown.

The WinMain function is identical(相同的) to wWinMain, except the command-line arguments are passed as an ANSI string. The Unicode version is preferred. You can use the ANSI WinMain function even if you compile your program as Unicode. To get a Unicode copy of the command-line arguments, call the GetCommandLine function. This function returns all of the arguments in a single string. If you want the arguments as an argv-style array, pass this string to CommandLineToArgvW.

How does the compiler know to invoke wWinMain instead of the standard main function? What actually happens is that the Microsoft C runtime library (CRT) provides an implementation of main that calls either WinMain or wWinMain.

Note  The CRT does some additional work inside main. For example, any static initializers are called before wWinMain. Although you can tell the linker to use a different entry-point function, use the default if you link to the CRT. Otherwise, the CRT initialization code will be skipped, with unpredictable results. (For example, global objects will not be initialized correctly.)

Here is an empty WinMain function.

INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR lpCmdLine, INT nCmdShow)
{
    return 0;
}

Now that you have the entry point and understand some of the basic terminology and coding conventions, you are ready to create a complete Window program.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值