{"id":42213,"date":"2003-10-09T12:14:00","date_gmt":"2003-10-09T12:14:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2003\/10\/09\/other-uses-for-bitmap-brushes\/"},"modified":"2003-10-09T12:14:00","modified_gmt":"2003-10-09T12:14:00","slug":"other-uses-for-bitmap-brushes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20031009-00\/?p=42213","title":{"rendered":"Other uses for bitmap brushes"},"content":{"rendered":"<p>\n        Bitmap brushes used to be these little 8&#215;8 monochrome patterns that you could use\n        for hatching and maybe little houndstooth patterns if you were really crazy. But you\n        can do better.\n    <\/p>\n<p>\n        CreatePatternBrush lets you pass in any old bitmap &#8211; even a huge one, and it will\n        create a brush from it. The bitmap will automatically be tiled, so this is a quick\n        way to get bitmap tiling. Let GDI do all the math for you!\n    <\/p>\n<p>\n        This is particularly handy when you&#8217;re stuck with a mechanism where you are forced\n        to pass an <code>HBRUSH<\/code> but you really want to pass an <code>HBITMAP<\/code>.\n        Convert the bitmap to a brush and return that brush instead.\n    <\/p>\n<p>\n        For example, let&#8217;s take our scratch program and give it a custom tiled background\n        by using a pattern brush.\n    <\/p>\n<pre>HBRUSH CreatePatternBrushFromFile(LPCTSTR pszFile)\n{\n    HBRUSH hbr = NULL;\n    HBITMAP hbm = (HBITMAP)LoadImage(g_hinst, pszFile,\n                   IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);\n    if (hbm) {\n        hbr = CreatePatternBrush(hbm);\n        DeleteObject(hbm);\n    }\n    return hbr;\n}\nBOOL\nInitApp(LPSTR lpCmdLine)\n{\n    BOOL fSuccess = FALSE;\n    HBRUSH hbr = CreatePatternBrushFromFile(lpCmdLine);\n    if (hbr) {\n        WNDCLASS wc;\n        wc.style = 0;\n        wc.lpfnWndProc = WndProc;\n        wc.cbClsExtra = 0;\n        wc.cbWndExtra = 0;\n        wc.hInstance = g_hinst;\n        wc.hIcon = NULL;\n        wc.hCursor = LoadCursor(NULL, IDC_ARROW);\n        wc.hbrBackground = hbr;\n        wc.lpszMenuName = NULL;\n        wc.lpszClassName = CLASSNAME;\n        fSuccess = RegisterClass(&amp;wc);\n        \/\/ Do not delete the brush - the class owns it now\n    }\n    return fSuccess;\n}\n<\/pre>\n<p>\n        With a corresponding adjustment to <code>WinMain<\/code>:\n    <\/p>\n<pre>    if (!InitApp(lpCmdLine)) return 0;\n<\/pre>\n<p>\n        Pass the path to a *.bmp file on the command line, and bingo, the window will tile\n        its background with that bitmap. Notice that we did not have to change anything other\n        than the class registration. No muss, no fuss, no bother.\n    <\/p>\n<p>\n        Here&#8217;s another way you can use bitmap brushes to save yourself a lot of work. Start\n        with a new scratch program and change it as follows:\n    <\/p>\n<pre>HBRUSH g_hbr; \/\/ the pattern brush we created\nHBRUSH CreatePatternBrushFromFile(LPCTSTR pszFile)\n{ ... same as above ... }\nvoid\nPaintContent(HWND hwnd, PAINTSTRUCT *pps)\n{\n    BeginPath(pps-&gt;hdc);\n    Ellipse(pps-&gt;hdc, 0, 0, 200, 100);\n    EndPath(pps-&gt;hdc);\n    HBRUSH hbrOld = SelectBrush(pps-&gt;hdc, g_hbr);\n    FillPath(pps-&gt;hdc);\n    SelectBrush(pps-&gt;hdc, hbrOld);\n}\n<\/pre>\n<p>\n        And add the following code to WinMain before the call to <code>CreateWindowEx<\/code>:\n    <\/p>\n<pre>    g_hbr = CreatePatternBrushFromFile(lpCmdLine);\n    if (!g_hbr) return 0;\n<\/pre>\n<p>\n        This time, since we are managing the brush ourselves we need to remember to\n    <\/p>\n<pre>    DeleteObject(g_hbr);\n<\/pre>\n<p>\n        at the end of WinMain before it returns.\n    <\/p>\n<p>    This program draws an ellipse filled with your bitmap. The <code>FillPath<\/code> function\n    uses the currently-selected brush, so we select our bitmap brush (instead of a boring\n    solid brush) and draw with that. Result: A pattern-filled ellipse. Without a bitmap\n    brush, you would have had to do a lot of work manually clipping the bitmap (and tiling\n    it) to the ellipse.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bitmap brushes used to be these little 8&#215;8 monochrome patterns that you could use for hatching and maybe little houndstooth patterns if you were really crazy. But you can do better. CreatePatternBrush lets you pass in any old bitmap &#8211; even a huge one, and it will create a brush from it. The bitmap will [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-42213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Bitmap brushes used to be these little 8&#215;8 monochrome patterns that you could use for hatching and maybe little houndstooth patterns if you were really crazy. But you can do better. CreatePatternBrush lets you pass in any old bitmap &#8211; even a huge one, and it will create a brush from it. The bitmap will [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/42213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=42213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/42213\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=42213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=42213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=42213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}