windows programming - warsaw university of...
TRANSCRIPT
Windows Programming
Krzysztof Mossakowski, MSc
http://www.mini.pw.edu.pl/~mossakow
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 2
Contents1. Windows, messages, time, errors
2. Types, structures, macros, the mouse, the keyboard, versions
3. GDI
4. Resources, dialog boxes, controls, scrolling
5. The application, forms, events, dialog boxes
6. Containers, controls
7. Custom controls Introduction, XAML, the application
8. Controls, events, resources
9. 2-D and 3-D Graphics
10. Multimedia, animation
11. DLL libraries, the clipboard, the registry, printing
12. The memory, processes and threads, the file system
13. Windows Shell, visual styles
14. Windows Mobile
15. International applications, GUI guidelines
WIN32 API
WINDOWS FORMS
INFORMACJE OGÓLNE
WPF
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 3
Bibliography Win32:
R. Simon, Microsoft Windows 2000 API SuperBible, Sams, 2000
C. Petzold, Programming Windows, 5th Ed., Microsoft Press, 1998
J. Richter, C. Nassarre, Windows via C/C++, 5th Ed., Microsoft Press, 2008
Windows Forms:
M. MacDonald, Pro .NET 2.0 Windows Forms and Custom Controls, Apress, 2006
C. Petzold, Programming Microsoft Windows with C#, Microsoft Press, 2002
I. Serban et al., GDI+ Custom Controls with Visual C# 2005, Packt Publishing, 2006
Windows Presentation Foundation:
C. Anderson, Essential Windows Presentation Foundation, Addison-Wesley, 2007
M. MacDonald, Pro WPF in C# 2008, 2nd Ed., Apress, 2008
S. Noble et al., WPF Recipes in C# 2008, Apress, 2008
C. Petzold, Applications = Code + Markup: A Guide to the Microsoft Windows Presentation Foundation, Microsoft Press, 2006
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 4
Programming Tools for Windows Win API
Win16 - up to Windows 3.1
Win32s - Windows 3.1
Win32 - since Windows 95 and all versions based on NT
Visual Basic
Visual C++
MFC (Microsoft Foundation Class Library)
WTL (Windows Template Library)
Visual J++
.NET Windows Forms (.NET Framework 1.0+)
Windows Presentation Foundation (.NET Framework 3.0+)
Delphi
Builder C++
Borland Developer Studio
Qt
GTK+
wxWidgets
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 5
Win32 API Application
#include <Windows.h>
int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow )
{...MyRegisterClass(hInstance);...InitInstance(hInstance, nCmdShow);...while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);DispatchMessage(&msg);
}
return (int)msg.wParam;}
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 6
The Window Class
It defines window’s features (the most important is the window procedure)
Types of windows’ classes:
system (e.g. Button, ComboBox, ScrollBar, MDIClient)
global in application – available for all modules
local in application – without the CS_GLOBALCLASS flag
To register window’s class, call one of functions:
RegisterClass(CONST WNDCLASS *lpWndClass)
RegisterClassEx(CONST WNDCLASSEX *lpwcx)
Information about a class: GetClassInfoEx(), GetClassLong()
Modification of a class: SetClassLong()
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 7
Attributes of the Window Class
lpszClassName [to get use the GetClassName() function]
lpfnWndProc
hInstance
hCursor [or use: SetCursor(), WM_SETCURSOR]
hIcon, hIconSm [or use the WM_SETICON message]
hbrBackground [or use the WM_ERASEBKGND message]
lpszMenuName
style [e.g. CS_DBLCLKS]
cbClsExtra [SetClassWord(), GetClassWord()]
cbWndExtra [SetWindowLong(), GetWindowLong()]
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 8
Registration of a Window ClassATOM MyRegisterClass(HINSTANCE hInstance){
WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = (WNDPROC)WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(hInstance,
(LPCTSTR)IDI_MY001);wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = (LPCTSTR)IDC_MY001;wcex.lpszClassName = szWindowClass;wcex.hIconSm = LoadIcon(wcex.hInstance,
(LPCTSTR)IDI_SMALL);return RegisterClassEx(&wcex);
}
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 9
The CreateWindow Function
HWND CreateWindowEx(DWORD dwExStyle,LPCTSTR lpClassName,LPCTSTR lpWindowName,DWORD dwStyle,int x,int y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HINSTANCE hInstance,LPVOID lpParam
);
HWND CreateWindow(
LPCTSTR lpClassName,LPCTSTR lpWindowName,DWORD dwStyle,int x,int y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HINSTANCE hInstance,LPVOID lpParam
);
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 10
Window Creation [VS .NET]
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{HWND hWnd;hWnd = CreateWindow(szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd) {return FALSE;
}
ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);
return TRUE;}
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 11
Messages
The basic rule for all Windows applications is to respond to events generated by the system and the user
All messages sent to the window are handled in the window procedure
The message:
parameters: hWnd, identifier, wParam, lParam
MSG structure
Types of messages:
system
WM_USER + _
WM_APP + _
registered (RegisterWindowMessage())
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 12
Message Queues
The message queue is a FIFO structure stored in system memory
Queued messages:
user interface, e.g. WM_MOUSEMOVE, WM_CHAR
WM_TIMER, WM_PAINT, WM_QUIT
Functions for queued messages:
PostMessage(), PostThreadMessage()
GetMessage(), PeekMessage(), DispatchMessage()
GetMessageTime(), GetMessagePos()
WaitMessage()
SendMessageExtraInfo(), GetMessageExtraInfo()
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 13
Nonqueued Messages
Routed directly to the window procedure
Examples:
notifications for a window, e.g. WM_ACTIVATE, WM_SETFOCUS, WM_SETCURSOR
actions from functions, e.g. WM_WINDOWPOSCHANGED after SetWindowPos()
Functions sending nonqueued messages:
SendMessage(), SendMessageCallback()
BroadcastSystemMessage(), BroadcastSystemMessageEx()
SendMessageTimeout()
SendNotifyMessage()
SendDlgItemMessage()
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 14
The Message Loop The GetMessage() function returns FALSE when WM_QUIT is
taken from the message loop
The PostQuitMessage() function sends the WM_QUIT message to the queue
Functions modifying messages:
TranslateAccelerator()
TranslateMessage()
IsDialogMessage()
while (GetMessage(&msg, NULL, 0, 0)) {if (TranslateAccelerator(msg.hwnd,
hAccelTable, &msg)) {TranslateMessage(&msg);DispatchMessage(&msg);
}}
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 15
The Window Procedure
It is a function which processes all messages sent to the window
The window procedure is common for all windows of one window class
The window procedure takes 4 parameters: HWND, UINT, WPARAM, LPARAM
It returns a value of type LRESULT (specific for a message)
The default window procedure: DefWindowProc()
Changing the window procedure (subclassing)
for one window: SetWindowLong(), GetWindowLong()
for a window class: SetClassLong(), GetClassLong()
use CallWindowProc() to handle a message
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 16
Window Procedure SampleLRESULT CALLBACK WndProc(HWND hWnd,
UINT message, WPARAM wParam, LPARAM lParam){
int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;switch (message) {
case WM_PAINT:hdc = BeginPaint(hWnd, &ps);EndPaint(hWnd, &ps); break;
case WM_DESTROY:PostQuitMessage(0);break;
default:return DefWindowProc(
hWnd, message, wParam, lParam);}return 0;
}
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 17
Window’s Elements
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 18
Types of Windows
Overlapped
a top-level window with a title, border and client area
in most cases, it is the main window of the application
overlapped windows have the WS_OVERLAPPED or WS_OVERLAPPEDWINDOW style applied
Pop-up
dialog boxes, message boxes, and other windows positioned outside the main window
the WS_POPUP lub WS_POPUPWINDOW style must be applied
Child
Layered [2000+]
they support opacity and transparency
the WS_EX_LAYERED must be applied
Message-only [2000+]
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 19
Child Windows Confined to the parent window’s client area
They always have the WS_CHILD style applied
Clipping - WS_CLIPCHILDREN, WS_CLIPSIBLINGS
Parent windows
all child windows are moved, hidden and destroyed with their parents
SetParent(), GetParent(), GetAncestor()
EnumChildWindows(), IsChild()
Messages
sent directly to the child window’s procedure
sent to a parent when the child window is disabled
notification messages are sent from a child to the parent window
The WS_EX_LAYERED style does not work for child windows
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 20
Window Relationships Foreground, background
the foreground window is a non-child window with which the user is currently working
GetForegroundWindow(), SetForegroundWindow()
Z-order
the order of drawing overlapping windows
WS_EX_TOPMOST
BringWindowToTop(), SetWindowPos(), DeferWindowPos()
GetTopWindow(), GetNextWindow()
The owned window
always above its owner in the Z-order
destroyed with its owner
hidden when its owner is minimized
CreateWindowEx(), GetWindow() with GW_OWNER
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 21
Active and Disabled Windows Activation
the active window – the user is currently working with it (from the same thread as the foreground window)
SetActiveWindow(), GetActiveWindow(), SetWindowPlacement()
WM_ACTIVETEAPP, WM_ACTIVATE
Disabling
a disabled window receives neither keyboard nor mouse input
it receives messages from other windows, applications and the system
WS_DISABLED
EnableWindow(), IsWindowEnabled()
WM_ENABLE
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 22
Visible, Minimized, Maximized Windows
Visibility
hidden windows are not drawn
they have the WS_VISIBLE style
IsWindowVisible(), ShowWindow(), SetWindowPos(), DeferWindowPos(), SetWindowPlacement(), SetWindowLong(), ShowOwnedPopups()
WM_SHOWWINDOW
Minimized, maximized and restored windows
WS_MINIMIZE, WS_MAXIMIZE
IsZoomed(), IsIconic(), GetWindowPlacement()
CloseWindow(), ShowWindow(), SetWindowPlacement()
WM_QUERYOPEN, WM_GETMINMAXINFO
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 23
Size and Position of a Window To use the default position and size, apply the CW_USEDEFAULT
constant in the window class
System commands are sent as WM_SYSCOMMAND
Functions:
MoveWindow(), SetWindowPos(), SetWindowPlacement(),
CascadeWindows(), TileWindows()
GetWindowRect(), GetClientRect(), AdjustWindowRect()
ScreenToClient(), ClientToScreen(), MapWindowPoints()
WindowFromPoint(), ChildWindowFromPoint()
Messages:
WM_WINDOWPOSCHANGING, WM_WINDOWPOSCHANGED
WM_SIZE, WM_SIZING, WM_MOVE, WM_MOVING
WM_NCCALCSIZE
WM_GETMINMAXINFO
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 24
Destruction of a Window
Actions taken by the system during destruction of a window:
hiding the window (if it is visible)
removing window’s internal data
invalidating the window’s handle
The DestroyWindow() function:
1. sends the WM_DESTROY message to the window
2. sends the WM_DESTROY message to all descendant windows
The WM_CLOSE message:
is received when the user clicks the close button (X)
an application can prompt the user for a confirmation
the DestroyWindow() function can be called to destroy the window
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 25
The Most Useful Window Styles
The type of a window:
WS_CHILD, WS_OVERLAPPED, WS_POPUP, WS_EX_APPWINDOW, WS_EX_TOOLWINDOW, WS_EX_MDICHILD
Window’s border:
WS_BORDER, WS_DLGFRAME, WS_THICKFRAME, WS_EX_CLIENTEDGE, WS_EX_WINDOWEDGE
Elements of a window:
WS_HSCROLL, WS_VSCROLL, WS_CAPTION, WS_MINIMIZEBOX, WS_MAXIMIZEBOX, WS_SYSMENU
Other features:
WS_EX_ACCEPTFILES, WS_EX_CONTEXTHELP, WS_EX_LEFTSCROLLBAR
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 26
Other Window’s Features
The class
GetClassName(), GetClassInfo(), SetClassInfo()
GetClassWord(), SetClassWord()
The name
GetWindowText(), SetWindowText(), GetWindowTextLength()
The private data
GetWindowLong(), SetWindowLong() with GWL_USERDATA
The handle - HWND
FindWindow(), FindWindowEx()
IsWindow()
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 27
Scenario of Using a Window
1. The class is registered
RegisterClass(), RegisterClassEx()
2. The window is created
CreateWindow(), CreateWindowEx()
3. All messages are processed
WndProc()
4. The window is destroyed
DestroyWindow()
WM_CLOSE, WM_DESTROY
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 28
The Desktop
The desktop is a window created at system startup
It covers the screen
It can have a wallpaper
Functions:
GetDesktopWindow()
SystemParametersInfo()
SPI_GETDESKWALLPAPER, SPI_SETDESKWALLPAPER
SPI_SETDESKPATTERN
SPI_GETWORKAREA, SPI_SETWORKAREA
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 29
Time The simplest timer (very inaccurate)
SetTimer(), KillTimer()
WM_TIMER
Period of time
GetTickCount()
GetSystemTimeAdjustment()
High-resolution timer
QueryPerformanceCounter()
QueryPerformanceFrequency()
The system time
GetSystemTime(), SetSystemTime(), GetTimeFormat()
The local time
GetLocalTime(), SetLocalTime()
Windows Programming
http://www.mini.pw.edu.pl/~mossakowKrzysztof MossakowskiFaculty of Mathematics and Information Science, Warsaw Univ. of Tech.
Lecture 1 - 30
System Errors
The last-error code
the only way to check the reason of a failure of the last called Win32 API function
GetLastError(), FormatMessage(), SetLastError()
User notifications
MessageBox()
MB_ABORTRETRYIGNORE, MB_OK, MB_OKCANCEL, MB_RETRYCANCEL, MB_YESNO, MB_YESNOCANCEL
MB_ICONEXCLAMATION, MB_ICONWARNING, MB_ICONCONFIRMATION, MB_ICONASTERISK, MB_ICONQUESTION, MB_ICONSTOP, MB_ICONERROR
MessageBeep(), Beep()
FlashWindow(), FlashWindowEx()