Drawing Graphics with C and Windows API is Easy

Поділитися
Вставка
  • Опубліковано 3 тра 2024
  • In this video I will demonstrate basic usage of Windows GDI to draw graphics on the screen.
    Code: github.com/nir9/poc/blob/mast...
  • Наука та технологія

КОМЕНТАРІ • 121

  • @georgeadrianstefan1676
    @georgeadrianstefan1676 26 днів тому +69

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

    • @whannabi
      @whannabi 25 днів тому

      Miss those days

  • @KimTiger777
    @KimTiger777 Місяць тому +158

    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 Місяць тому +15

      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 Місяць тому +1

      @@KimTiger777 could you provide code or vid?

    • @KimTiger777
      @KimTiger777 Місяць тому +2

      @@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 Місяць тому +28

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

  • @ismbks
    @ismbks Місяць тому +15

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

  • @multiamphetaminsaft
    @multiamphetaminsaft Місяць тому +51

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

  • @yorimirus
    @yorimirus Місяць тому +15

    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.

  • @jackkendall6420
    @jackkendall6420 Місяць тому +18

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

  • @ABCABC-sw8mh
    @ABCABC-sw8mh Місяць тому +36

    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 Місяць тому +4

    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.

  • @floor_3d
    @floor_3d 5 днів тому

    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!

  • @twentylush
    @twentylush 24 дні тому

    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…

  • @karolduracz4008
    @karolduracz4008 Місяць тому

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

  • @faucar93
    @faucar93 23 дні тому

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

  • @d_sanu
    @d_sanu 21 день тому

    Great video Sir. You are always to the point 👍

  • @GentleMannOfHats
    @GentleMannOfHats Місяць тому +1

    You make such intresting videos!

  • @user-kx5nr9hz2t
    @user-kx5nr9hz2t Місяць тому +4

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

  • @vishwa_randunu
    @vishwa_randunu Місяць тому +26

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

    • @ArneChristianRosenfeldt
      @ArneChristianRosenfeldt Місяць тому +5

      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.

  • @sebaitor
    @sebaitor 29 днів тому +2

    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?

  • @justinnamilee
    @justinnamilee Місяць тому +5

    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 Місяць тому

      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 29 днів тому

      @@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 29 днів тому

      @@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 28 днів тому

      @@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.

  • @michaeletzkorn
    @michaeletzkorn Місяць тому +10

    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  Місяць тому +5

      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 28 днів тому

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

  • @jamesross3939
    @jamesross3939 Місяць тому

    Nice! Liked and Subscribed!

  • @marindedic441
    @marindedic441 Місяць тому +2

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

  • @Humble_Electronic_Musician
    @Humble_Electronic_Musician Місяць тому +2

    Cool!

  • @jaywalks9918
    @jaywalks9918 24 дні тому

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

  • @zilzila
    @zilzila Місяць тому +4

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

    • @declanmoore
      @declanmoore Місяць тому +1

      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.

  • @ryonagana
    @ryonagana Місяць тому +2

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

    • @caspianmerlin6434
      @caspianmerlin6434 25 днів тому

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

  • @VaibhavSharma-zj4gk
    @VaibhavSharma-zj4gk 26 днів тому

    thank you

  • @lorensims4846
    @lorensims4846 Місяць тому

    Cool! Now how can you do this under DOS?

  • @manojht8036
    @manojht8036 Місяць тому

    Wow 👌

  • @zilzila
    @zilzila Місяць тому +3

    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 Місяць тому +4

      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 28 днів тому

      @@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.

  • @kongolandwalker
    @kongolandwalker 24 дні тому

    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

  • @milk-it
    @milk-it 21 день тому +1

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

  • @__hannibaalbarca__
    @__hannibaalbarca__ Місяць тому +3

    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 Місяць тому +1

      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 Місяць тому +1

      ​@@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 Місяць тому +1

      @@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 Місяць тому +1

      @@ArneChristianRosenfeldt Coprocessors are a fun topic.

  • @undeadpresident
    @undeadpresident 29 днів тому

    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 Місяць тому +1

      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.

  • @maciek367
    @maciek367 Місяць тому

    ducking awesome

  • @TheBypasser
    @TheBypasser 27 днів тому

    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) :)

  • @dovonun
    @dovonun Місяць тому +3

    🤯

  • @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.

  • @jopa19991
    @jopa19991 28 днів тому

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

  • @user-ft6zh8ny9i
    @user-ft6zh8ny9i 22 дні тому

    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:)))

  • @Play_Now
    @Play_Now 29 днів тому

    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 6 днів тому

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

  • @Byynx
    @Byynx 19 годин тому

    Could i use this api to directly send a bitmap with the pixel colors data to paint the window or do i have to use syscalls?

    • @nirlichtman
      @nirlichtman  18 годин тому

      You can use GDI to draw bitmaps on screen, check out CreateBitmap. Also, if you want to create a window, check out CreateWindow

  • @stephenkolostyak4087
    @stephenkolostyak4087 23 дні тому

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

  • @DerPille
    @DerPille 11 днів тому

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

  • @nno64209
    @nno64209 Місяць тому +1

    Can you do direct3D next?

  • @shlokbhakta2893
    @shlokbhakta2893 Місяць тому +1

    Checkmate Wayland 😂

  • @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 Місяць тому +1

      Just use Notepad if you don't know Vim.

    • @darkraven4207
      @darkraven4207 Місяць тому

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

    • @jyothishkumar3098
      @jyothishkumar3098 Місяць тому

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

    • @darkraven4207
      @darkraven4207 Місяць тому

      @@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 29 днів тому

      @@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

  • @Saw-qv3bl
    @Saw-qv3bl 27 днів тому

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

    • @nirlichtman
      @nirlichtman  26 днів тому

      You can draw bitmaps with GDI

  • @martinbean
    @martinbean 19 днів тому

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

    • @nirlichtman
      @nirlichtman  19 днів тому +1

      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 19 днів тому

      @@nirlichtman Thanks!

  • @Kayraman256
    @Kayraman256 Місяць тому +1

    cool🥙

  • @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  Місяць тому +3

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

  • @glewfw7989
    @glewfw7989 Місяць тому

    do one with winrt

  • @friedrichmyers
    @friedrichmyers Місяць тому

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

  • @trwwrt5687
    @trwwrt5687 Місяць тому

    Sending email with c please 😢

  • @wrestelman1
    @wrestelman1 Місяць тому

    Detected😢

  • @umathefurry
    @umathefurry Місяць тому

    ur funny

  • @mikeyjohnson5888
    @mikeyjohnson5888 24 дні тому

    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 Місяць тому +3

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

    • @nirlichtman
      @nirlichtman  Місяць тому +14

      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 Місяць тому +1

      @@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.