I need some help. Can you please tell me how to draw in the window of a program after the user clicks a button in the window. I want to have a button that draws a few lines in the program's window when clicked.
#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()
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) // handle the messages
{
case WM_CREATE: // when the window is created
break;
case WM_COMMAND: // we've received a command
if(LOWORD(wParam)==BUTTON1) // is it our button?
{
// draw some lines
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_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;
}
I'm going to sound like a jerk for saying this but the thread title "Windows Problem" is stating the obvious when you're posting it in the windows board. Perhaps you could choose a more meaningful subject line next time?
@Malachi
I really don't think so, because that I can't draw is a problem. And it is associated with windows, so I think my title is ok as it says I got a problem with windows.
@chris
I already read those MSDN articles, but they didn't seem to help me.
I will try your code, and I hope it works
To george135 & to Bazzy; although WM_PAINT will keep it always there, I am pretty sure (& it does for me anyway) it takes almost all the CPU it can & causes (for me) at least 95% CPU usage. In my opinion, it is better just find the cases where it would need to be redrawn (such as sizing the window or the window getting focus/loosing focus) & then put the code into there.
So maybe create a global variable if you need to redraw, then have your cases where it needs to redraw & your button click to activate this. I have used this code below & it works well for me, without the CPU usage of calling it on WM_PAINT:
#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;
}
(also as a note, I forgot to delete in my code, in WindowProcedure, the WM_CREATE case statement - that's not necessary in my code a few posts ago).