1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <windows.h>
#define BUTTON1 3141
#define BUTTON1_TEXT "Draw lines"
// Make the class name into a global variable
char szClassName[] = "WindowsApp";
// This is the handle for our window - global
HWND hwnd;
// This is the handle for our button - global
HWND button1;
// This is the handle to the device context for hwnd, we will set this value with GetDC in WinMain
HDC hdc;
// Declare Windows procedure
// This function is called by the Windows function DispatchMessage()
bool DrawLines=false; // our variable for if we need to draw
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) // handle the messages
{
case WM_SIZE: // for resizing
case WM_ACTIVATE: // works for focus changes & minimises
if(DrawLines)
{
MoveToEx(hdc,100,100,0); // move the drawing position to 100, 100
LineTo(hdc,150,150); // draw a line from the drawing position to 150, 150
MoveToEx(hdc,200,200,0); // move the drawing position to 200, 200
LineTo(hdc,300,200); // draw a line from the drawing position to 300, 200
}
break;
case WM_COMMAND: // we've received a command
// is it our button?
if(LOWORD(wParam)==BUTTON1)
{
DrawLines=true; // set the DrawLines variable to true
SendMessage(hwnd,WM_SIZE,0,0); // send the message to SIZE just to redraw
}
break;
case WM_DESTROY:
PostQuitMessage (0); // send a WM_QUIT to the message queue
break;
default: // for messages that we don't deal with
return DefWindowProc (hWnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
MSG messages; // Here messages to the application are saved
WNDCLASSEX wc ; // Data structure for the windowclass
// The Window structure
wc.hInstance = hThisInstance;
wc.lpszClassName = szClassName;
wc.lpfnWndProc = WindowProcedure; // This function is called by windows
wc.style = CS_DBLCLKS; // Catch double-clicks
wc.cbSize = sizeof (WNDCLASSEX);
// Use default icon and mouse-pointer
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.lpszMenuName = NULL; // No menu
wc.cbClsExtra = 0; // No extra bytes after the window class
wc.cbWndExtra = 0; // structure or the window instance
// Use Windows's default colour as the background of the window
wc.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
// Register the window class, and if it fails quit the program
if(!RegisterClassEx (&wc)){return 0;}
// The class is registered, let's create the program
hwnd = CreateWindowEx (
0, // Extended possibilites for variation
szClassName, // Classname
"Windows App", // Title Text
WS_OVERLAPPEDWINDOW, // default window
CW_USEDEFAULT, // Windows decides the position
CW_USEDEFAULT, // where the window ends up on the screen
400, // The program's width
300, // and height in pixels
HWND_DESKTOP, // The window is a child-window to desktop
NULL, // No menu
hThisInstance, // Program Instance handler
NULL // No Window Creation data
);
// Make the window visible on the screen
ShowWindow (hwnd, nCmdShow);
hdc = GetDC(hwnd); // this is setting hdc to hwnd's hdc - this is used for drawing
// make our button
button1 = CreateWindowEx (
0, // Extended possibilites for variation
"BUTTON", // Classname
BUTTON1_TEXT, // Button Text
WS_CHILD|WS_VISIBLE, // Child to the window, visible
5, // X co-ordinate
5, // Y co-ordinate
80, // The button's width
18, // and height in pixels
hwnd, // The button is a child to hwnd - our window
(HMENU)BUTTON1, // This value is sent to our WindowProcedure function when button is clicked
NULL, // Program Instance handler
NULL // No Window Creation data
);
// Run the message loop. It will run until GetMessage() returns 0
while (GetMessage (&messages, NULL, 0, 0))
{
// Translate virtual-key messages into character messages
TranslateMessage(&messages);
// Send message to WindowProcedure
DispatchMessage(&messages);
}
// The program return-value is 0 - The value that PostQuitMessage() gave
return messages.wParam;
}
|