Drawing Graphics with C and Windows API is Easy

Ғылым және технология

In this video I will demonstrate basic usage of Windows GDI to draw graphics on the screen.
Code: github.com/nir9/poc/blob/mast...

Пікірлер: 119

  • @georgeadrianstefan1676
    @georgeadrianstefan167625 күн бұрын

    No 30 seconds intro, no ads, no sponsors, no BS talking, straight to the point

  • @whannabi

    @whannabi

    24 күн бұрын

    Miss those days

  • @KimTiger777
    @KimTiger777Ай бұрын

    Nice, I didn't realize that you could draw directly onto the screen without a window. If I am not mistaken you could get the DC for the desktop it self. This gave some ideas for desktop widgets that displays information for instance a server monitor using ping to check if each server is online. Thanks as always.

  • @KimTiger777

    @KimTiger777

    Ай бұрын

    I managed to cobble together a window without border/caption/taskbar that is semi-transparent with the desktop that stays in the desktop area. Looks like it is complete part of the desktop background. This was a fun exercise to do.

  • @dimaryk11

    @dimaryk11

    Ай бұрын

    @@KimTiger777 could you provide code or vid?

  • @KimTiger777

    @KimTiger777

    Ай бұрын

    @@dimaryk11 Weird. I tried twice to add a link here but I guess YT doesn't like URLs. Not sure how to get around this limitation. Any ideas?

  • @dimaryk11

    @dimaryk11

    Ай бұрын

    @@KimTiger777 try without special chars, replace them with spaces

  • @KimTiger777

    @KimTiger777

    Ай бұрын

    @@dimaryk11github kimlar Win32_DrawingOnEntireDesktop

  • @user-bd1dh7hh1j
    @user-bd1dh7hh1jАй бұрын

    I cannot stress enough how much I love straight-to-the-point-no-bs videos such as yours.

  • @yorimirus
    @yorimirusАй бұрын

    Wow. Didn't know you can draw stuff without a window on windows. Not sure what it would be useful for but that's pretty cool.

  • @multiamphetaminsaft
    @multiamphetaminsaftАй бұрын

    Personally this is the most interesting / useful YT channel for me at the time. Please don‘t stop uploading.

  • @ismbks
    @ismbksАй бұрын

    The only man that programs on Windows like it's 1990 UNIX.

  • @jackkendall6420
    @jackkendall6420Ай бұрын

    I always get jumpscared by the reminder to subscribe at the end of these videos

  • @ABCABC-sw8mh
    @ABCABC-sw8mhАй бұрын

    how is this possible. the things you did in this and the last video I also did a few weeks ago. btw intersting video like everytime

  • @kinershah464
    @kinershah464Ай бұрын

    Wow, this was so simple and nice explanation. I knew GDI but thought it was too low level, but this is so cool. You also explained how to use cmd tool cl.exe and how to refer to microsoft docs. Please make more videos on GDI. Maybe two series, one that covers creating a UI using GDI, other that shows how to make a simple game using GDI.

  • @karolduracz4008
    @karolduracz400829 күн бұрын

    Your style to show how to use functions and API mechanisms step by step is great. Like yours videos. Make more videos like this.

  • @twentylush
    @twentylush23 күн бұрын

    I saw someone comment on another video that they were looking into a topic but couldn’t find straight-to-the-point specifics, only old stuff. Lo and behold, you uploaded this video on windowless graphics drawing through the windows api, something I was looking into and couldn’t find a good video on…

  • @faucar93
    @faucar9322 күн бұрын

    I was looking for this since I was 10 !!!!!! Thank you so much

  • @d_sanu
    @d_sanu20 күн бұрын

    Great video Sir. You are always to the point 👍

  • @floor_3d
    @floor_3d4 күн бұрын

    LOVE these no BS videos. 2 questions: 1. before the animation, you had the draw code in a loop. what purpose does that fulfill? does the rectangle disappear after a bit? 2. would you be able to make a video on drawing images (i.e. loaded from file)? Windows API is great. These videos are amazing!

  • @GentleMannOfHats
    @GentleMannOfHatsАй бұрын

    You make such intresting videos!

  • @user-kx5nr9hz2t
    @user-kx5nr9hz2tАй бұрын

    damn bro this is cool i will use this thank you so much

  • @sebaitor
    @sebaitor27 күн бұрын

    This introduces many questions, will it always draw on top? how does it know if its "focused"? Can I interact with GUI made from this directly onto the screen?

  • @vishwa_randunu
    @vishwa_randunuАй бұрын

    Sir, can you make video "How to direcly access graphic hardware in linux"?

  • @ArneChristianRosenfeldt

    @ArneChristianRosenfeldt

    Ай бұрын

    Vulkan

  • @gwentarinokripperinolkjdsf683

    @gwentarinokripperinolkjdsf683

    Ай бұрын

    ​@@ArneChristianRosenfeldtdirectly (of course tbis isn't as direct as it seems)

  • @ArneChristianRosenfeldt

    @ArneChristianRosenfeldt

    Ай бұрын

    @@gwentarinokripperinolkjdsf683 I read so many times that Vulkan is more direct than most devs can deal with. So much boilerplate .. or it looks like boilerplate until you really become an GPU expert and know what to change. But some comments seem to indicate that GPU hardware changed in unforeseen ways and Vulkan already becomes obsolete? I thought about dabbling in the FOSS ubuntu drivers, but there seems to be no favorite GPU with a big community. I am moreso thanful to the devs. You know, Kernel stuff is taught at the university and kinda pure and students like to dream up the ideal OS. But who has fun writing universal drivers for dated printers, soundcards, and GPUs? I should donate..

  • @liquidsnake6879

    @liquidsnake6879

    Ай бұрын

    I don't think you can access it directly unless you're in Kernel Space and you don't need to be in Kernel space unless you're making hardware device drivers, the lowest possible level above the Kernel is the Wayland and X APIs afaik, above those you have stuff like Cairo and Skia. Because Linux is not a corporate owned piece of software it's not as simple as Microsoft, there isn't just one option, there's several.

  • @marindedic441
    @marindedic441Ай бұрын

    Do a linux version! Love your content, very interesting. Keep it up!

  • @justinnamilee
    @justinnamileeАй бұрын

    Neat! Also, I find it interesting that the brush is tied to the screen instead of say to the rectangle, seems like it may be a pain to use multiple colours.

  • @ArneChristianRosenfeldt

    @ArneChristianRosenfeldt

    Ай бұрын

    I looked at windows to use more memory than there was in DOS. But GDI could not do any of the game graphics which were standard in DOS. Maybe wireframe? Windows games always looked very simple like BASIC examples a decade prior.

  • @declanmoore

    @declanmoore

    29 күн бұрын

    You could perhaps create an offscreen bitmap with CreateDIBSection, then render in software to a pixel buffer and BitBlt it to your window's DC. But then you don't get any benefits of hardware acceleration in the rendering and would probably be better off using Direct3D or OpenGL

  • @ArneChristianRosenfeldt

    @ArneChristianRosenfeldt

    28 күн бұрын

    @@declanmoore so the graphics driver software knows the device dependent format for hicolor and the windows file format for hicolor and translates this in a tight loop without a call to PutPixel? DOS games used this. They rendered into a 16bpp buffer in main RAM and then blitted to 8bpp video RAM.

  • @declanmoore

    @declanmoore

    28 күн бұрын

    @@ArneChristianRosenfeldt don't quote me on it but I think it's BitBlt that does the conversion? I thought I'd remembered reading that somewhere some time ago. But yeah drawing pixels to the buffer and converting them all at once is almost surely faster than all the overhead of GDI SetPixel, which as I recall comes down to an NtGdiSetPixel system call (at least on modern Windows).

  • @ArneChristianRosenfeldt

    @ArneChristianRosenfeldt

    27 күн бұрын

    @@declanmoore I just meant that graphic card manufacturers probably were proud of their internal format. And conversion from and to standard then was an afterthought implemented in software.

  • @Humble_Electronic_Musician
    @Humble_Electronic_MusicianАй бұрын

    Cool!

  • @michaeletzkorn
    @michaeletzkornАй бұрын

    Thanks for showcasing developer cmd prompt and cl. Visual studio’s gui is a nightmare and I’m glad to learn a gui-less compile method

  • @noxagonal

    @noxagonal

    Ай бұрын

    Might be handy for toy projects, otherwise just use CMake. Visual Studio GUI got updated somewhat recently, it's a little more streamlined, just FYI.

  • @nirlichtman

    @nirlichtman

    Ай бұрын

    In a future video I will also cover another builtin tool called nmake which helps when working with larger projects by providing support for makefiles

  • @jopa19991

    @jopa19991

    27 күн бұрын

    You might get more disappointed when you get to their CLI. After gcc/clang, parameter names are counter-intuitive

  • @lorensims4846
    @lorensims4846Ай бұрын

    Cool! Now how can you do this under DOS?

  • @jamesross3939
    @jamesross3939Ай бұрын

    Nice! Liked and Subscribed!

  • @manojht8036
    @manojht8036Ай бұрын

    Wow 👌

  • @jaywalks9918
    @jaywalks991823 күн бұрын

    Back in the day I wrote an n dimensional Grid control in C using GDI.

  • @ryonagana
    @ryonaganaАй бұрын

    nice. like old win3.11 and win95 games. love it.. my favorite is skifree i think its pure GDI right?

  • @caspianmerlin6434

    @caspianmerlin6434

    23 күн бұрын

    Yep, and the developer of SkiFree found his source code recently and recompiled it for x64. It’s on his website.

  • @VaibhavSharma-zj4gk
    @VaibhavSharma-zj4gk25 күн бұрын

    thank you

  • @zilzila
    @zilzilaАй бұрын

    Can you give an example of a simple transparent overlay that would be displayed on top of a full-screen application (game)?

  • @declanmoore

    @declanmoore

    29 күн бұрын

    You'd probably not want to draw directly to the screen since anything you draw will be constantly overwritten by other things drawing to the screen, and it would possibly look like a flickering mess. Most likely you'd want to create a window with no caption and using WS_EX_LAYERED, then set layered attributes to translucent and at whatever translucency your need. For per-pixel translucency though it gets a bit more complicated.

  • @maciek367
    @maciek367Ай бұрын

    ducking awesome

  • @undeadpresident
    @undeadpresident28 күн бұрын

    So this just uses the CPU to directly access the screen buffer, no GPU then?

  • @bebdaumon3948
    @bebdaumon3948Ай бұрын

    Interesting... Is this used to also make custom window for an application? Like can you draw your own functional app window instead of using the OS skin themes?

  • @noxagonal

    @noxagonal

    Ай бұрын

    Not practically. It's a relic from Windows 1.0, before GPUs were even a thing, it does not play nice with modern desktops. Instead, you can create an undecorated window. You can add Windows UI elements anywhere directly on that empty window. Alternatively you can also get the framebuffer for that window so that the GPU can draw into it. You can use many different libraries to draw whatever you want on it, or rawdog it with DirectX, OpenGL or Vulkan. I honestly didn't believe any part of GDI even worked anymore.

  • @bebdaumon3948

    @bebdaumon3948

    Ай бұрын

    @@noxagonal Thanks... I am trying to find a book that can teach me all these. I know c and C++ and just know the basics of how to code. I just don't know how the windows systems fully works and the only stuff I can find on the internet are just basic stuff like creating an empty window add text to the window and buttons. However, when I install drivers from ATI or Nvidia their installers uses windows that are theme by them. Meaning it won't look like a basic windows window nor would use a windows desktop theme. It would use it's own theme where it has it's own exit button minimize and maximize buttons and have it's own style text on the title of the window etc. I wanted to learn those things. Still learning about the windows management system. I just need to play around with but would like to know resources that will lead me to learn how to customize every aspects of the window or windows of my app.

  • @noxagonal

    @noxagonal

    Ай бұрын

    @@bebdaumon3948 np. There are many ways to do this and many libraries that do it for you as well as window editors that allow you to design the UI visually. For completely custom look, see "borderless" window, that displays only the contents of the window, and you can emulate the borders by drawing your own. Ask ChatGPT to get started fast. - Easiest is to simply change theme of the UI elements and borders, see resource files (.rc). Windows will take care of window buttons and other events for you. - For more control, use borderless window and use Windows UI elements to make your own borders within the window, you'll have to emulate all the close top-bar buttons and edge drag however. - For complete control, use borderless window and draw into its framebuffer. You'll need to create all of your own UI elements and logic however, or use a library like imGui. Eg. Blender uses OpenGL to draw all internal UI elements, and it could easily just hide the borders and draw its own. You'll need to see window event dispatching system to understand how to control the window. In the last 2 options, you'll have to tell the window what to do yourself. Eg. to close the window. In the first option, it's mostly done for you, however, you can still intercept the window close event and do something else instead, like display a warning about unsaved changes.

  • @kongolandwalker
    @kongolandwalker23 күн бұрын

    I was doing a project and found out that ColorPicker and RGB both use Colorref, but with reverse color order. I didn't bother to find out which of them is wrong, i just had to treat RGB like it takes Blue,Green,Red, and then i countered the inconsistency

  • @user-ft6zh8ny9i
    @user-ft6zh8ny9i21 күн бұрын

    I've wrote a bunch of functions in masm64 assembly and can use it. buy i can't create a .lib file and connect it to executable. My functions are inside namespace in C++. and it says that can't find a reference to function. Can you, please, help me with that? thank you:)))

  • @zakariajouimaa
    @zakariajouimaaАй бұрын

    i hope if you can start a cpp course from start it will be nice and thanks for the vid

  • @bobbeverage1776
    @bobbeverage1776Ай бұрын

    How do I remove a rectangle I drew? Seems like it should be a simple concept but I am not having much luck figuring that out.

  • @sealsharp

    @sealsharp

    Ай бұрын

    You don't. There is no rectangle "object" that could be removed. Drawing in GDI is a slightly more convenient way to changing RPG values in an array. But you can manually manage the states to go back to by using SaveDC and RestoreDC if you want to build your own windows paint.

  • @zilzila
    @zilzilaАй бұрын

    Will it work in a full-screen application (game)? Is it possible to display such an overlay on top of the game in this way? I want to see the chat during the game stream on my monitor (there is no other one).

  • @zilzila

    @zilzila

    Ай бұрын

    I just checked - no :(

  • @ggiceluna

    @ggiceluna

    Ай бұрын

    @@zilzila You probably need to find a handle to the games window and use that

  • @CaptTerrific

    @CaptTerrific

    Ай бұрын

    I highly recommend you don't even bother using GDI for this - it's REALLY old technology. IIRC it can't even handle an alpha channel or antialiasing, so your text and box will fully cover everything, and look jagged as balls. It's a pure CPU framework that died as soon as we needed to render our environments in 1080p. But it IS fun for learning, and will get you results quickly :)

  • @sealsharp

    @sealsharp

    Ай бұрын

    Probably no. Overlays have been made before, so i recommend you look into that and see what comes up.

  • @jopa19991

    @jopa19991

    27 күн бұрын

    @@CaptTerrific while I agree that it's really old technology, however, its' backend is hardware accelerated, it just misses features you mentioned. It's really one of those easy ways to start learning 2D graphics. If you try Direct2D as your first graphics SDK, you will want to hang immediately.

  • @milk-it
    @milk-it20 күн бұрын

    If you can code this in HTML and CSS, you can code this in C.

  • @jopa19991
    @jopa1999127 күн бұрын

    Try drawing on Direct2D device context with swap chain using C bindinghs for COM. This gonna be hell of a show😄

  • @DerPille
    @DerPille10 күн бұрын

    Please think about DeleteObject(), BeginPaint() and EndPaint()

  • @dovonun
    @dovonunАй бұрын

    🤯

  • @nno64209
    @nno64209Ай бұрын

    Can you do direct3D next?

  • @TheBypasser
    @TheBypasser25 күн бұрын

    Neat, though for the Windows graphics the fun happens when using D3D or D2D. It is not that tough, but by using swaps on demand and instanced drawcalls I managed to get a map renderer that felt much smoother than Paint lol (even with a test of rendering 1000+ clickable icons) :)

  • @Play_Now
    @Play_Now28 күн бұрын

    I followed the example at 3:15, but I noticed when I moved a window or a file under my rectangle, it started flickering. Why does this happen and is there any way to stop it?

  • @none4964

    @none4964

    5 күн бұрын

    If im not wrong, you have to create a windows and draw it on the window to prevent flickering.

  • @Saw-qv3bl
    @Saw-qv3bl26 күн бұрын

    Is there any GDI function that draws images to a screen?

  • @nirlichtman

    @nirlichtman

    25 күн бұрын

    You can draw bitmaps with GDI

  • @__hannibaalbarca__
    @__hannibaalbarca__Ай бұрын

    Just Question : What is deference between using GDI, OpenGL, is GDI use GPU resource, i don’t investigate this topic, I’m busy with networking(ASIO Lib).

  • @sealsharp

    @sealsharp

    Ай бұрын

    In OpenGL/Direct3D/Vulkan etc. you create a buffer in VRAM on the GPU and then you send commands to the GPU and the GPU draws that. In GDI you create a buffer in system RAM, which is an array of RGB values, and when youre done changing color values in that array it gets send to the GPU to show that.

  • @ArneChristianRosenfeldt

    @ArneChristianRosenfeldt

    Ай бұрын

    @@sealsharphow did “Windows accelerator” graphic cards work?

  • @sealsharp

    @sealsharp

    Ай бұрын

    ​@@ArneChristianRosenfeldt What i wrote is the simplest way to describe the differences. The actual way it works and worked is far more complicated and it changed a few times in the last 30 years. In modern Windows, GDi, GDi+, Direct2D and Direct3D are connected and GDI/Direct2D are what microsoft calls "partly hardware accelerated". Describing it would be me copying pages of Microsoft documentation here, so i will not do that. For "Windows accelerator” graphic cards, i assume you mean ones like that S3-Trio that everyone had before gaming GPUs took over? A nostagic part of me would love to know more about how these worked as well, but sadly the answer is i can't tell you. Hab einen schönen Sonntag 🙂

  • @ArneChristianRosenfeldt

    @ArneChristianRosenfeldt

    Ай бұрын

    @@sealsharp S3-Trio rings a bell / da klingelt was. My first card was NVidia Riva128 and it already had Direct3d. The Amiga could use copper + blitter to render glyphs. I think that the CPU still needed to calculate kerning, but did not need to wait for the rendering of large, colorful fonts. I guess that S3 basically copied this. CPU would write the characters+kerning over ISA / VesaLocal into a copper list in VRAM ("chipmem").

  • @sealsharp

    @sealsharp

    Ай бұрын

    @@ArneChristianRosenfeldt Coprocessors are a fun topic.

  • @stephenkolostyak4087
    @stephenkolostyak408722 күн бұрын

    "Now you can see the rectangle is growing." But does it blend?

  • @darkraven4207
    @darkraven4207Ай бұрын

    Can you please help me: 2 months ago you made a video of linux graphic distro. I'm stuck at a point in vim where you did NX11 = Y. I have no idea how can I use Vim and make It Y which button have you used to edit ? Please tell me

  • @jyothishkumar3098

    @jyothishkumar3098

    Ай бұрын

    Just use Notepad if you don't know Vim.

  • @darkraven4207

    @darkraven4207

    29 күн бұрын

    @@jyothishkumar3098 brother do you have Iso image of this project?

  • @jyothishkumar3098

    @jyothishkumar3098

    29 күн бұрын

    @@darkraven4207 Why ISO? Wasn't that a tutorial about making a distro from scratch?

  • @darkraven4207

    @darkraven4207

    29 күн бұрын

    @@jyothishkumar3098 first of all thank you so much for replying me. Is this working for you? I was following the same steps i even learn vim and applied that to Please tell me few things what is the base system on which you are doing like i am doing on my Kali Linux in vmware I stuck at a point(video minutes 10:30) where he said he got an error mine is a bit different error.

  • @darkraven4207

    @darkraven4207

    28 күн бұрын

    @@jyothishkumar3098 I have an important question. If I build linux graphical and what If i turn my pc off how can i access it again? Please help me

  • @martinbean
    @martinbean18 күн бұрын

    What does `[in]` designate in the parameters documentation?

  • @nirlichtman

    @nirlichtman

    18 күн бұрын

    That is a sign that means the parameter is an input to the function, there is also [out] which means the parameter gets an output from the function (GetFileSizeEx for example has that kind)

  • @martinbean

    @martinbean

    18 күн бұрын

    @@nirlichtman Thanks!

  • @shlokbhakta2893
    @shlokbhakta2893Ай бұрын

    Checkmate Wayland 😂

  • @glewfw7989
    @glewfw7989Ай бұрын

    do one with winrt

  • @mtk3078
    @mtk3078Ай бұрын

    there is direct2d too (for 2d) (its actually based on direct3d 10)

  • @davidfrischknecht8261

    @davidfrischknecht8261

    Ай бұрын

    It's also not as simple as GDI. There's also GDI+ which is also not as simple as GDI.

  • Ай бұрын

    And wgl

  • @nirlichtman

    @nirlichtman

    Ай бұрын

    Yes, DirectX is useful for more complex graphics, for instance for gaming, I will cover this in a future video

  • @friedrichmyers
    @friedrichmyersАй бұрын

    Ok, now I challege you to make a project with DirectX

  • @Kayraman256
    @Kayraman256Ай бұрын

    cool🥙

  • @trwwrt5687
    @trwwrt5687Ай бұрын

    Sending email with c please 😢

  • @wrestelman1
    @wrestelman1Ай бұрын

    Detected😢

  • @umathefurry
    @umathefurryАй бұрын

    ur funny

  • @mikeyjohnson5888
    @mikeyjohnson588823 күн бұрын

    I can't speak for everywhere, but when I was at a notable tech company, every single inhouse app they made used GDI and it always seemed buggy as shit. Shit programmers?

  • @rasputindasilva858
    @rasputindasilva858Ай бұрын

    Nobody uses GDI anymore that kind of programming is deprecated GDI.

  • @nirlichtman

    @nirlichtman

    Ай бұрын

    GDI is still widely used for Win32 applications and is considered the native way to draw basic graphics on Windows (there are other native Windows options for more complicated graphics like DirectX, but for most basic cases, GDI is sufficient). The windows graphics are eventually handled in the kernel so you can see how widely GDI is used on Windows by connecting with a kernel debugger to a Windows computer and putting a breakpoint on NtGdi* functions from the win32k driver

  • @arandomguythatdoesntpost

    @arandomguythatdoesntpost

    Ай бұрын

    ​@@nirlichtmanopenGL with transparent window vid?

  • @sealsharp

    @sealsharp

    Ай бұрын

    @@nirlichtman Now that Winforms has been ported to Net5+ and will be around for longer, this video will prove helpful to those who like to tinker with a bit of custom rendering on controls. From a C# dotnet perspective the drawing API seems a little strange and to understand how C-style APIs work, learning the basics like this is nice.