{"id":106048,"date":"2021-12-22T07:00:00","date_gmt":"2021-12-22T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106048"},"modified":"2021-12-17T21:21:04","modified_gmt":"2021-12-18T05:21:04","slug":"20211222-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20211222-00\/?p=106048","title":{"rendered":"How do I programmatically reposition monitors in a multiple-monitor system?"},"content":{"rendered":"<p>Say you&#8217;re looking to write a tool to manipulate monitor positions. What functions should you be calling?<\/p>\n<p>The function to call is <code>Change\u00adDisplay\u00adSettings\u00adEx<\/code> with a <code>DEVMODE<\/code> whose <code>dmFields<\/code> has the <code>DM_POSITION<\/code> flag set. Put the desired upper left corner of the monitor in the <code>dmPosition.x<\/code> and <code>dmPosition.y<\/code> fields. You&#8217;re probably also going to want to update the monitor size, which you do by setting the <code>DM_PELSWIDTH<\/code> and <code>DM_PELSHEIGHT<\/code> flags, putting the corresponding values in the <code>dmPelsWidth<\/code> and <code>dmPelsHeight<\/code> members. To remove a monitor from the desktop, set the position to (0,0) and the width and height to zero.<\/p>\n<p>The overall pattern is to use <code>Enum\u00adDisplay\u00adDevices<\/code> to get all the available display devices. For each one, call <code>Enum\u00adDisplay\u00adSettings\u00adEx<\/code> to get the current <code>DEVMODE<\/code> for each monitor, using <code>ENUM_<wbr \/>CURRENT_<wbr \/>SETTINGS<\/code> to get whatever the monitor setting is right now, or <code>ENUM_<wbr \/>REGISTRY_<wbr \/>SETTINGS<\/code> to get the saved settings. (The current settings may be different from the registry settings if the display is in a temporary mode, such as when playing a fullscreen DirectX game.)<\/p>\n<p>After you gather all of the <code>DEVMODE<\/code>s, you modify the things you want to modify, and then <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winuser\/nf-winuser-changedisplaysettingsexw\"> stage the values<\/a> by calling <code>Change\u00adDisplay\u00adSettings\u00adEx<\/code> to set the updated values with the <code>CDS_<wbr \/>UPDATE\u00adREGISTRY<\/code> and <code>CDS_<wbr \/>NORESET<\/code> flags.<\/p>\n<p>Finally, once you&#8217;re done setting all the coordinates, perform a final<\/p>\n<pre>ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);\r\n<\/pre>\n<p>to apply all the settings at once.<\/p>\n<p>MSDN provides <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/gdi\/enumeration-and-display-control\"> a sample function that detaches all secondary monitors<\/a> to give you an idea of how the whole thing fits together.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The magic phrase is <CODE>DM_POSITION<\/CODE>.<\/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-106048","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The magic phrase is <CODE>DM_POSITION<\/CODE>.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106048","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=106048"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106048\/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=106048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}