You are on page 1of 56

Windows basic concepts

Windows programming

CuuDuongThanCong.com https://fb.com/tailieudientucntt
In this talk
1. Windows history & characteristics
2. Window’s anatomy
3. Event-driven programming using messages

CuuDuongThanCong.com https://fb.com/tailieudientucntt
2
Windows history

CuuDuongThanCong.com https://fb.com/tailieudientucntt
3
Popularity

Wikipedia, https://en.wikipedia.org/wiki/Microsoft_Windows (06.2017)

CuuDuongThanCong.com https://fb.com/tailieudientucntt
4
Basic charateristics
 Event-driven: wait for input from system
 Multitask / multithread
 32 bit & 64 bit
 Graphical User Interface

CuuDuongThanCong.com https://fb.com/tailieudientucntt
5
Programming libraries
 Windows API (“Win32 API” and “Win64 API”)
 OWL - Object Windows Library
 MFC - Microsoft Foundation Classes
 ATL - Active Template Library
 WTL - Windows Template Library
 BCL - .NET Framework Base Class Library
 …

CuuDuongThanCong.com https://fb.com/tailieudientucntt
6
Covered in this course
 Windows Application Programming Interface (11
weeks)
 Win32 API (C++)
 C# - Universal app with WPF (3-4 weeks)

CuuDuongThanCong.com https://fb.com/tailieudientucntt
7
Microsoft .Net framework

CuuDuongThanCong.com https://fb.com/tailieudientucntt
8
Windows API
 Main components
 Base Services: file systems, devices, processes & threads,
Windows registry…
 Graphics Device Interface
 User Interface
 Windows Shell
 Network Services
 Multimedia related APIs: DirectX
 Programs interaction APIs: DDE, OLE, COM…

CuuDuongThanCong.com https://fb.com/tailieudientucntt
9
Windows SDK
 Software Development Kit

Documentation Header files, libraries

Samples Tools to compile, debug,…

 Windows (SDK) for Windows 10


 Already in Visual Studio

CuuDuongThanCong.com https://fb.com/tailieudientucntt
10
Version of VS in this course
 Visual Studio 2017 Community Edition
 Free

Other versions are okay, as long as you specify in


readme.txt or readme.docx

CuuDuongThanCong.com https://fb.com/tailieudientucntt
11
Types of application

Console UI

 Web-service
 Web-based
 And many more

Focus in this course: Graphical UI


CuuDuongThanCong.com https://fb.com/tailieudientucntt
12
Window’s anatomy

13
CuuDuongThanCong.com https://fb.com/tailieudientucntt
What is a window?

Quiz: How many windows are there?

CuuDuongThanCong.com https://fb.com/tailieudientucntt
14
Components of a Window

CuuDuongThanCong.com https://fb.com/tailieudientucntt
15
Common controls 1
Tab Control

Edit box

Listbox

Combobox

Check box
Static text

Button
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Common controls 2

CuuDuongThanCong.com https://fb.com/tailieudientucntt
17
Ribbon / fluent

CuuDuongThanCong.com https://fb.com/tailieudientucntt
18
How application started?
 OS create process and thread
 Load binary code into memory (DLL, if needed)
 Data is allocated & mapping into virtual memory
 Application start the thread

CuuDuongThanCong.com https://fb.com/tailieudientucntt
19
DOS vs Windows app

CuuDuongThanCong.com https://fb.com/tailieudientucntt
20
Console application

CuuDuongThanCong.com https://fb.com/tailieudientucntt
21
Simple Windows program

CuuDuongThanCong.com 10 Questions?
https://fb.com/tailieudientucntt
22
Create code from template
 File > New Project

CuuDuongThanCong.com https://fb.com/tailieudientucntt
23
Full program - main

WM_QUIT

CuuDuongThanCong.com https://fb.com/tailieudientucntt
24
To sum up
 Register window class
 Init an instance
 Message loop

CuuDuongThanCong.com https://fb.com/tailieudientucntt
25
Register class

CuuDuongThanCong.com https://fb.com/tailieudientucntt
26
Initialize main window

CuuDuongThanCong.com https://fb.com/tailieudientucntt
27
Some important terms
 Handle
 32 bits unsigned integer created by OS for an object
(window, file, memory, menu,…)
 ID (Identifier)
 Unsigned integer to identify between objects in a
program
 Instance
 Unsigned integer of current instance
 Callback function: function called by another function

CuuDuongThanCong.com https://fb.com/tailieudientucntt
28
Activity – Spy++
 .\Visual Studio installation
folder\Common7\Tools\spyxx_amd64.exe
 Identify a window handle, caption & classname

CuuDuongThanCong.com https://fb.com/tailieudientucntt
29
What can we do with handle?
 Get handle using Spy++
 Add button to desktop!
 Change label of OK button of Run dialog to Hello

 Force redraw
 InvalidateRect (hWnd, NULL, TRUE);
UpdateWindow (hWnd);

CuuDuongThanCong.com https://fb.com/tailieudientucntt
30
Little customization
 Change title of program to “Hello little world”

 Change icon of program

CuuDuongThanCong.com https://fb.com/tailieudientucntt
31
More customization
 Change color of background to better color
wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);

 Change menu to this

CuuDuongThanCong.com https://fb.com/tailieudientucntt
32
Deliverables
 Find the source code path
 Open folder in File explorer
 Debug & Release mode

 Remember to clean solution before


submission!
 Hidden .vs folder! 33
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Event-driven &
Messages

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Event-driven programming

CuuDuongThanCong.com https://fb.com/tailieudientucntt
35
Quiz
 List 10 messages
 Kick start: A key is pressed

CuuDuongThanCong.com https://fb.com/tailieudientucntt
36
Some messages
Message Sent when
WM_CHAR A key is pressed
WM_COMMAND User click on button, menu
WM_CREATE A window is created
WM_DESTROY A window is destroyed
WM_LBUTTONDOWN Left button is pressed (not released)
WM_LBUTTONUP Left button is released after being pressed
WM_MOUSEMOVE Mouse movement
WM_PAINT A windows needs to be repainted
WM_QUIT Application is going to close
WM_SIZE A window is about to change its size

CuuDuongThanCong.com https://fb.com/tailieudientucntt
37
Event processing model

CuuDuongThanCong.com https://fb.com/tailieudientucntt
38
Window messages
 Generated by system & applications

 Activity: Reading comprehension


 Reading-MessageQueue.docx

CuuDuongThanCong.com https://fb.com/tailieudientucntt
39
Message components

Window handle
Message identifier

CuuDuongThanCong.com https://fb.com/tailieudientucntt
40
Window procedure

41
CuuDuongThanCong.com
God mode: <windowsX.h> https://fb.com/tailieudientucntt
Flashback - Function pointer
 Pointer to another function to call

42
God mode: http://www.cprogramming.com/tutorial/function-pointers.html
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Two ways for message
1. Postmessage( )
• Send message into queue
2. Sendmessage( )
• Send message immediately, skip queue and acquire
instant response

CuuDuongThanCong.com https://fb.com/tailieudientucntt
43
Quiz
What should I change if I want to
1. Change icon of the program?
2. Change background color of the main window?
3. Change main window size to 300, 400?
How can we make it appear in the center?
How can we make our program appear fullscreen?
4. Save working progress on exit if dirty?
5. Get configuration of the program?

CuuDuongThanCong.com https://fb.com/tailieudientucntt
44
Question
 What if we have multiple windows, how many
WndProc does we have to write?

CuuDuongThanCong.com https://fb.com/tailieudientucntt
45
Resources
 Defined in .rc file
 Added to executable file when compiled
 Types of resources
 Accelerator table: hot-key
 Bitmap: bitmap
 Caret, Cursor
 Dialog box
 Font, Icon
 Menu
 String-table entry
 Version information

CuuDuongThanCong.com https://fb.com/tailieudientucntt
46
Accelerator table

CuuDuongThanCong.com https://fb.com/tailieudientucntt
47
Menu

CuuDuongThanCong.com https://fb.com/tailieudientucntt
48
Quiz – Hungarian notation
 HWND, HINSTANCE, BOOL, LRESULT
 WCHAR, TCHAR, CHAR
 LPCWSTR, LPTSTR
 WPARAM, LPARAM
 cbSize
 szWindowClass, szTitle

 L“” & _T(“”) difference?

CuuDuongThanCong.com https://fb.com/tailieudientucntt
49
Graphical user interface

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Types
 SDI - Single Document Interface
 MDI - Multiple Document Interface
 Dialog

CuuDuongThanCong.com https://fb.com/tailieudientucntt
51
Single Document Interface - SDI
 One working window
 Resizeable
 No child window
 Ex: Notepad, Paint,…

CuuDuongThanCong.com https://fb.com/tailieudientucntt
52
Multiple Document Interface - MDI

 One main window(Frame window) and many child


windows
 Resizeable
 Maximize/Minimize/Close child windows
 Ex: Word, Excel, VC++,…

CuuDuongThanCong.com https://fb.com/tailieudientucntt
53
Dialog
• Fixed size

CuuDuongThanCong.com https://fb.com/tailieudientucntt
54
Other OS out there

 Android
 iOS
 BlackBerryOS
 Tizen…

CuuDuongThanCong.com https://fb.com/tailieudientucntt
55
Refereces
 Windows API Index
 https://msdn.microsoft.com/en-
us/library/windows/desktop/ff818516(v=vs.85).aspx
 Windows data types
 https://msdn.microsoft.com/en-
us/library/windows/desktop/aa383751(v=vs.85).aspx

CuuDuongThanCong.com https://fb.com/tailieudientucntt
56

You might also like