int main(){}
instead of
int WINAPI WinMain(HINSTANCE ...
I have found an example, where I'm able to draw in console window
int main()
{
COLORREF color = RGB(255,0,0); // COLORREF to hold the color info
SetConsoleTitle("Pixel In Console?"); // Set text of the console so you can find the window
HWND hwnd = FindWindow(NULL, "Pixel In Console?"); // Get the HWND
HDC hdc = GetDC(hwnd); // Get t开发者_运维问答he DC from that HWND
for( int i = 0 ; i < 50 ; i++ )
{
SetPixel(hdc, 5+i, 12, color); // SetPixel(HDC hdc, int x, int y, COLORREF color)
}
ReleaseDC(hwnd, hdc); // Release the DC
DeleteDC(hdc); // Delete the DC
system("pause");
return(0);
}
but instead of console, I want to draw on my selected region, which will hold focus ( when user clicks on it, etc ).
It would be also great to be able to handle simple keyboard/mouse events for this program, but it isn't my primary target here, maybe some other third party libraries will help with it.
I hope I've explained clearly what I want to do, but English isn't my native language, so I'm very sorry for any missunderstandings.
I will be thankfull for any help.
As I'm using this site first time, I'm sorry for little spam or messages in wrong places, as I'm not sure where to post my next messages :-) So what i wanted to write, is:
" Otherwise, how does Allegro/SDL create window? They use assembler calls or shell ones? I'll be much happier, when I'll be able to create window from scratch, no matter how much work does it take :) "
You won't like this - in Windows, you have to create a window, then override WM_PAINT message, then draw what you have to draw when you are called from the system. That's old-school way of doing things, and it isn't so bad.
Some interesting and relevant links:
http://www.winprog.org/tutorial/bitmaps.html
http://www.codeproject.com/Articles/66250/BeginPaint-EndPaint-or-GetDC-ReleaseDC.aspx
If you are really into avoiding all that, try popcap. Learning curve involved there maybe steeper, so you probably really want to stick with GDI and HWND no matter how hard and confusing it might look in the beginning.
I suggest you to try OpenGL coupled with either GLFW or glut. With OpenGL you will be able to handle all the things that are related to graphics processing (2D/3D rendering), and the role of the GLFW library for instance is to add some functionalities like: window management, event management, timers, threads etc.
Personnal note, go for GLFW because glut isn't maintained anymore I think...
Otherwise, how does Allegro/SDL create window? They use assembler calls or shell ones? I'll be much happier, when I'll be able to create window from scratch, no matter how much work does it take :)
Problem with your code: I tried out your code and i found that the line seemed to appear in some instance of its execution, but sometimes, when I execute it, the red line isn't found! From what I understand it is because you kept these 3 statements:
// Set text of the console so you can find the window
SetConsoleTitle("Pixel In Console?");
HWND hwnd = FindWindow(NULL, "Pixel In Console?"); // Get the HWND
HDC hdc = GetDC(hwnd); // Get the DC from that HWND
But Windows only updates your window name after a little time, so if you make these statements execute one after the other, the HWND hwnd = FindWindow(NULL, "Pixel In Console?");
statement cant find such a window, because Windows takes time to update the title as done by : SetConsoleTitle("Pixel In Console?");
Solution:
You can either keep a Sleep(int);
(include windows.h
) or use:
HWND hwnd = FindWindowA("ConsoleWindowClass",NULL); // Get the HWND
instead of
SetConsoleTitle("Pixel In Console?"); // Set text of the console so you can find the window
HWND hwnd = FindWindow(NULL, "Pixel In Console?"); // Get the HWND
Here is an example program:
#include <windows.h>
#include<conio.h>`
#include<iostream.h>`
POINT p; //structure with coordinates to mouse location`
void main()
{
COLORREF color = RGB(255,0,0); // COLORREF to hold the color info`
HWND hwnd = FindWindowA("ConsoleWindowClass",NULL); // Get the HWND
HDC hdc = GetDC(hwnd); // Get the DC from that HWND
for( int i = 0 ; i < 400 ; i++ )
{
for(int j=0;j<50;j++)
SetPixel(hdc, i, j, color);
}
while(1)
{
GetCursorPos(&p);
ScreenToClient(hwnd,&p);
cout<<p.x<<' '<<p.y;
clrscr();
if(GetAsyncKeyState(1)) //checks if left mouse button is pressed
{
//fool around with these functions:
SetPixel(hdc,p.x,p.y,color);
//LineTo(hdc,p.x,p.y);
//Rectangle(hdc,200,200,p.x,p.y);
Sleep(10);
}
}
ReleaseDC(hwnd, hdc); // Release the DC
DeleteDC(hdc); // Delete the DC
system("pause");
}
This is my first answer. I hope I helped and it took a lot of searching to find these functions besides I think I learned more from your script than I showed you :)
Long story short: You should use a graphics framework / widgeting toolkit in order to create a window, and start from there.
Then, depending on what framework you use, you'll then create the window and modify the pixels according to its reference.
I recommend SDL (small, probably exactly what you need), Allegro (similar) or Qt (largeish).
精彩评论