Week 3 Unicode and Windows Architecture
Week 3 Unicode and Windows Architecture
1
Unicode
• Unicode is an aspects of C that not encountered in conventional character-
mode programming but that play a part in Microsoft Windows. The subject
of wide-character sets and Unicode almost certainly qualifies in that respect.
• Unicode is an extension of ASCII character encoding. Rather than the 7 bits
used to represent each character in strict ASCII, or the 8 bits per character
that have become common on computers, Unicode uses a full 16 bits for
character encoding.
• This allows Unicode to represent all the letters, ideographs, and other
symbols used in all the written languages of the world that are likely to be
used in computer communication.
• Unicode is intended initially to supplement ASCII.
• In this course, we will use the Compiler default character setting which is
unicode but we will still need to confront ASCII character received from
standard sources e.g from files or network, which needed to be
converted/adapted to unicode processing functions.
2
The char (ANSI) Data Type (1)
• The following statement defines and initializes a variable
containing a single character:
char c = `A' ;
The variable c requires 1 byte of storage and will be initialized with
the hexadecimal value 0x41, which is the ASCII code for the letter A.
• You can define a pointer to a character string like so:
char * p ;
Because Windows is a 32-bit operating system, the pointer variable
p requires 4 bytes of storage.
• You can also initialize a pointer to a character string:
char * p = "Hello!" ;
The variable p still requires 4 bytes of storage as before. The
character string is stored in static memory and uses 7 bytes of
storage—the 6 bytes of the string in addition to a terminating 0.
3
The char (ANSI) Data Type (2)
• You can also define an array of characters, like this:
char a[10] ;
In this case, the compiler reserves 10 bytes of storage for the
array.
The expression sizeof (a) will return 10.
• If the array is global (that is, defined outside any
function), you can initialize an array of characters by
using a statement like so:
char a[] = "Hello!" ;
• If you define this array as a local variable to a function, it
must be defined as a static variable, as follows:
static char a[] = "Hello!" ;
In either case, the string is stored in static program memory with
a 0 appended at the end, thus requiring 7 bytes of storage. 4
Unicode (WideCharacter) (1)
10
Pointers data type
• Defined in WINNT.H
– six data types you can use as pointers to 16-bit
character strings
typedef WCHAR * PWCHAR, * LPWCH, * PWCH,
*NWPSTR, * LPWSTR, * PWSTR ;
– four data types you can use as pointers to const
16-bit character strings
typedef CONST WCHAR * LPCWCH, * PCWCH, *
LPCWSTR, * PCWSTR ;
11
The Windows Function Calls (1)
• In WINUSER.H
– MessageBox identifier is defined
#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif
12
The Windows Function Calls (2)
• In WINUSER.H
– SetWindowText identifier is defined
BOOL WINAPI SetWindowTextA ( HWND hWnd, LPCSTR lpString );
BOOL WINAPI SetWindowTextW ( HWND hWnd, LPCWSTR lpString );
#ifdef UNICODE
#define SetWindowText SetWindowTextW
#else
#define SetWindowText SetWindowTextA
#endif
14
Window Architectural Overview (1)
• A window is a type of object-oriented program
• Types of windows:
– application windows
• contain a title bar that shows the program's name, a menu, and perhaps a toolbar and a
scroll bar.
– dialog box
• may or may not have a title bar
– push buttons, radio buttons, check boxes, list boxes, scroll bars, and text-entry
fields that adorn the surfaces of dialog boxes.
• called "child windows" or "control windows" or "child window controls.“
• A window is always created based on a "window class."
• The window class identifies the window procedure that processes
messages to the window. The use of a window class allows multiple
windows to be based on the same window class and hence use the same
window procedure.
15
Window Architectural Overview (2)
• For example, all buttons in all Windows programs are based on the
same window class.
• This window class is associated with a window procedure located in
a Windows dynamic-link library that processes messages to all the
button windows.
• The user sees these windows as objects on the screen and interacts
directly with them using the keyboard or the mouse.
• The window receives the user input in the form of "messages" to
the window.
• A window also uses messages to communicate with other windows.
• Very often these messages inform a window of user input from the
keyboard or the mouse.
16
Window Architectural Overview (3)
• Every window that a program creates has an
associated window procedure.
• This window procedure is a function that could be
either in the program itself or in a dynamic-link
library.
• Windows sends a message to a window by calling
the window procedure.
• The window procedure does some processing
based on the message and then returns control to
Windows.
17
Components of a Typical Application Window
18
Writing a Windows Program
• Generally, Windows programmers begin a new
program by copying an existing program and
making appropriate changes to it.
• HELLOWIN.CPP is an example program which
we will use to understand about creating a
basic window program.
• Click here to open program.
19
Windows Programming Example
• Write the program“HELLOWIN.CPP” in a Windows project named
“Petzold”.
• Build and RUN the program. You will hear no sound which you should.
• Search for file “Petzold.exe” and open the folder where the file is located.
• Search for file “hellowin.wav” and open the folder where the file is
located.
• Copy “hellowin.wav” to the folder where the file “Petzold.exe” is located.
• Now run the program and you will hear this sound.
• This program creates a normal application window, and displays, "Hello,
Windows!" at the centre of that window. If you have a sound board
installed, you will also hear me saying the same thing.
• For Lecturer use this solution
20